Split handling and application of event (#37665)

This way _handle_event can contain things available
when entity has been added to home assistant,
and _apply event can remain internal and used on init.
This commit is contained in:
Joakim Plate authored and GitHub committed 2020-07-09 11:40:37 +02:00
1 parent 155a5f7c26
commit a3310330f4
6 files changed
+127 -123

No files matched your search

+12 -14
View File
@@ -65,7 +65,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
new_device = get_new_device(event, config, RfxtrxLight)
if new_device:
new_device.apply_event(event)
add_entities([new_device])
# Subscribe to main RFXtrx events
@@ -92,16 +91,9 @@ class RfxtrxLight(RfxtrxDevice, LightEntity, RestoreEntity):
):
self._brightness = int(old_state.attributes[ATTR_BRIGHTNESS])
def _handle_event(event):
"""Check if event applies to me and update."""
if event.device.id_string != self._event.device.id_string:
return
self.apply_event(event)
self.async_on_remove(
self.hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_EVENT, _handle_event
SIGNAL_EVENT, self._handle_event
)
)
@@ -131,7 +123,7 @@ class RfxtrxLight(RfxtrxDevice, LightEntity, RestoreEntity):
self._brightness = 0
self._send_command("turn_off")
def apply_event(self, event):
def _apply_event(self, event):
"""Apply command from rfxtrx."""
if event.values["Command"] in COMMAND_ON_LIST:
self._state = True
@@ -141,7 +133,13 @@ class RfxtrxLight(RfxtrxDevice, LightEntity, RestoreEntity):
self._brightness = event.values["Dim level"] * 255 // 100
self._state = self._brightness > 0
if self.hass:
self.schedule_update_ha_state()
if self.should_fire_event:
fire_command_event(self.hass, self.entity_id, event.values["Command"])
def _handle_event(self, event):
"""Check if event applies to me and update."""
if event.device.id_string != self._device.id_string:
return
self._apply_event(event)
self.schedule_update_ha_state()
if self.should_fire_event:
fire_command_event(self.hass, self.entity_id, event.values["Command"])