Skip to content

Commit

Permalink
Add device left event (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulcahey committed Apr 1, 2024
1 parent 4e98384 commit cb510bc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions zha/application/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 16 additions & 2 deletions zha/application/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cb510bc

Please sign in to comment.