Replace rfxtrx entity events with integration events (#37565)

* Drop per entity events

* Correct linting error

* Drop keys()
This commit is contained in:
Joakim Plate authored and GitHub committed 2020-07-13 02:57:19 +02:00
1 parent d3b47b6d89
commit 87f6b7cdd4
14 files changed
+132 -183

No files matched your search

+9 -16
View File
@@ -9,12 +9,11 @@ from homeassistant.components.sensor import (
DEVICE_CLASS_SIGNAL_STRENGTH,
DEVICE_CLASS_TEMPERATURE,
)
from homeassistant.const import ATTR_ENTITY_ID, CONF_DEVICES
from homeassistant.const import CONF_DEVICES
from homeassistant.helpers.entity import Entity
from . import (
CONF_AUTOMATIC_ADD,
CONF_FIRE_EVENT,
DATA_TYPES,
SIGNAL_EVENT,
get_device_id,
@@ -63,7 +62,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
return isinstance(event, (ControlEvent, SensorEvent))
entities = []
for packet_id, entity_info in discovery_info[CONF_DEVICES].items():
for packet_id in discovery_info[CONF_DEVICES]:
event = get_rfx_object(packet_id)
if event is None:
_LOGGER.error("Invalid device: %s", packet_id)
@@ -78,19 +77,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
continue
data_ids.add(data_id)
entity = RfxtrxSensor(
event.device, data_type, entity_info[CONF_FIRE_EVENT],
)
entity = RfxtrxSensor(event.device, device_id, data_type)
entities.append(entity)
add_entities(entities)
def sensor_update(event):
def sensor_update(event, device_id):
"""Handle sensor updates from the RFXtrx gateway."""
if not supported(event):
return
device_id = get_device_id(event.device)
for data_type in set(event.values) & set(DATA_TYPES):
data_id = (*device_id, data_type)
if data_id in data_ids:
@@ -105,7 +101,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"".join(f"{x:02x}" for x in event.data),
)
entity = RfxtrxSensor(event.device, data_type, event=event)
entity = RfxtrxSensor(event.device, device_id, data_type, event=event)
add_entities([entity])
# Subscribe to main RFXtrx events
@@ -116,15 +112,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class RfxtrxSensor(Entity):
"""Representation of a RFXtrx sensor."""
def __init__(self, device, data_type, should_fire_event=False, event=None):
def __init__(self, device, device_id, data_type, event=None):
"""Initialize the sensor."""
self.event = None
self._device = device
self._name = f"{device.type_string} {device.id_string} {data_type}"
self.should_fire_event = should_fire_event
self.data_type = data_type
self._unit_of_measurement = DATA_TYPES.get(data_type, "")
self._device_id = get_device_id(device)
self._device_id = device_id
self._unique_id = "_".join(x for x in (*self._device_id, data_type))
self._device_class = DEVICE_CLASSES.get(data_type)
@@ -186,12 +181,12 @@ class RfxtrxSensor(Entity):
"""Apply command from rfxtrx."""
self.event = event
def _handle_event(self, event):
def _handle_event(self, event, device_id):
"""Check if event applies to me and update."""
if not isinstance(event, SensorEvent):
return
if event.device.id_string != self._device.id_string:
if device_id != self._device_id:
return
if self.data_type not in event.values:
@@ -207,5 +202,3 @@ class RfxtrxSensor(Entity):
self._apply_event(event)
self.schedule_update_ha_state()
if self.should_fire_event:
self.hass.bus.fire("signal_received", {ATTR_ENTITY_ID: self.entity_id})