Skip to content

Commit

Permalink
Better names (#10)
Browse files Browse the repository at this point in the history
* Better names

* Add fabric ID to unique ID

* isort
  • Loading branch information
balloob authored Jun 15, 2022
1 parent 17051a6 commit 1049301
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
3 changes: 2 additions & 1 deletion custom_components/matter_experimental/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ async def setup_node(self, node: MatterNode) -> None:
if device.device_type is device_types.RootNode:
continue

name = device.device_type.__doc__[:-1]
name = f"{device.device_type.__doc__[:-1]} {node.node_id}"
break

dr.async_get(self.hass).async_get_or_create(
name=name,
Expand Down
41 changes: 23 additions & 18 deletions custom_components/matter_experimental/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import async_timeout
from homeassistant.core import callback
from homeassistant.helpers import entity
from homeassistant.helpers import device_registry, entity

from matter_server.client.model.device import MatterDevice

Expand All @@ -22,23 +22,7 @@ class MatterEntity(entity.Entity):
def __init__(self, device: MatterDevice, mapping: DeviceMapping) -> None:
self._device = device
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
self._attr_unique_id = f"{device.node.matter.client.server_info.compressedFabricId}-{device.node.unique_id}-{device.endpoint_id}-{device.device_type.device_type}"

@property
def device_info(self) -> entity.DeviceInfo | None:
Expand All @@ -49,6 +33,27 @@ async def async_added_to_hass(self) -> None:
"""Handle being added to Home Assistant."""
await super().async_added_to_hass()

device_name = (
device_registry.async_get(self.hass)
.async_get(self.registry_entry.device_id)
.name
)

device_type_name = self._device.device_type.__doc__[:-1]
name = f"{device_name} {device_type_name}"

# If this device has multiple of this device type, add their endpoint.
if (
sum(
dev.device_type is self._device.device_type
for dev in self._device.node.devices
)
> 1
):
name += f" ({self._device.endpoint_id})"

self._attr_name = name

if not self._device_mapping.subscribe_attributes:
self._update_from_device()
return
Expand Down

0 comments on commit 1049301

Please sign in to comment.