8000 Streamline setup of deCONZ scene platform by Kane610 · Pull Request #70700 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Streamline setup of deCONZ scene platform #70700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions homeassistant/components/deconz/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ def __init__(

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,
}

Expand Down
31 changes: 9 additions & 22 deletions homeassistant/components/deconz/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

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
Expand All @@ -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):
Expand Down
16 changes: 10 additions & 6 deletions tests/components/deconz/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

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,
Expand Down Expand Up @@ -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": {
Expand All @@ -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
0