* Use set not list * Events are not entities * Don't await unload_events * Remove checks of entities content in tests * List to set comprehension * Why is it so hard to remember that sets arent parenthesis...
This commit is contained in:
1 parent
8895752837
commit
66a8edb11e
12 files changed
+20
-67
No files matched your search
@@ -10,17 +10,10 @@ from .const import DOMAIN as DECONZ_DOMAIN
|
||||
class DeconzBase:
|
||||
"""Common base for deconz entities and events."""
|
||||
|
||||
TYPE = ""
|
||||
|
||||
def __init__(self, device, gateway):
|
||||
"""Set up device and add update callback to get data from websocket."""
|
||||
self._device = device
|
||||
self.gateway = gateway
|
||||
self.gateway.entities[self.TYPE].add(self.unique_id)
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Remove unique id."""
|
||||
self.gateway.entities[self.TYPE].remove(self.unique_id)
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
@@ -57,6 +50,13 @@ class DeconzBase:
|
||||
class DeconzDevice(DeconzBase, Entity):
|
||||
"""Representation of a deCONZ device."""
|
||||
|
||||
TYPE = ""
|
||||
|
||||
def __init__(self, device, gateway):
|
||||
"""Set up device and add update callback to get data from websocket."""
|
||||
super().__init__(device, gateway)
|
||||
self.gateway.entities[self.TYPE].add(self.unique_id)
|
||||
|
||||
@property
|
||||
def entity_registry_enabled_default(self):
|
||||
"""Return if the entity should be enabled when first added to the entity registry.
|
||||
@@ -83,7 +83,7 @@ class DeconzDevice(DeconzBase, Entity):
|
||||
self._device.remove_callback(self.async_update_callback)
|
||||
if self.entity_id in self.gateway.deconz_ids:
|
||||
del self.gateway.deconz_ids[self.entity_id]
|
||||
await super().async_will_remove_from_hass()
|
||||
self.gateway.entities[self.TYPE].remove(self.unique_id)
|
||||
|
||||
@callback
|
||||
def async_update_callback(self, force_update=False, ignore_update=False):
|
||||
|
||||
@@ -11,12 +11,9 @@ from .deconz_device import DeconzBase
|
||||
|
||||
CONF_DECONZ_EVENT = "deconz_event"
|
||||
|
||||
EVENT = "Event"
|
||||
|
||||
|
||||
async def async_setup_events(gateway) -> None:
|
||||
"""Set up the deCONZ events."""
|
||||
gateway.entities[EVENT] = set()
|
||||
|
||||
@callback
|
||||
def async_add_sensor(sensors):
|
||||
@@ -26,10 +23,9 @@ async def async_setup_events(gateway) -> None:
|
||||
if not gateway.option_allow_clip_sensor and sensor.type.startswith("CLIP"):
|
||||
continue
|
||||
|
||||
if (
|
||||
sensor.type not in Switch.ZHATYPE
|
||||
or sensor.uniqueid in gateway.entities[EVENT]
|
||||
):
|
||||
if sensor.type not in Switch.ZHATYPE or sensor.uniqueid in {
|
||||
event.unique_id for event in gateway.events
|
||||
}:
|
||||
continue
|
||||
|
||||
new_event = DeconzEvent(sensor, gateway)
|
||||
@@ -47,10 +43,11 @@ async def async_setup_events(gateway) -> None:
|
||||
)
|
||||
|
||||
|
||||
async def async_unload_events(gateway) -> None:
|
||||
@callback
|
||||
def async_unload_events(gateway) -> None:
|
||||
"""Unload all deCONZ events."""
|
||||
for event in gateway.events:
|
||||
await event.async_will_remove_from_hass()
|
||||
event.async_will_remove_from_hass()
|
||||
|
||||
gateway.events.clear()
|
||||
|
||||
@@ -62,8 +59,6 @@ class DeconzEvent(DeconzBase):
|
||||
instead of a sensor entity in hass.
|
||||
"""
|
||||
|
||||
TYPE = EVENT
|
||||
|
||||
def __init__(self, device, gateway):
|
||||
"""Register callback that will be used for signals."""
|
||||
super().__init__(device, gateway)
|
||||
@@ -79,10 +74,10 @@ class DeconzEvent(DeconzBase):
|
||||
"""Return Event device."""
|
||||
return self._device
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
@callback
|
||||
def async_will_remove_from_hass(self) -> None:
|
||||
"""Disconnect event object when removed."""
|
||||
self._device.remove_callback(self.async_update_callback)
|
||||
await super().async_will_remove_from_hass()
|
||||
|
||||
@callback
|
||||
def async_update_callback(self, force_update=False, ignore_update=False):
|
||||
|
||||
@@ -232,7 +232,7 @@ class DeconzGateway:
|
||||
unsub_dispatcher()
|
||||
self.listeners = []
|
||||
|
||||
await async_unload_events(self)
|
||||
async_unload_events(self)
|
||||
|
||||
self.deconz_ids = {}
|
||||
return True
|
||||
|
||||
@@ -75,7 +75,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
if not group.lights:
|
||||
continue
|
||||
|
||||
known_groups = list(gateway.entities[DOMAIN])
|
||||
known_groups = set(gateway.entities[DOMAIN])
|
||||
new_group = DeconzGroup(group, gateway)
|
||||
if new_group.unique_id not in known_groups:
|
||||
entities.append(new_group)
|
||||
|
||||
@@ -96,7 +96,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
if sensor.battery is not None:
|
||||
battery_handler.remove_tracker(sensor)
|
||||
|
||||
known_batteries = list(gateway.entities[DOMAIN])
|
||||
known_batteries = set(gateway.entities[DOMAIN])
|
||||
new_battery = DeconzBattery(sensor, gateway)
|
||||
if new_battery.unique_id not in known_batteries:
|
||||
entities.append(new_battery)
|
||||
|
||||
Reference in new issue
Block a user