Refactor tellstick code (#4460)
* Refactor tellstick code for increased readability. Especially highlight if "device" is a telldus core device or a HA entity. * Refactor Tellstick object model for increased clarity. * Update comments. Unify better with sensors. Fix typo bug. Add debug logging. * Refactor tellstick code for increased readability. Especially highlight if "device" is a telldus core device or a HA entity. * Refactor Tellstick object model for increased clarity. * Update comments. Unify better with sensors. Fix typo bug. Add debug logging. * Fix lint issues.
This commit is contained in:
1 parent
65b85ec6c0
commit
1d8a1df2c4
4 files changed
+204
-175
No files matched your search
@@ -16,6 +16,8 @@ import homeassistant.helpers.config_validation as cv
|
||||
|
||||
REQUIREMENTS = ['tellcore-py==1.1.2']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DatatypeDescription = namedtuple('DatatypeDescription', ['name', 'unit'])
|
||||
|
||||
CONF_DATATYPE_MASK = 'datatype_mask'
|
||||
@@ -65,27 +67,28 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
}
|
||||
|
||||
try:
|
||||
core = telldus.TelldusCore()
|
||||
tellcore_lib = telldus.TelldusCore()
|
||||
except OSError:
|
||||
logging.getLogger(__name__).exception('Could not initialize Tellstick')
|
||||
_LOGGER.exception('Could not initialize Tellstick')
|
||||
return
|
||||
|
||||
sensors = []
|
||||
datatype_mask = config.get(CONF_DATATYPE_MASK)
|
||||
|
||||
for ts_sensor in core.sensors():
|
||||
for tellcore_sensor in tellcore_lib.sensors():
|
||||
try:
|
||||
sensor_name = config[ts_sensor.id]
|
||||
sensor_name = config[tellcore_sensor.id]
|
||||
except KeyError:
|
||||
if config.get(CONF_ONLY_NAMED):
|
||||
continue
|
||||
sensor_name = str(ts_sensor.id)
|
||||
sensor_name = str(tellcore_sensor.id)
|
||||
|
||||
for datatype in sensor_value_descriptions:
|
||||
if datatype & datatype_mask and ts_sensor.has_value(datatype):
|
||||
sensor_info = sensor_value_descriptions[datatype]
|
||||
sensors.append(TellstickSensor(
|
||||
sensor_name, ts_sensor, datatype, sensor_info))
|
||||
if datatype & datatype_mask:
|
||||
if tellcore_sensor.has_value(datatype):
|
||||
sensor_info = sensor_value_descriptions[datatype]
|
||||
sensors.append(TellstickSensor(
|
||||
sensor_name, tellcore_sensor, datatype, sensor_info))
|
||||
|
||||
add_devices(sensors)
|
||||
|
||||
@@ -93,10 +96,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
class TellstickSensor(Entity):
|
||||
"""Representation of a Tellstick sensor."""
|
||||
|
||||
def __init__(self, name, sensor, datatype, sensor_info):
|
||||
def __init__(self, name, tellcore_sensor, datatype, sensor_info):
|
||||
"""Initialize the sensor."""
|
||||
self.datatype = datatype
|
||||
self.sensor = sensor
|
||||
self._datatype = datatype
|
||||
self._tellcore_sensor = tellcore_sensor
|
||||
self._unit_of_measurement = sensor_info.unit or None
|
||||
self._value = None
|
||||
|
||||
@@ -119,4 +122,4 @@ class TellstickSensor(Entity):
|
||||
|
||||
def update(self):
|
||||
"""Update tellstick sensor."""
|
||||
self._value = self.sensor.value(self.datatype).value
|
||||
self._value = self._tellcore_sensor.value(self._datatype).value
|
||||
Reference in new issue
Block a user