From cb510bc5d9adbebaafbfe39b5bb10701d8c9c916 Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Mon, 1 Apr 2024 11:01:25 -0400 Subject: [PATCH] Add device left event (#26) --- tests/test_gateway.py | 2 +- zha/application/const.py | 1 + zha/application/gateway.py | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/test_gateway.py b/tests/test_gateway.py index fee532b9..f4031d1a 100644 --- a/tests/test_gateway.py +++ b/tests/test_gateway.py @@ -532,7 +532,7 @@ def test_gateway_raw_device_initialized( "raw_device_initialized", RawDeviceInitializedEvent( device_info=RawDeviceInitializedDeviceInfo( - ieee="00:0d:6f:00:0a:90:69:e7", + ieee=zigpy.types.EUI64.convert("00:0d:6f:00:0a:90:69:e7"), nwk=0xB79C, pairing_status="INTERVIEW_COMPLETE", model="FakeModel", diff --git a/zha/application/const.py b/zha/application/const.py index 1b79f151..61dce324 100644 --- a/zha/application/const.py +++ b/zha/application/const.py @@ -319,6 +319,7 @@ def description(self) -> str: ZHA_GW_MSG_DEVICE_FULL_INIT = "device_fully_initialized" ZHA_GW_MSG_DEVICE_INFO = "device_info" ZHA_GW_MSG_DEVICE_JOINED = "device_joined" +ZHA_GW_MSG_DEVICE_LEFT = "device_left" ZHA_GW_MSG_DEVICE_REMOVED = "device_removed" ZHA_GW_MSG_GROUP_ADDED = "group_added" ZHA_GW_MSG_GROUP_INFO = "group_info" diff --git a/zha/application/gateway.py b/zha/application/gateway.py index b6456c9a..9a049fb5 100644 --- a/zha/application/gateway.py +++ b/zha/application/gateway.py @@ -34,6 +34,7 @@ ZHA_GW_MSG, ZHA_GW_MSG_DEVICE_FULL_INIT, ZHA_GW_MSG_DEVICE_JOINED, + ZHA_GW_MSG_DEVICE_LEFT, ZHA_GW_MSG_DEVICE_REMOVED, ZHA_GW_MSG_GROUP_ADDED, ZHA_GW_MSG_GROUP_MEMBER_ADDED, @@ -98,6 +99,16 @@ class DeviceJoinedEvent: event: Final[str] = ZHA_GW_MSG_DEVICE_JOINED +@dataclass(kw_only=True, frozen=True) +class DeviceLeftEvent: + """Event to signal that a device has joined the network.""" + + ieee: EUI64 + nwk: int + event_type: Final[str] = ZHA_GW_MSG + event: Final[str] = ZHA_GW_MSG_DEVICE_LEFT + + @dataclass(kw_only=True, frozen=True) class RawDeviceInitializedDeviceInfo(DeviceJoinedDeviceInfo): """Information about a device that has been initialized without quirks loaded.""" @@ -371,7 +382,7 @@ def device_joined(self, device: zigpy.device.Device) -> None: ZHA_GW_MSG_DEVICE_JOINED, DeviceJoinedEvent( device_info=DeviceJoinedDeviceInfo( - ieee=str(device.ieee), + ieee=device.ieee, nwk=device.nwk, pairing_status=DevicePairingStatus.PAIRED.name, ) @@ -385,7 +396,7 @@ def raw_device_initialized(self, device: zigpy.device.Device) -> None: # pylint ZHA_GW_MSG_RAW_INIT, RawDeviceInitializedEvent( device_info=RawDeviceInitializedDeviceInfo( - ieee=str(device.ieee), + ieee=device.ieee, nwk=device.nwk, pairing_status=DevicePairingStatus.INTERVIEW_COMPLETE.name, model=device.model if device.model else UNKNOWN_MODEL, @@ -418,6 +429,9 @@ def device_left(self, device: zigpy.device.Device) -> None: if zha_device is not None: zha_device.on_network = False self.async_update_device(device, available=False) + self.emit( + ZHA_GW_MSG_DEVICE_LEFT, DeviceLeftEvent(ieee=device.ieee, nwk=device.nwk) + ) def group_member_removed( self, zigpy_group: zigpy.group.Group, endpoint: zigpy.endpoint.Endpoint