deCONZ - move event handling (#40424)
* Working draft * Remove event references in sensor
This commit is contained in:
parent
6e8e4eedb5
commit
fb7fb0ea78
3 changed files with 48 additions and 17 deletions
|
@ -1,14 +1,54 @@
|
|||
"""Representation of a deCONZ remote."""
|
||||
from pydeconz.sensor import Switch
|
||||
|
||||
from homeassistant.const import CONF_EVENT, CONF_ID, CONF_UNIQUE_ID
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.util import slugify
|
||||
|
||||
from .const import CONF_ANGLE, CONF_GESTURE, CONF_XY, LOGGER
|
||||
from .const import CONF_ANGLE, CONF_GESTURE, CONF_XY, LOGGER, NEW_SENSOR
|
||||
from .deconz_device import DeconzBase
|
||||
|
||||
CONF_DECONZ_EVENT = "deconz_event"
|
||||
|
||||
|
||||
async def async_setup_events(gateway) -> None:
|
||||
"""Set up the deCONZ events."""
|
||||
|
||||
@callback
|
||||
def async_add_sensor(sensors, new=True):
|
||||
"""Create DeconzEvent."""
|
||||
for sensor in sensors:
|
||||
|
||||
if not gateway.option_allow_clip_sensor and sensor.type.startswith("CLIP"):
|
||||
continue
|
||||
|
||||
if not new or sensor.type not in Switch.ZHATYPE:
|
||||
continue
|
||||
|
||||
new_event = DeconzEvent(sensor, gateway)
|
||||
gateway.hass.async_create_task(new_event.async_update_device_registry())
|
||||
gateway.events.append(new_event)
|
||||
|
||||
gateway.listeners.append(
|
||||
async_dispatcher_connect(
|
||||
gateway.hass, gateway.async_signal_new_device(NEW_SENSOR), async_add_sensor
|
||||
)
|
||||
)
|
||||
|
||||
async_add_sensor(
|
||||
[gateway.api.sensors[key] for key in sorted(gateway.api.sensors, key=int)]
|
||||
)
|
||||
|
||||
|
||||
async def async_unload_events(gateway) -> None:
|
||||
"""Unload all deCONZ events."""
|
||||
for event in gateway.events:
|
||||
event.async_will_remove_from_hass()
|
||||
|
||||
gateway.events.clear()
|
||||
|
||||
|
||||
class DeconzEvent(DeconzBase):
|
||||
"""When you want signals instead of entities.
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ from .const import (
|
|||
NEW_SENSOR,
|
||||
SUPPORTED_PLATFORMS,
|
||||
)
|
||||
from .deconz_event import async_setup_events, async_unload_events
|
||||
from .errors import AuthenticationRequired, CannotConnect
|
||||
|
||||
|
||||
|
@ -112,6 +113,8 @@ class DeconzGateway:
|
|||
)
|
||||
)
|
||||
|
||||
self.hass.async_create_task(async_setup_events(self))
|
||||
|
||||
self.api.start()
|
||||
|
||||
self.config_entry.add_update_listener(self.async_config_entry_updated)
|
||||
|
@ -227,9 +230,7 @@ class DeconzGateway:
|
|||
unsub_dispatcher()
|
||||
self.listeners = []
|
||||
|
||||
for event in self.events:
|
||||
event.async_will_remove_from_hass()
|
||||
self.events.clear()
|
||||
await async_unload_events(self)
|
||||
|
||||
self.deconz_ids = {}
|
||||
return True
|
||||
|
|
|
@ -35,7 +35,6 @@ from homeassistant.helpers.dispatcher import (
|
|||
|
||||
from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR
|
||||
from .deconz_device import DeconzDevice
|
||||
from .deconz_event import DeconzEvent
|
||||
from .gateway import get_gateway_from_config_entry
|
||||
|
||||
ATTR_CURRENT = "current"
|
||||
|
@ -82,7 +81,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
def async_add_sensor(sensors, new=True):
|
||||
"""Add sensors from deCONZ.
|
||||
|
||||
Create DeconzEvent if part of ZHAType list.
|
||||
Create DeconzSensor if not a ZHAType and not a binary sensor.
|
||||
Create DeconzBattery if sensor has a battery attribute.
|
||||
If new is false it means an existing sensor has got a battery state reported.
|
||||
|
@ -91,19 +89,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
|
||||
for sensor in sensors:
|
||||
|
||||
if new and sensor.type in Switch.ZHATYPE:
|
||||
|
||||
if gateway.option_allow_clip_sensor or not sensor.type.startswith(
|
||||
"CLIP"
|
||||
):
|
||||
new_event = DeconzEvent(sensor, gateway)
|
||||
hass.async_create_task(new_event.async_update_device_registry())
|
||||
gateway.events.append(new_event)
|
||||
|
||||
elif (
|
||||
if (
|
||||
new
|
||||
and sensor.BINARY is False
|
||||
and sensor.type not in Battery.ZHATYPE + Thermostat.ZHATYPE
|
||||
and sensor.type
|
||||
not in Battery.ZHATYPE + Switch.ZHATYPE + Thermostat.ZHATYPE
|
||||
and (
|
||||
gateway.option_allow_clip_sensor
|
||||
or not sensor.type.startswith("CLIP")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue