1 """Support for Ubiquiti mFi sensors."""
3 from __future__
import annotations
7 from mficlient.client
import FailedToLogin, MFiClient
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
32 _LOGGER = logging.getLogger(__name__)
35 DEFAULT_VERIFY_SSL =
True
37 DIGITS = {
"volts": 1,
"amps": 1,
"active_power": 0,
"temperature": 1}
48 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
50 vol.Required(CONF_HOST): cv.string,
51 vol.Required(CONF_USERNAME): cv.string,
52 vol.Required(CONF_PASSWORD): cv.string,
53 vol.Optional(CONF_PORT): cv.port,
54 vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
55 vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
63 add_entities: AddEntitiesCallback,
64 discovery_info: DiscoveryInfoType |
None =
None,
66 """Set up mFi sensors."""
67 host = config.get(CONF_HOST)
68 username = config.get(CONF_USERNAME)
69 password = config.get(CONF_PASSWORD)
70 use_tls = config.get(CONF_SSL)
71 verify_tls = config.get(CONF_VERIFY_SSL)
72 default_port = 6443
if use_tls
else 6080
73 port =
int(config.get(CONF_PORT, default_port))
77 host, username, password, port=port, use_tls=use_tls, verify=verify_tls
79 except (FailedToLogin, requests.exceptions.ConnectionError)
as ex:
80 _LOGGER.error(
"Unable to connect to mFi: %s",
str(ex))
85 for device
in client.get_devices()
86 for port
in device.ports.values()
87 if port.model
in SENSOR_MODELS
92 """Representation of a mFi sensor."""
95 """Initialize the sensor."""
101 """Return the name of the sensor."""
102 return self.
_port_port.label
106 """Return the state of the sensor."""
108 tag = self.
_port_port.tag
113 if self.
_port_port.model ==
"Input Digital":
114 return STATE_ON
if self.
_port_port.value > 0
else STATE_OFF
115 digits = DIGITS.get(self.
_port_port.tag, 0)
116 return round(self.
_port_port.value, digits)
120 """Return the device class of the sensor."""
122 tag = self.
_port_port.tag
126 if tag ==
"temperature":
127 return SensorDeviceClass.TEMPERATURE
133 """Return the unit of measurement of this entity, if any."""
135 tag = self.
_port_port.tag
139 if tag ==
"temperature":
140 return UnitOfTemperature.CELSIUS
141 if tag ==
"active_pwr":
143 if self.
_port_port.model ==
"Input Digital":
148 """Get the latest data."""
149 self.
_port_port.refresh()
def native_unit_of_measurement(self)
def __init__(self, port, hass)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)