2022-12-01 19:44:56 +00:00
|
|
|
"""Provide common test tools."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from functools import cache
|
|
|
|
import json
|
2022-12-06 17:38:13 +00:00
|
|
|
from typing import Any
|
|
|
|
from unittest.mock import MagicMock
|
2022-12-01 19:44:56 +00:00
|
|
|
|
2022-12-06 17:38:13 +00:00
|
|
|
from matter_server.common.helpers.util import dataclass_from_dict
|
|
|
|
from matter_server.common.models.events import EventType
|
2022-12-01 19:44:56 +00:00
|
|
|
from matter_server.common.models.node import MatterNode
|
|
|
|
|
2022-12-06 17:38:13 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-12-01 19:44:56 +00:00
|
|
|
|
2022-12-06 17:38:13 +00:00
|
|
|
from tests.common import MockConfigEntry, load_fixture
|
2022-12-01 19:44:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cache
|
|
|
|
def load_node_fixture(fixture: str) -> str:
|
|
|
|
"""Load a fixture."""
|
|
|
|
return load_fixture(f"matter/nodes/{fixture}.json")
|
|
|
|
|
|
|
|
|
|
|
|
def load_and_parse_node_fixture(fixture: str) -> dict[str, Any]:
|
|
|
|
"""Load and parse a node fixture."""
|
|
|
|
return json.loads(load_node_fixture(fixture))
|
|
|
|
|
|
|
|
|
|
|
|
async def setup_integration_with_node_fixture(
|
2022-12-06 17:38:13 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
node_fixture: str,
|
|
|
|
client: MagicMock,
|
2022-12-01 19:44:56 +00:00
|
|
|
) -> MatterNode:
|
|
|
|
"""Set up Matter integration with fixture as node."""
|
|
|
|
node_data = load_and_parse_node_fixture(node_fixture)
|
2022-12-06 17:38:13 +00:00
|
|
|
node = dataclass_from_dict(
|
|
|
|
MatterNode,
|
2022-12-01 19:44:56 +00:00
|
|
|
node_data,
|
|
|
|
)
|
2022-12-06 17:38:13 +00:00
|
|
|
client.get_nodes.return_value = [node]
|
|
|
|
client.get_node.return_value = node
|
2022-12-01 19:44:56 +00:00
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain="matter", data={"url": "http://mock-matter-server-url"}
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
2022-12-06 17:38:13 +00:00
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2022-12-01 19:44:56 +00:00
|
|
|
|
|
|
|
return node
|
2022-12-06 17:38:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_node_attribute(
|
|
|
|
node: MatterNode,
|
|
|
|
endpoint: int,
|
|
|
|
cluster_id: int,
|
|
|
|
attribute_id: int,
|
|
|
|
value: Any,
|
|
|
|
) -> None:
|
|
|
|
"""Set a node attribute."""
|
|
|
|
attribute = node.attributes[f"{endpoint}/{cluster_id}/{attribute_id}"]
|
|
|
|
attribute.value = value
|
|
|
|
|
|
|
|
|
|
|
|
async def trigger_subscription_callback(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
client: MagicMock,
|
|
|
|
event: EventType = EventType.ATTRIBUTE_UPDATED,
|
|
|
|
data: Any = None,
|
|
|
|
) -> None:
|
|
|
|
"""Trigger a subscription callback."""
|
|
|
|
callback = client.subscribe.call_args[0][0]
|
|
|
|
callback(event, data)
|
|
|
|
await hass.async_block_till_done()
|