-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scene support to WMS WebControl pro
- Loading branch information
Showing
6 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Support for scenes provided by WMS WebControl pro.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from wmspro.scene import Scene as WMS_Scene | ||
|
||
from homeassistant.components.scene import Scene | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.device_registry import DeviceInfo | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import WebControlProConfigEntry | ||
from .const import ATTRIBUTION, DOMAIN, MANUFACTURER | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: WebControlProConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up the WMS based scenes from a config entry.""" | ||
hub = config_entry.runtime_data | ||
|
||
entities: list[WebControlProScene] = [ | ||
WebControlProScene(config_entry.entry_id, scene) | ||
for scene in hub.scenes.values() | ||
] | ||
|
||
async_add_entities(entities) | ||
|
||
|
||
class WebControlProScene(Scene): | ||
"""Representation of a WMS based scene.""" | ||
|
||
_attr_attribution = ATTRIBUTION | ||
_attr_has_entity_name = True | ||
_attr_name = None | ||
|
||
def __init__(self, config_entry_id: str, scene: WMS_Scene) -> None: | ||
"""Initialize the entity with the configured scene.""" | ||
super().__init__() | ||
scene_id_str = str(scene.id) | ||
self._scene = scene | ||
self._attr_unique_id = scene_id_str | ||
self._attr_device_info = DeviceInfo( | ||
identifiers={(DOMAIN, scene_id_str)}, | ||
manufacturer=MANUFACTURER, | ||
model="Scene", | ||
name=scene.name, | ||
serial_number=scene_id_str, | ||
suggested_area=scene.room.name, | ||
via_device=(DOMAIN, config_entry_id), | ||
configuration_url=f"http://{scene.host}/control", | ||
) | ||
|
||
async def async_activate(self, **kwargs: Any) -> None: | ||
"""Activate scene. Try to get entities into requested state.""" | ||
await self._scene() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# serializer version: 1 | ||
# name: test_scene_activate | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'attribution': 'Data provided by WMS WebControl pro API', | ||
'friendly_name': 'Gute Nacht', | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'scene.gute_nacht', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'unknown', | ||
}) | ||
# --- | ||
# name: test_scene_device | ||
DeviceRegistryEntrySnapshot({ | ||
'area_id': 'raum_0', | ||
'config_entries': <ANY>, | ||
'configuration_url': 'http://webcontrol/control', | ||
'connections': set({ | ||
}), | ||
'disabled_by': None, | ||
'entry_type': None, | ||
'hw_version': None, | ||
'id': <ANY>, | ||
'identifiers': set({ | ||
tuple( | ||
'wmspro', | ||
'688966', | ||
), | ||
}), | ||
'is_new': False, | ||
'labels': set({ | ||
}), | ||
'manufacturer': 'WAREMA Renkhoff SE', | ||
'model': 'Scene', | ||
'model_id': None, | ||
'name': 'Gute Nacht', | ||
'name_by_user': None, | ||
'primary_config_entry': <ANY>, | ||
'serial_number': '688966', | ||
'suggested_area': 'Raum 0', | ||
'sw_version': None, | ||
'via_device_id': <ANY>, | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Test the wmspro scene support.""" | ||
|
||
from unittest.mock import AsyncMock | ||
|
||
from syrupy import SnapshotAssertion | ||
|
||
from homeassistant.components.wmspro.const import DOMAIN | ||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import device_registry as dr | ||
from homeassistant.setup import async_setup_component | ||
|
||
from . import setup_config_entry | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
async def test_scene_device( | ||
hass: HomeAssistant, | ||
mock_config_entry: MockConfigEntry, | ||
mock_hub_ping: AsyncMock, | ||
mock_hub_configuration_test: AsyncMock, | ||
mock_dest_refresh: AsyncMock, | ||
device_registry: dr.DeviceRegistry, | ||
snapshot: SnapshotAssertion, | ||
) -> None: | ||
"""Test that a scene device is created correctly.""" | ||
assert await setup_config_entry(hass, mock_config_entry) | ||
assert len(mock_hub_ping.mock_calls) == 1 | ||
assert len(mock_hub_configuration_test.mock_calls) == 1 | ||
|
||
device_entry = device_registry.async_get_device(identifiers={(DOMAIN, "688966")}) | ||
assert device_entry is not None | ||
assert device_entry == snapshot | ||
|
||
|
||
async def test_scene_activate( | ||
hass: HomeAssistant, | ||
mock_config_entry: MockConfigEntry, | ||
mock_hub_ping: AsyncMock, | ||
mock_hub_configuration_test: AsyncMock, | ||
mock_dest_refresh: AsyncMock, | ||
mock_scene_call: AsyncMock, | ||
snapshot: SnapshotAssertion, | ||
) -> None: | ||
"""Test that a scene entity is created and activated correctly.""" | ||
assert await setup_config_entry(hass, mock_config_entry) | ||
assert len(mock_hub_ping.mock_calls) == 1 | ||
assert len(mock_hub_configuration_test.mock_calls) == 1 | ||
|
||
entity = hass.states.get("scene.gute_nacht") | ||
assert entity is not None | ||
assert entity == snapshot | ||
|
||
await async_setup_component(hass, "homeassistant", {}) | ||
await hass.services.async_call( | ||
"homeassistant", | ||
SERVICE_TURN_ON, | ||
{ATTR_ENTITY_ID: entity.entity_id}, | ||
blocking=True, | ||
) | ||
|
||
assert len(mock_scene_call.mock_calls) == 1 |