deCONZ fix comments from #40265 (#40640)

* 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...
pull/40663/head
Robert Svensson 2020-09-27 11:02:45 +02:00 committed by GitHub
parent 8895752837
commit 66a8edb11e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 20 additions and 67 deletions

View File

@ -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):

View File

@ -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):

View File

@ -232,7 +232,7 @@ class DeconzGateway:
unsub_dispatcher()
self.listeners = []
await async_unload_events(self)
async_unload_events(self)
self.deconz_ids = {}
return True

View File

@ -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)

View File

@ -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)

View File

@ -67,7 +67,6 @@ async def test_no_binary_sensors(hass):
"""Test that no sensors in deconz results in no sensor entities."""
gateway = await setup_deconz_integration(hass)
assert len(gateway.deconz_ids) == 0
assert len(gateway.entities[binary_sensor.DOMAIN]) == 0
assert len(hass.states.async_all()) == 0
@ -81,7 +80,6 @@ async def test_binary_sensors(hass):
assert "binary_sensor.clip_presence_sensor" not in gateway.deconz_ids
assert "binary_sensor.vibration_sensor" in gateway.deconz_ids
assert len(hass.states.async_all()) == 3
assert len(gateway.entities[binary_sensor.DOMAIN]) == 2
presence_sensor = hass.states.get("binary_sensor.presence_sensor")
assert presence_sensor.state == "off"
@ -113,7 +111,6 @@ async def test_binary_sensors(hass):
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[binary_sensor.DOMAIN]) == 0
async def test_allow_clip_sensor(hass):
@ -130,7 +127,6 @@ async def test_allow_clip_sensor(hass):
assert "binary_sensor.clip_presence_sensor" in gateway.deconz_ids
assert "binary_sensor.vibration_sensor" in gateway.deconz_ids
assert len(hass.states.async_all()) == 4
assert len(gateway.entities[binary_sensor.DOMAIN]) == 3
presence_sensor = hass.states.get("binary_sensor.presence_sensor")
assert presence_sensor.state == "off"
@ -154,7 +150,6 @@ async def test_allow_clip_sensor(hass):
assert "binary_sensor.clip_presence_sensor" not in gateway.deconz_ids
assert "binary_sensor.vibration_sensor" in gateway.deconz_ids
assert len(hass.states.async_all()) == 3
assert len(gateway.entities[binary_sensor.DOMAIN]) == 2
hass.config_entries.async_update_entry(
gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}
@ -166,14 +161,12 @@ async def test_allow_clip_sensor(hass):
assert "binary_sensor.clip_presence_sensor" in gateway.deconz_ids
assert "binary_sensor.vibration_sensor" in gateway.deconz_ids
assert len(hass.states.async_all()) == 4
assert len(gateway.entities[binary_sensor.DOMAIN]) == 3
async def test_add_new_binary_sensor(hass):
"""Test that adding a new binary sensor works."""
gateway = await setup_deconz_integration(hass)
assert len(gateway.deconz_ids) == 0
assert len(gateway.entities[binary_sensor.DOMAIN]) == 0
state_added_event = {
"t": "event",
@ -189,4 +182,3 @@ async def test_add_new_binary_sensor(hass):
presence_sensor = hass.states.get("binary_sensor.presence_sensor")
assert presence_sensor.state == "off"
assert len(gateway.entities[binary_sensor.DOMAIN]) == 1

View File

@ -59,7 +59,6 @@ async def test_no_sensors(hass):
gateway = await setup_deconz_integration(hass)
assert len(gateway.deconz_ids) == 0
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[climate.DOMAIN]) == 0
async def test_climate_devices(hass):
@ -73,7 +72,6 @@ async def test_climate_devices(hass):
assert "climate.presence_sensor" not in gateway.deconz_ids
assert "climate.clip_thermostat" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 3
assert len(gateway.entities[climate.DOMAIN]) == 1
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "auto"
@ -183,7 +181,6 @@ async def test_climate_devices(hass):
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[climate.DOMAIN]) == 0
async def test_clip_climate_device(hass):
@ -201,7 +198,6 @@ async def test_clip_climate_device(hass):
assert "climate.presence_sensor" not in gateway.deconz_ids
assert "climate.clip_thermostat" in gateway.deconz_ids
assert len(hass.states.async_all()) == 4
assert len(gateway.entities[climate.DOMAIN]) == 2
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "auto"
@ -229,7 +225,6 @@ async def test_clip_climate_device(hass):
assert "climate.presence_sensor" not in gateway.deconz_ids
assert "climate.clip_thermostat" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 3
assert len(gateway.entities[climate.DOMAIN]) == 1
hass.config_entries.async_update_entry(
gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}
@ -242,7 +237,6 @@ async def test_clip_climate_device(hass):
assert "climate.presence_sensor" not in gateway.deconz_ids
assert "climate.clip_thermostat" in gateway.deconz_ids
assert len(hass.states.async_all()) == 4
assert len(gateway.entities[climate.DOMAIN]) == 2
async def test_verify_state_update(hass):
@ -274,7 +268,6 @@ async def test_add_new_climate_device(hass):
"""Test that adding a new climate device works."""
gateway = await setup_deconz_integration(hass)
assert len(gateway.deconz_ids) == 0
assert len(gateway.entities[climate.DOMAIN]) == 0
state_added_event = {
"t": "event",
@ -290,4 +283,3 @@ async def test_add_new_climate_device(hass):
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "auto"
assert len(gateway.entities[climate.DOMAIN]) == 1

View File

@ -67,7 +67,6 @@ async def test_no_covers(hass):
"""Test that no cover entities are created."""
gateway = await setup_deconz_integration(hass)
assert len(gateway.deconz_ids) == 0
assert len(gateway.entities[cover.DOMAIN]) == 0
assert len(hass.states.async_all()) == 0
@ -82,7 +81,6 @@ async def test_cover(hass):
assert "cover.deconz_old_brightness_cover" in gateway.deconz_ids
assert "cover.window_covering_controller" in gateway.deconz_ids
assert len(hass.states.async_all()) == 5
assert len(gateway.entities[cover.DOMAIN]) == 4
level_controllable_cover = hass.states.get("cover.level_controllable_cover")
assert level_controllable_cover.state == "open"
@ -160,4 +158,3 @@ async def test_cover(hass):
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[cover.DOMAIN]) == 0

View File

@ -1,7 +1,7 @@
"""Test deCONZ remote events."""
from copy import deepcopy
from homeassistant.components.deconz.deconz_event import CONF_DECONZ_EVENT, EVENT
from homeassistant.components.deconz.deconz_event import CONF_DECONZ_EVENT
from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration
@ -62,7 +62,6 @@ async def test_deconz_events(hass):
assert "sensor.switch_2_battery_level" in gateway.deconz_ids
assert len(hass.states.async_all()) == 3
assert len(gateway.events) == 5
assert len(gateway.entities[EVENT]) == 5
switch_1 = hass.states.get("sensor.switch_1")
assert switch_1 is None
@ -128,4 +127,3 @@ async def test_deconz_events(hass):
assert len(hass.states.async_all()) == 0
assert len(gateway.events) == 0
assert len(gateway.entities[EVENT]) == 0

View File

@ -95,7 +95,6 @@ async def test_no_lights_or_groups(hass):
gateway = await setup_deconz_integration(hass)
assert len(gateway.deconz_ids) == 0
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[light.DOMAIN]) == 0
async def test_lights_and_groups(hass):
@ -112,7 +111,6 @@ async def test_lights_and_groups(hass):
assert "light.on_off_light" in gateway.deconz_ids
assert len(hass.states.async_all()) == 6
assert len(gateway.entities[light.DOMAIN]) == 5
rgb_light = hass.states.get("light.rgb_light")
assert rgb_light.state == "on"
@ -258,7 +256,6 @@ async def test_lights_and_groups(hass):
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[light.DOMAIN]) == 0
async def test_disable_light_groups(hass):
@ -278,7 +275,6 @@ async def test_disable_light_groups(hass):
assert "light.on_off_switch" not in gateway.deconz_ids
# 3 entities
assert len(hass.states.async_all()) == 5
assert len(gateway.entities[light.DOMAIN]) == 4
rgb_light = hass.states.get("light.rgb_light")
assert rgb_light is not None
@ -304,7 +300,6 @@ async def test_disable_light_groups(hass):
assert "light.on_off_switch" not in gateway.deconz_ids
# 3 entities
assert len(hass.states.async_all()) == 6
assert len(gateway.entities[light.DOMAIN]) == 5
hass.config_entries.async_update_entry(
gateway.config_entry, options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: False}
@ -318,4 +313,3 @@ async def test_disable_light_groups(hass):
assert "light.on_off_switch" not in gateway.deconz_ids
# 3 entities
assert len(hass.states.async_all()) == 5
assert len(gateway.entities[light.DOMAIN]) == 4

View File

@ -2,7 +2,6 @@
from copy import deepcopy
from homeassistant.components import deconz
from homeassistant.components.deconz.deconz_event import EVENT
import homeassistant.components.sensor as sensor
from homeassistant.const import (
DEVICE_CLASS_BATTERY,
@ -97,7 +96,6 @@ async def test_no_sensors(hass):
gateway = await setup_deconz_integration(hass)
assert len(gateway.deconz_ids) == 0
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[sensor.DOMAIN]) == 0
async def test_sensors(hass):
@ -116,7 +114,6 @@ async def test_sensors(hass):
assert "sensor.consumption_sensor" in gateway.deconz_ids
assert "sensor.clip_light_level_sensor" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 5
assert len(gateway.entities[sensor.DOMAIN]) == 5
light_level_sensor = hass.states.get("sensor.light_level_sensor")
assert light_level_sensor.state == "999.8"
@ -177,8 +174,6 @@ async def test_sensors(hass):
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
# Daylight sensor from deCONZ is added to set but is disabled by default
assert len(gateway.entities[sensor.DOMAIN]) == 1
async def test_allow_clip_sensors(hass):
@ -201,7 +196,6 @@ async def test_allow_clip_sensors(hass):
assert "sensor.consumption_sensor" in gateway.deconz_ids
assert "sensor.clip_light_level_sensor" in gateway.deconz_ids
assert len(hass.states.async_all()) == 6
assert len(gateway.entities[sensor.DOMAIN]) == 6
light_level_sensor = hass.states.get("sensor.light_level_sensor")
assert light_level_sensor.state == "999.8"
@ -249,7 +243,6 @@ async def test_allow_clip_sensors(hass):
assert "sensor.consumption_sensor" in gateway.deconz_ids
assert "sensor.clip_light_level_sensor" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 5
assert len(gateway.entities[sensor.DOMAIN]) == 5
hass.config_entries.async_update_entry(
gateway.config_entry, options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True}
@ -267,7 +260,6 @@ async def test_allow_clip_sensors(hass):
assert "sensor.consumption_sensor" in gateway.deconz_ids
assert "sensor.clip_light_level_sensor" in gateway.deconz_ids
assert len(hass.states.async_all()) == 6
assert len(gateway.entities[sensor.DOMAIN]) == 6
async def test_add_new_sensor(hass):
@ -300,8 +292,6 @@ async def test_add_battery_later(hass):
assert len(gateway.deconz_ids) == 0
assert len(gateway.events) == 1
assert len(remote._callbacks) == 2
assert len(gateway.entities[sensor.DOMAIN]) == 0
assert len(gateway.entities[EVENT]) == 1
remote.update({"config": {"battery": 50}})
await hass.async_block_till_done()
@ -312,5 +302,3 @@ async def test_add_battery_later(hass):
battery_sensor = hass.states.get("sensor.switch_1_battery_level")
assert battery_sensor is not None
assert len(gateway.entities[sensor.DOMAIN]) == 1
assert len(gateway.entities[EVENT]) == 1

View File

@ -64,7 +64,6 @@ async def test_no_switches(hass):
gateway = await setup_deconz_integration(hass)
assert len(gateway.deconz_ids) == 0
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[switch.DOMAIN]) == 0
async def test_switches(hass):
@ -78,7 +77,6 @@ async def test_switches(hass):
assert "switch.unsupported_switch" not in gateway.deconz_ids
assert "switch.on_off_relay" in gateway.deconz_ids
assert len(hass.states.async_all()) == 5
assert len(gateway.entities[switch.DOMAIN]) == 4
on_off_switch = hass.states.get("switch.on_off_switch")
assert on_off_switch.state == "on"
@ -175,4 +173,3 @@ async def test_switches(hass):
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
assert len(gateway.entities[switch.DOMAIN]) == 0