2019-02-14 15:01:46 +00:00
|
|
|
"""Support for ISY994 sensors."""
|
2020-05-12 02:32:19 +00:00
|
|
|
from typing import Callable, Dict, Union
|
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-09 19:49:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-05-12 02:32:19 +00:00
|
|
|
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
|
2020-05-09 19:49:00 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
_LOGGER,
|
|
|
|
DOMAIN as ISY994_DOMAIN,
|
|
|
|
ISY994_NODES,
|
2020-05-10 14:40:19 +00:00
|
|
|
ISY994_VARIABLES,
|
2020-05-12 02:32:19 +00:00
|
|
|
UOM_DOUBLE_TEMP,
|
2020-05-09 19:49:00 +00:00
|
|
|
UOM_FRIENDLY_NAME,
|
|
|
|
UOM_TO_STATES,
|
|
|
|
)
|
2020-05-10 14:40:19 +00:00
|
|
|
from .entity import ISYEntity, ISYNodeEntity
|
2020-05-12 02:32:19 +00:00
|
|
|
from .helpers import convert_isy_value_to_hass, migrate_old_unique_ids
|
2020-05-11 15:58:58 +00:00
|
|
|
from .services import async_setup_device_services
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[list], None],
|
|
|
|
) -> bool:
|
2017-04-24 03:41:09 +00:00
|
|
|
"""Set up the ISY994 sensor platform."""
|
2020-05-09 19:49:00 +00:00
|
|
|
hass_isy_data = hass.data[ISY994_DOMAIN][entry.entry_id]
|
2016-09-11 18:18:53 +00:00
|
|
|
devices = []
|
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
for node in hass_isy_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
|
|
|
|
2020-05-10 14:40:19 +00:00
|
|
|
for vname, vobj in hass_isy_data[ISY994_VARIABLES]:
|
|
|
|
devices.append(ISYSensorVariableEntity(vname, vobj))
|
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
await migrate_old_unique_ids(hass, SENSOR, devices)
|
|
|
|
async_add_entities(devices)
|
2020-05-11 15:58:58 +00:00
|
|
|
async_setup_device_services(hass)
|
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
|
2020-05-12 02:32:19 +00:00
|
|
|
def raw_unit_of_measurement(self) -> Union[dict, str]:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""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])
|
2020-05-12 02:32:19 +00:00
|
|
|
|
|
|
|
# Special cases for ISY UOM index units:
|
|
|
|
isy_states = UOM_TO_STATES.get(uom)
|
|
|
|
if isy_states:
|
|
|
|
return isy_states
|
|
|
|
|
2020-05-08 04:15:42 +00:00
|
|
|
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-12 02:32:19 +00:00
|
|
|
value = self._node.status
|
|
|
|
if value == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
2020-05-08 04:15:42 +00:00
|
|
|
|
2020-05-12 02:32:19 +00:00
|
|
|
# Get the translated ISY Unit of Measurement
|
|
|
|
uom = self.raw_unit_of_measurement
|
|
|
|
|
|
|
|
# Check if this is a known index pair UOM
|
|
|
|
if isinstance(uom, dict):
|
|
|
|
return uom.get(value, value)
|
|
|
|
|
|
|
|
# Handle ISY precision and rounding
|
|
|
|
value = convert_isy_value_to_hass(value, uom, self._node.prec)
|
|
|
|
|
|
|
|
# Convert temperatures to Home Assistant's unit
|
|
|
|
if uom in (TEMP_CELSIUS, TEMP_FAHRENHEIT):
|
|
|
|
value = self.hass.config.units.temperature(value, uom)
|
|
|
|
|
|
|
|
return value
|
2016-09-11 18:18:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self) -> str:
|
2020-05-12 02:32:19 +00:00
|
|
|
"""Get the Home Assistant unit of measurement for the device."""
|
2016-09-11 18:18:53 +00:00
|
|
|
raw_units = self.raw_unit_of_measurement
|
2020-05-12 02:32:19 +00:00
|
|
|
# Check if this is a known index pair UOM
|
|
|
|
if isinstance(raw_units, dict):
|
|
|
|
return None
|
|
|
|
if raw_units in (TEMP_FAHRENHEIT, TEMP_CELSIUS, UOM_DOUBLE_TEMP):
|
2016-09-11 18:18:53 +00:00
|
|
|
return self.hass.config.units.temperature_unit
|
2017-07-06 06:30:01 +00:00
|
|
|
return raw_units
|
2020-05-10 14:40:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ISYSensorVariableEntity(ISYEntity):
|
|
|
|
"""Representation of an ISY994 variable as a sensor device."""
|
|
|
|
|
|
|
|
def __init__(self, vname: str, vobj: object) -> None:
|
|
|
|
"""Initialize the ISY994 binary sensor program."""
|
|
|
|
super().__init__(vobj)
|
|
|
|
self._name = vname
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the variable."""
|
2020-05-12 02:32:19 +00:00
|
|
|
return self._node.status
|
2020-05-10 14:40:19 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self) -> Dict:
|
|
|
|
"""Get the state attributes for the device."""
|
|
|
|
return {"init_value": int(self._node.init)}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return "mdi:counter"
|