1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Cleaned up ISY994 light and sensor code to use the same abstract class.

This commit is contained in:
Ryan Kraus
2015-04-12 16:45:23 -04:00
parent 57f27cc97a
commit f6d75f2db2
3 changed files with 141 additions and 156 deletions

View File

@@ -3,7 +3,7 @@
import logging
# homeassistant imports
from ..isy994 import ISY
from homeassistant.components.isy994 import ISY, ISYDeviceABC
from homeassistant.helpers.entity import Entity
from homeassistant.const import STATE_OPEN, STATE_CLOSED
@@ -21,80 +21,25 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if ISY.climate is not None:
for prop in ISY.climate._id2name:
if prop is not None:
devs.append(ISYSensorDevice('ISY.weather.' + prop, prop,
getattr(ISY.climate, prop),
getattr(ISY.climate, prop + '_units')))
node = WeatherPseudoNode('ISY.weather.' + prop, prop,
getattr(ISY.climate, prop),
getattr(ISY.climate, prop + '_units'))
devs.append(ISYSensorDevice(node))
add_devices(devs)
class ISYSensorDevice(Entity):
class WeatherPseudoNode(object):
""" This class allows weather variable to act as regular nodes. """
def __init__(self, device_id, name, status, units=None):
self._id = device_id
self.name = name
self.status = status
self.units = units
class ISYSensorDevice(ISYDeviceABC):
""" represents a isy sensor within home assistant. """
domain = 'sensor'
def __init__(self, device_id, name, source, units=None):
# setup properties
self._id = device_id
self._name = name
self.entity_id = self.domain + '.' + self.name.replace(' ', '_')
self._source = source
self._units = units
# track changes
self._changeHandler = self._source.subscribe('changed', self.onUpdate)
def __del__(self):
self._changeHandler.unsubscribe()
@property
def should_poll(self):
return False
@property
def dtype(self):
return 'binary' if self._units is None else 'analog'
@property
def state(self):
""" Returns the state. """
if self.dtype is 'binary':
return STATE_OPEN if self.is_open >= 255 else STATE_CLOSED
else:
return self.value
@property
def state_attributes(self):
return {}
@property
def unit_of_measurement(self):
return self._units
@property
def unique_id(self):
""" Returns the id of this isy sensor """
return self._id
@property
def name(self):
""" Returns the name of the sensor if any. """
return self._name
def update(self):
""" Update state of the sensor. """
# ISY objects are automatically updated by the ISY's event stream
pass
@property
def is_open(self):
""" True if door is open. """
return self.value >= 255
@property
def value(self):
return self._source._val
def onUpdate(self, e):
self.update_ha_state()
_domain = 'sensor'