Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Ubiquiti mFi switches."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from mficlient.client import FailedToLogin, MFiClient
9 import requests
10 import voluptuous as vol
11 
13  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
14  SwitchEntity,
15 )
16 from homeassistant.const import (
17  CONF_HOST,
18  CONF_PASSWORD,
19  CONF_PORT,
20  CONF_SSL,
21  CONF_USERNAME,
22  CONF_VERIFY_SSL,
23 )
24 from homeassistant.core import HomeAssistant
26 from homeassistant.helpers.entity_platform import AddEntitiesCallback
27 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 DEFAULT_SSL = True
32 DEFAULT_VERIFY_SSL = True
33 
34 SWITCH_MODELS = ["Outlet", "Output 5v", "Output 12v", "Output 24v", "Dimmer Switch"]
35 
36 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
37  {
38  vol.Required(CONF_HOST): cv.string,
39  vol.Required(CONF_USERNAME): cv.string,
40  vol.Required(CONF_PASSWORD): cv.string,
41  vol.Optional(CONF_PORT): cv.port,
42  vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
43  vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
44  }
45 )
46 
47 
49  hass: HomeAssistant,
50  config: ConfigType,
51  add_entities: AddEntitiesCallback,
52  discovery_info: DiscoveryInfoType | None = None,
53 ) -> None:
54  """Set up mFi sensors."""
55  host = config.get(CONF_HOST)
56  username = config.get(CONF_USERNAME)
57  password = config.get(CONF_PASSWORD)
58  use_tls = config[CONF_SSL]
59  verify_tls = config.get(CONF_VERIFY_SSL)
60  default_port = 6443 if use_tls else 6080
61  port = int(config.get(CONF_PORT, default_port))
62 
63  try:
64  client = MFiClient(
65  host, username, password, port=port, use_tls=use_tls, verify=verify_tls
66  )
67  except (FailedToLogin, requests.exceptions.ConnectionError) as ex:
68  _LOGGER.error("Unable to connect to mFi: %s", str(ex))
69  return
70 
72  MfiSwitch(port)
73  for device in client.get_devices()
74  for port in device.ports.values()
75  if port.model in SWITCH_MODELS
76  )
77 
78 
80  """Representation of an mFi switch-able device."""
81 
82  def __init__(self, port):
83  """Initialize the mFi device."""
84  self._port_port = port
85  self._target_state_target_state = None
86 
87  @property
88  def unique_id(self):
89  """Return the unique ID of the device."""
90  return self._port_port.ident
91 
92  @property
93  def name(self):
94  """Return the name of the device."""
95  return self._port_port.label
96 
97  @property
98  def is_on(self):
99  """Return true if the device is on."""
100  return self._port_port.output
101 
102  def update(self) -> None:
103  """Get the latest state and update the state."""
104  self._port_port.refresh()
105  if self._target_state_target_state is not None:
106  self._port_port.data["output"] = float(self._target_state_target_state)
107  self._target_state_target_state = None
108 
109  def turn_on(self, **kwargs: Any) -> None:
110  """Turn the switch on."""
111  self._port_port.control(True)
112  self._target_state_target_state = True
113 
114  def turn_off(self, **kwargs: Any) -> None:
115  """Turn the switch off."""
116  self._port_port.control(False)
117  self._target_state_target_state = False
None turn_on(self, **Any kwargs)
Definition: switch.py:109
None turn_off(self, **Any kwargs)
Definition: switch.py:114
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:53