2019-02-14 15:01:46 +00:00
|
|
|
"""Support for ISY994 sensors."""
|
2018-06-25 17:05:07 +00:00
|
|
|
from typing import Callable
|
2015-04-04 08:33:03 +00:00
|
|
|
|
2020-05-08 04:15:42 +00:00
|
|
|
from pyisy.constants import ISY_VALUE_UNKNOWN
|
|
|
|
|
2020-05-05 00:21:40 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR
|
2020-05-08 04:15:42 +00:00
|
|
|
from homeassistant.const import STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT
|
2016-09-11 18:18:53 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
|
2020-05-08 04:15:42 +00:00
|
|
|
from . import ISY994_NODES
|
2020-05-05 00:21:40 +00:00
|
|
|
from .const import _LOGGER, UOM_FRIENDLY_NAME, UOM_TO_STATES
|
2020-05-08 04:15:42 +00:00
|
|
|
from .entity import ISYNodeEntity
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass, config: ConfigType, add_entities: Callable[[list], None], discovery_info=None
|
|
|
|
):
|
2017-04-24 03:41:09 +00:00
|
|
|
"""Set up the ISY994 sensor platform."""
|
2016-09-11 18:18:53 +00:00
|
|
|
devices = []
|
|
|
|
|
2020-05-05 00:21:40 +00:00
|
|
|
for node in hass.data[ISY994_NODES][SENSOR]:
|
2017-12-26 08:26:37 +00:00
|
|
|
_LOGGER.debug("Loading %s", node.name)
|
2020-05-05 04:03:12 +00:00
|
|
|
devices.append(ISYSensorEntity(node))
|
2015-04-13 05:47:32 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devices)
|
2015-04-04 08:33:03 +00:00
|
|
|
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYSensorEntity(ISYNodeEntity):
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Representation of an ISY994 sensor device."""
|
2015-04-04 08:33:03 +00:00
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
@property
|
|
|
|
def raw_unit_of_measurement(self) -> str:
|
|
|
|
"""Get the raw unit of measurement for the ISY994 sensor device."""
|
2020-05-08 04:15:42 +00:00
|
|
|
uom = self._node.uom
|
|
|
|
|
|
|
|
# Backwards compatibility for ISYv4 Firmware:
|
|
|
|
if isinstance(uom, list):
|
|
|
|
return UOM_FRIENDLY_NAME.get(uom[0], uom[0])
|
|
|
|
return UOM_FRIENDLY_NAME.get(uom)
|
2015-04-04 08:33:03 +00:00
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Get the state of the ISY994 sensor device."""
|
2020-05-08 04:15:42 +00:00
|
|
|
if self.value == ISY_VALUE_UNKNOWN:
|
|
|
|
return STATE_UNKNOWN
|
|
|
|
|
|
|
|
uom = self._node.uom
|
|
|
|
# Backwards compatibility for ISYv4 Firmware:
|
|
|
|
if isinstance(uom, list):
|
|
|
|
uom = uom[0]
|
|
|
|
if not uom:
|
|
|
|
return STATE_UNKNOWN
|
|
|
|
|
|
|
|
states = UOM_TO_STATES.get(uom)
|
|
|
|
if states and states.get(self.value):
|
|
|
|
return states.get(self.value)
|
|
|
|
if self._node.prec and int(self._node.prec) != 0:
|
|
|
|
str_val = str(self.value)
|
|
|
|
int_prec = int(self._node.prec)
|
|
|
|
decimal_part = str_val[-int_prec:]
|
|
|
|
whole_part = str_val[: len(str_val) - int_prec]
|
|
|
|
val = float(f"{whole_part}.{decimal_part}")
|
|
|
|
raw_units = self.raw_unit_of_measurement
|
|
|
|
if raw_units in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
|
|
|
|
val = self.hass.config.units.temperature(val, raw_units)
|
|
|
|
return val
|
|
|
|
return self.value
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""Get the unit of measurement for the ISY994 sensor device."""
|
|
|
|
raw_units = self.raw_unit_of_measurement
|
|
|
|
if raw_units in (TEMP_FAHRENHEIT, TEMP_CELSIUS):
|
|
|
|
return self.hass.config.units.temperature_unit
|
2017-07-06 06:30:01 +00:00
|
|
|
return raw_units
|