mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Streamline setup of deCONZ scene platform (#70700)
This commit is contained in:
parent
9dfc2636a9
commit
f7d9797c62
@ -65,13 +65,11 @@ class DeconzGateway:
|
||||
|
||||
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_scene = f"deconz_new_scene_{config_entry.entry_id}"
|
||||
self.signal_new_sensor = f"deconz_new_sensor_{config_entry.entry_id}"
|
||||
|
||||
self.deconz_resource_type_to_signal_new_device = {
|
||||
ResourceGroup.GROUP.value: self.signal_new_group,
|
||||
ResourceGroup.LIGHT.value: self.signal_new_light,
|
||||
ResourceGroup.SCENE.value: self.signal_new_scene,
|
||||
ResourceGroup.SENSOR.value: self.signal_new_sensor,
|
||||
}
|
||||
|
||||
|
@ -4,12 +4,11 @@ from __future__ import annotations
|
||||
|
||||
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.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .deconz_device import DeconzSceneMixin
|
||||
@ -21,37 +20,25 @@ async def async_setup_entry(
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up scenes for deCONZ component."""
|
||||
"""Set up scenes for deCONZ integration."""
|
||||
gateway = get_gateway_from_config_entry(hass, config_entry)
|
||||
gateway.entities[DOMAIN] = set()
|
||||
|
||||
@callback
|
||||
def async_add_scene(scenes: list[PydeconzScene] | None = None) -> None:
|
||||
def async_add_scene(_: EventType, scene_id: str) -> None:
|
||||
"""Add scene from deCONZ."""
|
||||
entities = []
|
||||
|
||||
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)
|
||||
scene = gateway.api.scenes[scene_id]
|
||||
async_add_entities([DeconzScene(scene, gateway)])
|
||||
|
||||
config_entry.async_on_unload(
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
gateway.signal_new_scene,
|
||||
gateway.api.scenes.subscribe(
|
||||
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):
|
||||
|
@ -4,11 +4,9 @@ from unittest.mock import patch
|
||||
|
||||
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.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
|
||||
from .test_gateway import (
|
||||
DECONZ_WEB_REQUEST,
|
||||
@ -106,7 +104,7 @@ async def test_scenes(hass, aioclient_mock, raw_data, expected):
|
||||
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."""
|
||||
data = {
|
||||
"groups": {
|
||||
@ -122,12 +120,18 @@ async def test_only_new_scenes_are_created(hass, aioclient_mock):
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
gateway = get_gateway_from_config_entry(hass, config_entry)
|
||||
async_dispatcher_send(hass, gateway.signal_new_scene)
|
||||
event_changed_group = {
|
||||
"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()
|
||||
|
||||
assert len(hass.states.async_all()) == 2
|
||||
|
Loading…
x
Reference in New Issue
Block a user