Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device left event #26

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Event to signal that a device has joined the network."""
"""Event to signal that a device has left 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
Loading