From 8550f1be7ff788f00bba332e1d7e5e9311c77e81 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 14 Jun 2022 09:24:46 -0700 Subject: [PATCH] Create default better entity names --- .../matter_experimental/binary_sensor.py | 6 ----- .../matter_experimental/entity.py | 16 ++++++++++++ .../matter_experimental/light.py | 1 - .../matter_experimental/sensor.py | 6 ----- .../matter_experimental/switch.py | 6 ----- matter_server/client/matter.py | 26 +++++++++++++++---- matter_server/client/model/driver.py | 10 ------- 7 files changed, 37 insertions(+), 34 deletions(-) diff --git a/custom_components/matter_experimental/binary_sensor.py b/custom_components/matter_experimental/binary_sensor.py index bc120b38..c067c274 100644 --- a/custom_components/matter_experimental/binary_sensor.py +++ b/custom_components/matter_experimental/binary_sensor.py @@ -13,7 +13,6 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from matter_server.client.model.device import MatterDevice from matter_server.vendor import device_types from matter_server.vendor.chip.clusters import Objects as clusters @@ -38,11 +37,6 @@ async def async_setup_entry( class MatterBinarySensor(MatterEntity, BinarySensorEntity): """Representation of a Matter binary sensor.""" - def __init__(self, device: MatterDevice, mapping: DeviceMapping) -> None: - """Initialize the sensor.""" - super().__init__(device, mapping) - self._attr_name = device.node.name or f"Matter Sensor {device.node.node_id}" - @callback def _update_from_device(self) -> None: """Update from device.""" diff --git a/custom_components/matter_experimental/entity.py b/custom_components/matter_experimental/entity.py index f4b6e30a..5f0345e8 100644 --- a/custom_components/matter_experimental/entity.py +++ b/custom_components/matter_experimental/entity.py @@ -22,6 +22,22 @@ def __init__(self, device: MatterDevice, mapping: DeviceMapping) -> None: self._device_mapping = mapping self._attr_unique_id = f"{device.node.unique_id}-{device.endpoint_id}-{device.device_type.device_type}" + device_type_name = device.device_type.__doc__[:-1] + name = device.node.name + if name: + name += f" {device_type_name}" + else: + name = f"{device_type_name} {device.node.node_id}" + + # If this device has multiple of this device type, add their endpoint. + if ( + sum(dev.device_type is device.device_type for dev in device.node.devices) + > 1 + ): + name += f" ({device.endpoint_id})" + + self._attr_name = name + @property def device_info(self) -> entity.DeviceInfo | None: """Return device info for device registry.""" diff --git a/custom_components/matter_experimental/light.py b/custom_components/matter_experimental/light.py index b04fbf2b..c45f6ce9 100644 --- a/custom_components/matter_experimental/light.py +++ b/custom_components/matter_experimental/light.py @@ -38,7 +38,6 @@ class MatterLight(MatterEntity, LightEntity): def __init__(self, device: MatterDevice, mapping: DeviceMapping) -> None: """Initialize the light.""" super().__init__(device, mapping) - self._attr_name = device.node.name or f"Matter Light {device.node.node_id}" if self._supports_brightness(): self._attr_supported_color_modes = [ColorMode.BRIGHTNESS] diff --git a/custom_components/matter_experimental/sensor.py b/custom_components/matter_experimental/sensor.py index e93faa26..1592b133 100644 --- a/custom_components/matter_experimental/sensor.py +++ b/custom_components/matter_experimental/sensor.py @@ -23,7 +23,6 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from matter_server.client.model.device import MatterDevice from matter_server.vendor import device_types from matter_server.vendor.chip.clusters import Objects as clusters from matter_server.vendor.chip.clusters.Types import NullValue @@ -52,11 +51,6 @@ class MatterSensor(MatterEntity, SensorEntity): _attr_state_class = SensorStateClass.MEASUREMENT _device_mapping: SensorDeviceMapping - def __init__(self, device: MatterDevice, mapping: SensorDeviceMapping) -> None: - """Initialize the sensor.""" - super().__init__(device, mapping) - self._attr_name = device.node.name or f"Matter Sensor {device.node.node_id}" - @callback def _update_from_device(self) -> None: """Update from device.""" diff --git a/custom_components/matter_experimental/switch.py b/custom_components/matter_experimental/switch.py index 0539ff0f..ef4ef0a0 100644 --- a/custom_components/matter_experimental/switch.py +++ b/custom_components/matter_experimental/switch.py @@ -13,7 +13,6 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from matter_server.client.model.device import MatterDevice from matter_server.vendor import device_types from matter_server.vendor.chip.clusters import Objects as clusters @@ -38,11 +37,6 @@ async def async_setup_entry( class MatterSwitch(MatterEntity, SwitchEntity): """Representation of a Matter switch.""" - def __init__(self, device: MatterDevice, mapping: DeviceMapping) -> None: - """Initialize the switch.""" - super().__init__(device, mapping) - self._attr_name = device.node.name or f"Matter Switch {device.node.node_id}" - async def async_turn_on(self, **kwargs: Any) -> None: """Turn switch on.""" await self._device.send_command( diff --git a/matter_server/client/matter.py b/matter_server/client/matter.py index 1c78e12b..88e3861e 100644 --- a/matter_server/client/matter.py +++ b/matter_server/client/matter.py @@ -57,7 +57,9 @@ async def connect(self): != self.client.server_info.compressedFabricId ): self.adapter.logger.warning( - "Connected to a server with a new fabric ID. Resetting data" + "Connected to a server with a new fabric ID (current: %s, server: %s). Resetting data", + data.get("compressed_fabric_id"), + self.client.server_info.compressedFabricId, ) data = None # TODO can we detect all known nodes to the server and interview them? @@ -123,7 +125,12 @@ async def _interview_node(self, node_id: int) -> None: self.adapter.delay_save_data(self._data_to_save) - await self.adapter.setup_node(self._nodes[node_id]) + try: + await self.adapter.setup_node(self._nodes[node_id]) + except Exception: # pylint: disable=broad-except + self.adapter.logger.exception( + "Unexptected error setting up node %s", node_id + ) def _schedule_interview_retry(self, nodes: set[int], timeout=INTERVIEW_RETRY_TIME): """Schedule a retry of failed nodes.""" @@ -184,10 +191,19 @@ async def _client_listen(self) -> None: async def _handle_driver_ready(self) -> None: """Handle driver ready.""" await self.driver_ready.wait() - tasks = [self.adapter.setup_node(node) for node in self.get_nodes()] + nodes = self.get_nodes() + tasks = [self.adapter.setup_node(node) for node in nodes] if tasks: - await asyncio.gather(*tasks) + results = await asyncio.gather(*tasks, return_exceptions=True) + + for node, result in zip(nodes, results): + if isinstance(result, Exception): + self.adapter.logger.error( + "Unexpected error setting up node %s", + node.node_id, + exc_info=result, + ) to_interview = { node_id for node_id, info in self._nodes.items() if info is None @@ -200,7 +216,7 @@ async def _handle_driver_ready(self) -> None: def _data_to_save(self) -> dict: return { - "compressed_fabric_id": self.client.driver.compressed_fabric_id, + "compressed_fabric_id": self.client.server_info.compressedFabricId, "next_node_id": self.next_node_id, "nodes": { node_id: node.raw_data if node else None diff --git a/matter_server/client/model/driver.py b/matter_server/client/model/driver.py index ef8cb7c7..df5be9c9 100644 --- a/matter_server/client/model/driver.py +++ b/matter_server/client/model/driver.py @@ -17,13 +17,3 @@ def __init__(self, client: client.Client, server_info: ServerInformation): self._client = client self.device_controller = DeviceController(client) self.read_subscriptions = ReadSubscriptions(client) - - @property - def fabric_id(self) -> int: - """Fabric ID.""" - return self.server_info.fabricId - - @property - def compressed_fabric_id(self) -> int: - """Compressed fabric ID.""" - return self.server_info.compressedFabricId