1 """Support for Ubiquiti mFi switches."""
3 from __future__
import annotations
8 from mficlient.client
import FailedToLogin, MFiClient
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SWITCH_PLATFORM_SCHEMA,
29 _LOGGER = logging.getLogger(__name__)
32 DEFAULT_VERIFY_SSL =
True
34 SWITCH_MODELS = [
"Outlet",
"Output 5v",
"Output 12v",
"Output 24v",
"Dimmer Switch"]
36 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
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,
51 add_entities: AddEntitiesCallback,
52 discovery_info: DiscoveryInfoType |
None =
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))
65 host, username, password, port=port, use_tls=use_tls, verify=verify_tls
67 except (FailedToLogin, requests.exceptions.ConnectionError)
as ex:
68 _LOGGER.error(
"Unable to connect to mFi: %s",
str(ex))
73 for device
in client.get_devices()
74 for port
in device.ports.values()
75 if port.model
in SWITCH_MODELS
80 """Representation of an mFi switch-able device."""
83 """Initialize the mFi device."""
89 """Return the unique ID of the device."""
90 return self.
_port_port.ident
94 """Return the name of the device."""
95 return self.
_port_port.label
99 """Return true if the device is on."""
100 return self.
_port_port.output
103 """Get the latest state and update the state."""
104 self.
_port_port.refresh()
110 """Turn the switch on."""
111 self.
_port_port.control(
True)
115 """Turn the switch off."""
116 self.
_port_port.control(
False)
None turn_on(self, **Any kwargs)
None turn_off(self, **Any kwargs)
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)