Bump python-matter-server to 3.0.0 (#88607)

* Bump python-matter-server to 3.0.0

Include all fixes for the changed api schema of the library

* fix test fixtures

* remove invalid data from fixtures

* fix some of the tests

* fix binary sensor bug

* fix sensor bug

* fix switch test

* fix tests

* adjust bugs and typos
This commit is contained in:
Marcel van der Veldt 2023-02-22 20:24:30 +01:00 committed by GitHub
parent 9c7adfc4b2
commit 881e85d74f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 3687 additions and 31021 deletions

View file

@ -7,10 +7,11 @@ from dataclasses import dataclass
import logging
from typing import TYPE_CHECKING, Any, cast
from matter_server.common.models.device_type_instance import MatterDeviceTypeInstance
from matter_server.common.models.events import EventType
from matter_server.common.models.node_device import AbstractMatterNodeDevice
from matter_server.common.models.server_information import ServerInfo
from chip.clusters.Objects import ClusterAttributeDescriptor
from matter_server.client.models.device_type_instance import MatterDeviceTypeInstance
from matter_server.client.models.node_device import AbstractMatterNodeDevice
from matter_server.common.helpers.util import create_attribute_path
from matter_server.common.models import EventType, ServerInfoMessage
from homeassistant.core import callback
from homeassistant.helpers.entity import DeviceInfo, Entity, EntityDescription
@ -20,7 +21,6 @@ from .helpers import get_device_id, get_operational_instance_id
if TYPE_CHECKING:
from matter_server.client import MatterClient
from matter_server.common.models.node import MatterAttribute
LOGGER = logging.getLogger(__name__)
@ -59,19 +59,20 @@ class MatterEntity(Entity):
self.entity_description = entity_description
self._unsubscribes: list[Callable] = []
# for fast lookups we create a mapping to the attribute paths
# The server info is set when the client connects to the server.
self._attributes_map: dict[type, str] = {}
server_info = cast(ServerInfo, self.matter_client.server_info)
# The server info is set when the client connects to the server.
server_info = cast(ServerInfoMessage, self.matter_client.server_info)
# create unique_id based on "Operational Instance Name" and endpoint/device type
self._attr_unique_id = (
f"{get_operational_instance_id(server_info, self._node_device.node())}-"
f"{device_type_instance.endpoint}-"
f"{device_type_instance.endpoint.endpoint_id}-"
f"{device_type_instance.device_type.device_type}"
)
node_device_id = get_device_id(server_info, node_device)
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, f"{ID_TYPE_DEVICE_ID}_{node_device_id}")}
)
self._attr_available = self._node_device.node().available
async def async_added_to_hass(self) -> None:
"""Handle being added to Home Assistant."""
@ -79,19 +80,24 @@ class MatterEntity(Entity):
# Subscribe to attribute updates.
for attr_cls in self.entity_description.subscribe_attributes:
if matter_attr := self.get_matter_attribute(attr_cls):
self._attributes_map[attr_cls] = matter_attr.path
self._unsubscribes.append(
self.matter_client.subscribe(
self._on_matter_event,
EventType.ATTRIBUTE_UPDATED,
self._device_type_instance.node.node_id,
matter_attr.path,
)
attr_path = self.get_matter_attribute_path(attr_cls)
self._attributes_map[attr_cls] = attr_path
self._unsubscribes.append(
self.matter_client.subscribe(
callback=self._on_matter_event,
event_filter=EventType.ATTRIBUTE_UPDATED,
node_filter=self._device_type_instance.node.node_id,
attr_path_filter=attr_path,
)
continue
# not sure if this can happen, but just in case log it.
LOGGER.warning("Attribute not found on device: %s", attr_cls)
)
# subscribe to node (availability changes)
self._unsubscribes.append(
self.matter_client.subscribe(
callback=self._on_matter_event,
event_filter=EventType.NODE_UPDATED,
node_filter=self._device_type_instance.node.node_id,
)
)
# make sure to update the attributes once
self._update_from_device()
@ -104,6 +110,7 @@ class MatterEntity(Entity):
@callback
def _on_matter_event(self, event: EventType, data: Any = None) -> None:
"""Call on update."""
self._attr_available = self._device_type_instance.node.available
self._update_from_device()
self.async_write_ha_state()
@ -113,13 +120,18 @@ class MatterEntity(Entity):
"""Update data from Matter device."""
@callback
def get_matter_attribute(self, attribute: type) -> MatterAttribute | None:
"""Lookup MatterAttribute on device by providing the attribute class."""
return next(
(
x
for x in self._device_type_instance.attributes
if x.attribute_type == attribute
),
None,
def get_matter_attribute_value(
self, attribute: type[ClusterAttributeDescriptor]
) -> Any:
"""Get current value for given attribute."""
return self._device_type_instance.get_attribute_value(None, attribute)
@callback
def get_matter_attribute_path(
self, attribute: type[ClusterAttributeDescriptor]
) -> str:
"""Return AttributePath by providing the endpoint and Attribute class."""
endpoint = self._device_type_instance.endpoint.endpoint_id
return create_attribute_path(
endpoint, attribute.cluster_id, attribute.attribute_id
)