2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Ubiquiti mFi sensors."""
|
2022-01-04 10:30:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-02-06 21:36:41 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-05 05:14:26 +00:00
|
|
|
from mficlient.client import FailedToLogin, MFiClient
|
2016-02-25 23:48:46 +00:00
|
|
|
import requests
|
2016-09-04 02:32:35 +00:00
|
|
|
import voluptuous as vol
|
2016-02-25 23:48:46 +00:00
|
|
|
|
2021-12-16 14:51:36 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
)
|
2016-08-20 22:40:16 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
2019-12-05 05:14:26 +00:00
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SSL,
|
2019-12-05 05:14:26 +00:00
|
|
|
CONF_USERNAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_VERIFY_SSL,
|
2019-12-05 05:14:26 +00:00
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
2022-12-20 17:30:46 +00:00
|
|
|
UnitOfTemperature,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-04 10:30:13 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-09-04 02:32:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-04 10:30:13 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-09-04 02:32:35 +00:00
|
|
|
DEFAULT_SSL = True
|
|
|
|
DEFAULT_VERIFY_SSL = True
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DIGITS = {"volts": 1, "amps": 1, "active_power": 0, "temperature": 1}
|
2016-09-04 02:32:35 +00:00
|
|
|
|
2016-02-06 21:36:41 +00:00
|
|
|
SENSOR_MODELS = [
|
2019-07-31 19:25:30 +00:00
|
|
|
"Ubiquiti mFi-THS",
|
|
|
|
"Ubiquiti mFi-CS",
|
|
|
|
"Ubiquiti mFi-DS",
|
|
|
|
"Outlet",
|
|
|
|
"Input Analog",
|
|
|
|
"Input Digital",
|
2016-02-06 21:36:41 +00:00
|
|
|
]
|
2016-09-04 02:32:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
|
2022-01-04 10:30:13 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up mFi sensors."""
|
2016-08-20 22:40:16 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2016-02-06 21:36:41 +00:00
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
2016-09-04 02:32:35 +00:00
|
|
|
use_tls = config.get(CONF_SSL)
|
|
|
|
verify_tls = config.get(CONF_VERIFY_SSL)
|
2017-07-06 03:02:16 +00:00
|
|
|
default_port = 6443 if use_tls else 6080
|
2016-09-04 02:32:35 +00:00
|
|
|
port = int(config.get(CONF_PORT, default_port))
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
client = MFiClient(
|
|
|
|
host, username, password, port=port, use_tls=use_tls, verify=verify_tls
|
|
|
|
)
|
2016-02-25 23:48:46 +00:00
|
|
|
except (FailedToLogin, requests.exceptions.ConnectionError) as ex:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("Unable to connect to mFi: %s", str(ex))
|
2022-01-04 10:30:13 +00:00
|
|
|
return
|
2016-02-06 21:36:41 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
add_entities(
|
|
|
|
MfiSensor(port, hass)
|
|
|
|
for device in client.get_devices()
|
|
|
|
for port in device.ports.values()
|
|
|
|
if port.model in SENSOR_MODELS
|
|
|
|
)
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class MfiSensor(SensorEntity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a mFi sensor."""
|
2016-02-06 21:36:41 +00:00
|
|
|
|
|
|
|
def __init__(self, port, hass):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-02-06 21:36:41 +00:00
|
|
|
self._port = port
|
|
|
|
self._hass = hass
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2021-07-12 18:41:45 +00:00
|
|
|
"""Return the name of the sensor."""
|
2016-02-06 21:36:41 +00:00
|
|
|
return self._port.label
|
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:47 +00:00
|
|
|
def native_value(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2016-09-07 01:04:20 +00:00
|
|
|
try:
|
|
|
|
tag = self._port.tag
|
|
|
|
except ValueError:
|
|
|
|
tag = None
|
|
|
|
if tag is None:
|
|
|
|
return STATE_OFF
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._port.model == "Input Digital":
|
2017-07-06 03:02:16 +00:00
|
|
|
return STATE_ON if self._port.value > 0 else STATE_OFF
|
|
|
|
digits = DIGITS.get(self._port.tag, 0)
|
|
|
|
return round(self._port.value, digits)
|
2016-02-06 21:36:41 +00:00
|
|
|
|
2021-07-12 18:41:45 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
try:
|
|
|
|
tag = self._port.tag
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if tag == "temperature":
|
2021-12-16 14:51:36 +00:00
|
|
|
return SensorDeviceClass.TEMPERATURE
|
2021-07-12 18:41:45 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2016-02-06 21:36:41 +00:00
|
|
|
@property
|
2021-08-11 19:17:47 +00:00
|
|
|
def native_unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2016-09-07 01:04:20 +00:00
|
|
|
try:
|
|
|
|
tag = self._port.tag
|
|
|
|
except ValueError:
|
2023-01-10 15:45:59 +00:00
|
|
|
return None
|
2016-09-07 01:04:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if tag == "temperature":
|
2022-12-20 17:30:46 +00:00
|
|
|
return UnitOfTemperature.CELSIUS
|
2019-07-31 19:25:30 +00:00
|
|
|
if tag == "active_pwr":
|
|
|
|
return "Watts"
|
|
|
|
if self._port.model == "Input Digital":
|
2023-01-10 15:45:59 +00:00
|
|
|
return None
|
2016-09-07 01:04:20 +00:00
|
|
|
return tag
|
2016-02-06 21:36:41 +00:00
|
|
|
|
2022-09-05 08:59:36 +00:00
|
|
|
def update(self) -> None:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data."""
|
2016-02-06 21:36:41 +00:00
|
|
|
self._port.refresh()
|