refactor rfxtrx code
This commit is contained in:
parent
d6a14a1767
commit
23db6e753f
4 changed files with 93 additions and 78 deletions
|
@ -11,6 +11,8 @@ import homeassistant.components.rfxtrx as rfxtrx
|
|||
from homeassistant.const import TEMP_CELCIUS
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import slugify
|
||||
from homeassistant.components.rfxtrx import (
|
||||
ATTR_PACKETID, ATTR_NAME, ATTR_DATA_TYPE)
|
||||
|
||||
DEPENDENCIES = ['rfxtrx']
|
||||
|
||||
|
@ -29,25 +31,41 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||
"""Setup the RFXtrx platform."""
|
||||
from RFXtrx import SensorEvent
|
||||
|
||||
sensors = []
|
||||
for device_id, entity_info in config.get('devices', {}).items():
|
||||
if device_id not in rfxtrx.RFX_DEVICES:
|
||||
_LOGGER.info("Add %s rfxtrx.sensor", entity_info[ATTR_NAME])
|
||||
event = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
|
||||
new_sensor = RfxtrxSensor(event, entity_info[ATTR_NAME],
|
||||
entity_info.get(ATTR_DATA_TYPE, None))
|
||||
rfxtrx.RFX_DEVICES[device_id] = new_sensor
|
||||
sensors.append(new_sensor)
|
||||
|
||||
add_devices_callback(sensors)
|
||||
|
||||
def sensor_update(event):
|
||||
"""Callback for sensor updates from the RFXtrx gateway."""
|
||||
if isinstance(event, SensorEvent):
|
||||
entity_id = slugify(event.device.id_string.lower())
|
||||
if not isinstance(event, SensorEvent):
|
||||
return
|
||||
|
||||
# Add entity if not exist and the automatic_add is True
|
||||
if entity_id not in rfxtrx.RFX_DEVICES:
|
||||
automatic_add = config.get('automatic_add', True)
|
||||
if automatic_add:
|
||||
_LOGGER.info("Automatic add %s rfxtrx.sensor", entity_id)
|
||||
new_sensor = RfxtrxSensor(event)
|
||||
rfxtrx.RFX_DEVICES[entity_id] = new_sensor
|
||||
add_devices_callback([new_sensor])
|
||||
else:
|
||||
_LOGGER.debug(
|
||||
"EntityID: %s sensor_update",
|
||||
entity_id,
|
||||
)
|
||||
rfxtrx.RFX_DEVICES[entity_id].event = event
|
||||
device_id = "sensor_" + slugify(event.device.id_string.lower())
|
||||
|
||||
if device_id in rfxtrx.RFX_DEVICES:
|
||||
rfxtrx.RFX_DEVICES[device_id].event = event
|
||||
return
|
||||
|
||||
# Add entity if not exist and the automatic_add is True
|
||||
if config.get('automatic_add', True):
|
||||
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
|
||||
entity_name = "%s : %s" % (device_id, pkt_id)
|
||||
_LOGGER.info(
|
||||
"Automatic add rfxtrx.sensor: (%s : %s)",
|
||||
device_id,
|
||||
pkt_id)
|
||||
|
||||
new_sensor = RfxtrxSensor(event, entity_name)
|
||||
rfxtrx.RFX_DEVICES[device_id] = new_sensor
|
||||
add_devices_callback([new_sensor])
|
||||
|
||||
if sensor_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
|
||||
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(sensor_update)
|
||||
|
@ -56,21 +74,21 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||
class RfxtrxSensor(Entity):
|
||||
"""Represents a RFXtrx sensor."""
|
||||
|
||||
def __init__(self, event):
|
||||
def __init__(self, event, name, data_type=None):
|
||||
self.event = event
|
||||
self._unit_of_measurement = None
|
||||
self._data_type = None
|
||||
self._name = name
|
||||
if data_type:
|
||||
self._data_type = data_type
|
||||
self._unit_of_measurement = DATA_TYPES[data_type]
|
||||
return
|
||||
for data_type in DATA_TYPES:
|
||||
if data_type in self.event.values:
|
||||
self._unit_of_measurement = DATA_TYPES[data_type]
|
||||
self._data_type = data_type
|
||||
break
|
||||
|
||||
id_string = int(event.device.id_string.replace(":", ""), 16)
|
||||
self._name = "{} {} ({})".format(self._data_type,
|
||||
self.event.device.type_string,
|
||||
id_string)
|
||||
|
||||
def __str__(self):
|
||||
"""Returns the name."""
|
||||
return self._name
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue