Streamline setup of deCONZ scene platform (#70700)

This commit is contained in:
Robert Svensson 2022-04-26 11:42:56 +02:00 committed by GitHub
parent 9dfc2636a9
commit f7d9797c62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 30 deletions

View File

@ -65,13 +65,11 @@ class DeconzGateway:
self.signal_new_group = f"deconz_new_group_{config_entry.entry_id}" self.signal_new_group = f"deconz_new_group_{config_entry.entry_id}"
self.signal_new_light = f"deconz_new_light_{config_entry.entry_id}" self.signal_new_light = f"deconz_new_light_{config_entry.entry_id}"
self.signal_new_scene = f"deconz_new_scene_{config_entry.entry_id}"
self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}" self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}"
self.deconz_resource_type_to_signal_new_device = { self.deconz_resource_type_to_signal_new_device = {
ResourceGroup.GROUP.value: self.signal_new_group, ResourceGroup.GROUP.value: self.signal_new_group,
ResourceGroup.LIGHT.value: self.signal_new_light, ResourceGroup.LIGHT.value: self.signal_new_light,
ResourceGroup.SCENE.value: self.signal_new_scene,
ResourceGroup.SENSOR.value: self.signal_new_sensor, ResourceGroup.SENSOR.value: self.signal_new_sensor,
} }

View File

@ -4,12 +4,11 @@ from __future__ import annotations
from typing import Any from typing import Any
from pydeconz.models.scene import Scene as PydeconzScene from pydeconz.models.event import EventType
from homeassistant.components.scene import DOMAIN, Scene from homeassistant.components.scene import DOMAIN, Scene
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .deconz_device import DeconzSceneMixin from .deconz_device import DeconzSceneMixin
@ -21,37 +20,25 @@ async def async_setup_entry(
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up scenes for deCONZ component.""" """Set up scenes for deCONZ integration."""
gateway = get_gateway_from_config_entry(hass, config_entry) gateway = get_gateway_from_config_entry(hass, config_entry)
gateway.entities[DOMAIN] = set() gateway.entities[DOMAIN] = set()
@callback @callback
def async_add_scene(scenes: list[PydeconzScene] | None = None) -> None: def async_add_scene(_: EventType, scene_id: str) -> None:
"""Add scene from deCONZ.""" """Add scene from deCONZ."""
entities = [] scene = gateway.api.scenes[scene_id]
async_add_entities([DeconzScene(scene, gateway)])
if scenes is None:
scenes = list(gateway.api.scenes.values())
for scene in scenes:
known_entities = set(gateway.entities[DOMAIN])
new_entity = DeconzScene(scene, gateway)
if new_entity.unique_id not in known_entities:
entities.append(new_entity)
if entities:
async_add_entities(entities)
config_entry.async_on_unload( config_entry.async_on_unload(
async_dispatcher_connect( gateway.api.scenes.subscribe(
hass,
gateway.signal_new_scene,
async_add_scene, async_add_scene,
EventType.ADDED,
) )
) )
async_add_scene() for scene_id in gateway.api.scenes:
async_add_scene(EventType.ADDED, scene_id)
class DeconzScene(DeconzSceneMixin, Scene): class DeconzScene(DeconzSceneMixin, Scene):

View File

@ -4,11 +4,9 @@ from unittest.mock import patch
import pytest import pytest
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN, SERVICE_TURN_ON from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN, SERVICE_TURN_ON
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE
from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_send
from .test_gateway import ( from .test_gateway import (
DECONZ_WEB_REQUEST, DECONZ_WEB_REQUEST,
@ -106,7 +104,7 @@ async def test_scenes(hass, aioclient_mock, raw_data, expected):
assert len(hass.states.async_all()) == 0 assert len(hass.states.async_all()) == 0
async def test_only_new_scenes_are_created(hass, aioclient_mock): async def test_only_new_scenes_are_created(hass, aioclient_mock, mock_deconz_websocket):
"""Test that scenes works.""" """Test that scenes works."""
data = { data = {
"groups": { "groups": {
@ -122,12 +120,18 @@ async def test_only_new_scenes_are_created(hass, aioclient_mock):
} }
} }
with patch.dict(DECONZ_WEB_REQUEST, data): with patch.dict(DECONZ_WEB_REQUEST, data):
config_entry = await setup_deconz_integration(hass, aioclient_mock) await setup_deconz_integration(hass, aioclient_mock)
assert len(hass.states.async_all()) == 2 assert len(hass.states.async_all()) == 2
gateway = get_gateway_from_config_entry(hass, config_entry) event_changed_group = {
async_dispatcher_send(hass, gateway.signal_new_scene) "t": "event",
"e": "changed",
"r": "groups",
"id": "1",
"scenes": [{"id": "1", "name": "Scene"}],
}
await mock_deconz_websocket(data=event_changed_group)
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(hass.states.async_all()) == 2 assert len(hass.states.async_all()) == 2