Clean up matter adapter (#84144)

This commit is contained in:
Martin Hjelmare 2022-12-17 23:44:19 +01:00 committed by GitHub
parent d6158c0fcc
commit f29024795b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 26 deletions

View file

@ -728,7 +728,6 @@ omit =
homeassistant/components/mastodon/notify.py homeassistant/components/mastodon/notify.py
homeassistant/components/matrix/* homeassistant/components/matrix/*
homeassistant/components/matter/__init__.py homeassistant/components/matter/__init__.py
homeassistant/components/matter/adapter.py
homeassistant/components/matter/entity.py homeassistant/components/matter/entity.py
homeassistant/components/meater/__init__.py homeassistant/components/meater/__init__.py
homeassistant/components/meater/const.py homeassistant/components/meater/const.py

View file

@ -1,8 +1,6 @@
"""Matter to Home Assistant adapter.""" """Matter to Home Assistant adapter."""
from __future__ import annotations from __future__ import annotations
import asyncio
import logging
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from chip.clusters import Objects as all_clusters from chip.clusters import Objects as all_clusters
@ -14,7 +12,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from .const import DOMAIN, LOGGER
from .device_platform import DEVICE_PLATFORM from .device_platform import DEVICE_PLATFORM
if TYPE_CHECKING: if TYPE_CHECKING:
@ -35,27 +33,22 @@ class MatterAdapter:
self.matter_client = matter_client self.matter_client = matter_client
self.hass = hass self.hass = hass
self.config_entry = config_entry self.config_entry = config_entry
self.logger = logging.getLogger(__name__)
self.platform_handlers: dict[Platform, AddEntitiesCallback] = {} self.platform_handlers: dict[Platform, AddEntitiesCallback] = {}
self._platforms_set_up = asyncio.Event()
def register_platform_handler( def register_platform_handler(
self, platform: Platform, add_entities: AddEntitiesCallback self, platform: Platform, add_entities: AddEntitiesCallback
) -> None: ) -> None:
"""Register a platform handler.""" """Register a platform handler."""
self.platform_handlers[platform] = add_entities self.platform_handlers[platform] = add_entities
if len(self.platform_handlers) == len(DEVICE_PLATFORM):
self._platforms_set_up.set()
async def setup_nodes(self) -> None: async def setup_nodes(self) -> None:
"""Set up all existing nodes.""" """Set up all existing nodes."""
await self._platforms_set_up.wait()
for node in await self.matter_client.get_nodes(): for node in await self.matter_client.get_nodes():
await self._setup_node(node) self._setup_node(node)
async def _setup_node(self, node: MatterNode) -> None: def _setup_node(self, node: MatterNode) -> None:
"""Set up an node.""" """Set up an node."""
self.logger.debug("Setting up entities for node %s", node.node_id) LOGGER.debug("Setting up entities for node %s", node.node_id)
bridge_unique_id: str | None = None bridge_unique_id: str | None = None
@ -115,7 +108,7 @@ class MatterAdapter:
entities = [] entities = []
for entity_description in entity_descriptions: for entity_description in entity_descriptions:
self.logger.debug( LOGGER.debug(
"Creating %s entity for %s (%s)", "Creating %s entity for %s (%s)",
platform, platform,
instance.device_type.__name__, instance.device_type.__name__,
@ -134,7 +127,7 @@ class MatterAdapter:
created = True created = True
if not created: if not created:
self.logger.warning( LOGGER.warning(
"Found unsupported device %s (%s)", "Found unsupported device %s (%s)",
type(instance).__name__, type(instance).__name__,
hex(instance.device_type.device_type), hex(instance.device_type.device_type),

View file

@ -1,7 +1,7 @@
"""Test the adapter.""" """Test the adapter."""
from __future__ import annotations from __future__ import annotations
from typing import Any from unittest.mock import MagicMock
import pytest import pytest
@ -11,36 +11,40 @@ from homeassistant.helpers import device_registry as dr
from .common import setup_integration_with_node_fixture from .common import setup_integration_with_node_fixture
# TEMP: Tests need to be fixed
pytestmark = pytest.mark.skip("all tests still WIP")
async def test_device_registry_single_node_device( async def test_device_registry_single_node_device(
hass: HomeAssistant, hass_storage: dict[str, Any] hass: HomeAssistant,
matter_client: MagicMock,
) -> None: ) -> None:
"""Test bridge devices are set up correctly with via_device.""" """Test bridge devices are set up correctly with via_device."""
await setup_integration_with_node_fixture( await setup_integration_with_node_fixture(
hass, hass_storage, "lighting-example-app" hass,
"onoff-light",
matter_client,
) )
dev_reg = dr.async_get(hass) dev_reg = dr.async_get(hass)
entry = dev_reg.async_get_device({(DOMAIN, "BE8F70AA40DDAE41")}) entry = dev_reg.async_get_device({(DOMAIN, "mock-onoff-light")})
assert entry is not None assert entry is not None
assert entry.name == "My Cool Light" assert entry.name == "Mock OnOff Light"
assert entry.manufacturer == "Nabu Casa" assert entry.manufacturer == "Nabu Casa"
assert entry.model == "M5STAMP Lighting App" assert entry.model == "Mock Light"
assert entry.hw_version == "v1.0" assert entry.hw_version == "v1.0"
assert entry.sw_version == "55ab764bea" assert entry.sw_version == "v1.0"
@pytest.mark.skip("Waiting for a new test fixture")
async def test_device_registry_bridge( async def test_device_registry_bridge(
hass: HomeAssistant, hass_storage: dict[str, Any] hass: HomeAssistant,
matter_client: MagicMock,
) -> None: ) -> None:
"""Test bridge devices are set up correctly with via_device.""" """Test bridge devices are set up correctly with via_device."""
await setup_integration_with_node_fixture( await setup_integration_with_node_fixture(
hass, hass_storage, "fake-bridge-two-light" hass,
"fake-bridge-two-light",
matter_client,
) )
dev_reg = dr.async_get(hass) dev_reg = dr.async_get(hass)