Skip to content

Commit

Permalink
Ensure device endpoints sync with register_endpoints
Browse files Browse the repository at this point in the history
This way if we need a reference to one we can grab it in a way we expect
  • Loading branch information
konistehrad committed Dec 9, 2023
1 parent 1ac01ed commit fa401f3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
10 changes: 9 additions & 1 deletion bellows/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import statistics
import sys
import typing

if sys.version_info[:2] < (3, 11):
from async_timeout import timeout as asyncio_timeout # pragma: no cover
Expand Down Expand Up @@ -75,6 +76,7 @@ class ControllerApplication(zigpy.application.ControllerApplication):
def __init__(self, config: dict):
super().__init__(config)
self._ctrl_event = asyncio.Event()
self._created_device_endpoints: typing.List[zdo_t.SimpleDescriptor] = []
self._ezsp = None
self._multicast = None
self._mfg_id_task: asyncio.Task | None = None
Expand Down Expand Up @@ -111,9 +113,12 @@ async def add_endpoint(self, descriptor: zdo_t.SimpleDescriptor) -> None:
descriptor.input_clusters,
descriptor.output_clusters,
)

if status != t.EmberStatus.SUCCESS:
raise StackAlreadyRunning()

self._created_device_endpoints.append(descriptor)

async def cleanup_tc_link_key(self, ieee: t.EmberEUI64) -> None:
"""Remove tc link_key for the given device."""
(index,) = await self._ezsp.findKeyTableEntry(ieee, True)
Expand Down Expand Up @@ -212,7 +217,10 @@ async def start_network(self):
self.devices[self.state.node_info.ieee] = ezsp_device

# The coordinator device does not respond to attribute reads
ezsp_device.endpoints[1] = EZSPEndpoint(ezsp_device, 1)
for zdo_desc in self._created_device_endpoints:
ep = EZSPEndpoint(ezsp_device, zdo_desc)
ezsp_device.endpoints[zdo_desc.endpoint] = ep

ezsp_device.model = ezsp_device.endpoints[1].model
ezsp_device.manufacturer = ezsp_device.endpoints[1].manufacturer
await ezsp_device.schedule_initialize()
Expand Down
32 changes: 32 additions & 0 deletions bellows/zigbee/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import zigpy.endpoint
import zigpy.util
import zigpy.zdo
import zigpy.zdo.types as zdo_t
import zigpy.profiles.zha
import zigpy.profiles.zll
import zigpy.profiles.zgp

import bellows.types as t

Expand All @@ -17,6 +21,10 @@


class EZSPEndpoint(zigpy.endpoint.Endpoint):
def __init__(self, device, descriptor: zdo_t.SimpleDescriptor) -> None:
self._desc = descriptor
super().__init__(device, descriptor.endpoint)

@property
def manufacturer(self) -> str:
"""Manufacturer."""
Expand All @@ -27,6 +35,30 @@ def model(self) -> str:
"""Model."""
return "EZSP"

async def initialize(self) -> None:
if self.profile_id is not None or self.status == zigpy.endpoint.Status.ENDPOINT_INACTIVE:
self.info("Endpoint already initialized")
else:
sd = self._desc
self.info("Reusing endpoint information: %s", sd)
self.profile_id = sd.profile
self.device_type = sd.device_type

if self.profile_id == zigpy.profiles.zha.PROFILE_ID:
self.device_type = zigpy.profiles.zha.DeviceType(self.device_type)
elif self.profile_id == zigpy.profiles.zll.PROFILE_ID:
self.device_type = zigpy.profiles.zll.DeviceType(self.device_type)
elif self.profile_id == zigpy.profiles.zgp.PROFILE_ID:
self.device_type = zigpy.profiles.zgp.DeviceType(self.device_type)

for cluster in sd.input_clusters:
self.add_input_cluster(cluster)

for cluster in sd.output_clusters:
self.add_output_cluster(cluster)

self.status = zigpy.endpoint.Status.ENDPOINT_INACTIVE

async def add_to_group(self, grp_id: int, name: str = None) -> t.EmberStatus:
if grp_id in self.member_of:
return t.EmberStatus.SUCCESS
Expand Down

0 comments on commit fa401f3

Please sign in to comment.