core/homeassistant/components/isy994/sensor.py

76 lines
2.5 KiB
Python
Raw Normal View History

"""Support for ISY994 sensors."""
from typing import Callable
2015-04-04 08:33:03 +00:00
from pyisy.constants import ISY_VALUE_UNKNOWN
from homeassistant.components.sensor import DOMAIN as SENSOR
from homeassistant.const import STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.helpers.typing import ConfigType
from . import ISY994_NODES
from .const import _LOGGER, UOM_FRIENDLY_NAME, UOM_TO_STATES
from .entity import ISYNodeEntity
2019-07-31 19:25:30 +00:00
def setup_platform(
hass, config: ConfigType, add_entities: Callable[[list], None], discovery_info=None
):
"""Set up the ISY994 sensor platform."""
devices = []
for node in hass.data[ISY994_NODES][SENSOR]:
Huge ISY994 platform cleanup, fixes support for 5.0.10 firmware (#11243) * Huge ISY994 platform cleanup, fixes support for 5.0.10 firmware # * No more globals - store on hass.data # * Parent ISY994 component handles categorizing nodes in to Hass components, rather than each individual domain filtering all nodes themselves # * Remove hidden string, replace with ignore string. Hidden should be done via the customize block; ignore fully prevents the node from getting a Hass entity # * Removed a few unused methods in the ISYDevice class # * Cleaned up the hostname parsing # * Removed broken logic in the fan Program component. It was setting properties that have no setters # * Added the missing SUPPORTED_FEATURES to the fan component to indicate that it can set speed # * Added better error handling and a log warning when an ISY994 program entity fails to initialize # * Cleaned up a few instances of unecessarily complicated logic paths, and other cases of unnecessary logic that is already handled by base classes * Use `super()` instead of explicit base class calls * Move `hass` argument to first position * Use str.format instead of string addition * Move program structure building and validation to component Removes the need for a bunch of duplicate exception handling in each individual platform * Fix climate nodes, fix climate names, add config to disable climate Sensor platform was crashing when the ISY reported climate nodes. Logic has been fixed. Also added a config option to prevent climate sensors from getting imported from the ISY. Also replace the underscore from climate node names with spaces so they default to friendly names. * Space missing in error message * Fix string comparison to use `==` * Explicitly check for attributes rather than catch AttributeError Also removes two stray debug lines * Remove null checks on hass.data, as they are always null at this point
2017-12-26 08:26:37 +00:00
_LOGGER.debug("Loading %s", node.name)
devices.append(ISYSensorEntity(node))
add_entities(devices)
2015-04-04 08:33:03 +00:00
class ISYSensorEntity(ISYNodeEntity):
"""Representation of an ISY994 sensor device."""
2015-04-04 08:33:03 +00:00
@property
def raw_unit_of_measurement(self) -> str:
"""Get the raw unit of measurement for the ISY994 sensor device."""
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
@property
def state(self) -> str:
"""Get the state of the ISY994 sensor device."""
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
@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
return raw_units