From db623040a4a2c9dfd21b00e22aa22956735a6a6e Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Thu, 5 Jan 2017 17:33:52 -0500 Subject: [PATCH] Re-enabled Weather Sensors for the ISY component. (#5148) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Re-enabled Weather Sensors for the ISY component. As the ISY component had been updated to support newer components and have better UOM support, the support for ISY’s weather module was dropped. This adds that support back into Home Assistant. * Cleanup of the ISY Weather support. Cleaned up the for loops used to generate nodes representing weather data from the ISY. Moved the weather_node named tuple to the top of the file so that it can be more easily identified. * Update isy994.py --- homeassistant/components/isy994.py | 19 +++++++++ homeassistant/components/sensor/isy994.py | 47 +++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/homeassistant/components/isy994.py b/homeassistant/components/isy994.py index 0539469f198..7451b3286f7 100644 --- a/homeassistant/components/isy994.py +++ b/homeassistant/components/isy994.py @@ -4,6 +4,7 @@ Support the ISY-994 controllers. For configuration details please visit the documentation for this component at https://home-assistant.io/components/isy994/ """ +from collections import namedtuple import logging from urllib.parse import urlparse import voluptuous as vol @@ -47,6 +48,7 @@ CONFIG_SCHEMA = vol.Schema({ }, extra=vol.ALLOW_EXTRA) SENSOR_NODES = [] +WEATHER_NODES = [] NODES = [] GROUPS = [] PROGRAMS = {} @@ -59,6 +61,9 @@ SUPPORTED_DOMAINS = ['binary_sensor', 'cover', 'fan', 'light', 'lock', 'sensor', 'switch'] +WeatherNode = namedtuple('WeatherNode', ('status', 'name', 'uom')) + + def filter_nodes(nodes: list, units: list=None, states: list=None) -> list: """Filter a list of ISY nodes based on the units and states provided.""" filtered_nodes = [] @@ -132,6 +137,17 @@ def _categorize_programs() -> None: PROGRAMS[component].append(program) +def _categorize_weather() -> None: + """Categorize the ISY994 weather data.""" + global WEATHER_NODES + + climate_attrs = dir(ISY.climate) + WEATHER_NODES = [WeatherNode(getattr(ISY.climate, attr), attr, + getattr(ISY.climate, attr + '_units')) + for attr in climate_attrs + if attr + '_units' in climate_attrs] + + def setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the ISY 994 platform.""" isy_config = config.get(DOMAIN) @@ -178,6 +194,9 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: _categorize_programs() + if ISY.configuration.get('Weather Information'): + _categorize_weather() + # Listen for HA stop to disconnect. hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop) diff --git a/homeassistant/components/sensor/isy994.py b/homeassistant/components/sensor/isy994.py index df3ae9ed7ba..d35c76b81dc 100644 --- a/homeassistant/components/sensor/isy994.py +++ b/homeassistant/components/sensor/isy994.py @@ -251,6 +251,9 @@ def setup_platform(hass, config: ConfigType, _LOGGER.debug('LOADING %s', node.name) devices.append(ISYSensorDevice(node)) + for node in isy.WEATHER_NODES: + devices.append(ISYWeatherDevice(node)) + add_devices(devices) @@ -309,3 +312,47 @@ class ISYSensorDevice(isy.ISYDevice): return self.hass.config.units.temperature_unit else: return raw_units + + +class ISYWeatherDevice(isy.ISYDevice): + """Representation of an ISY994 weather device.""" + + _domain = 'sensor' + + def __init__(self, node) -> None: + """Initialize the ISY994 weather device.""" + isy.ISYDevice.__init__(self, node) + + @property + def unique_id(self) -> str: + """Return the unique identifier for the node.""" + return self._node.name + + @property + def raw_units(self) -> str: + """Return the raw unit of measurement.""" + if self._node.uom == 'F': + return TEMP_FAHRENHEIT + if self._node.uom == 'C': + return TEMP_CELSIUS + return self._node.uom + + @property + def state(self) -> object: + """Return the value of the node.""" + # pylint: disable=protected-access + val = self._node.status._val + raw_units = self._node.uom + + if raw_units in [TEMP_CELSIUS, TEMP_FAHRENHEIT]: + return self.hass.config.units.temperature(val, raw_units) + return val + + @property + def unit_of_measurement(self) -> str: + """Return the unit of measurement for the node.""" + raw_units = self.raw_units + + if raw_units in [TEMP_CELSIUS, TEMP_FAHRENHEIT]: + return self.hass.config.units.temperature_unit + return raw_units