diff --git a/homeassistant/components/zha/core/channels/__init__.py b/homeassistant/components/zha/core/channels/__init__.py index dca80e9fcb3..1bd8a52b6e6 100644 --- a/homeassistant/components/zha/core/channels/__init__.py +++ b/homeassistant/components/zha/core/channels/__init__.py @@ -4,6 +4,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union import zigpy.zcl.clusters.closures +from homeassistant.const import ATTR_DEVICE_ID from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_send @@ -157,6 +158,7 @@ class Channels: { const.ATTR_DEVICE_IEEE: str(self.zha_device.ieee), const.ATTR_UNIQUE_ID: self.unique_id, + ATTR_DEVICE_ID: self.zha_device.device_id, **event_data, }, ) diff --git a/homeassistant/components/zha/core/device.py b/homeassistant/components/zha/core/device.py index 2b4f8359e90..81b522308ff 100644 --- a/homeassistant/components/zha/core/device.py +++ b/homeassistant/components/zha/core/device.py @@ -30,7 +30,6 @@ from .const import ( ATTR_CLUSTER_ID, ATTR_COMMAND, ATTR_COMMAND_TYPE, - ATTR_DEVICE_IEEE, ATTR_DEVICE_TYPE, ATTR_ENDPOINT_ID, ATTR_ENDPOINT_NAMES, @@ -355,10 +354,8 @@ class ZHADevice(LogMixin): self.hass.async_create_task(self._async_became_available()) return if availability_changed and not available: - self.hass.bus.async_fire( - "zha_event", + self._channels.zha_send_event( { - ATTR_DEVICE_IEEE: str(self.ieee), "device_event_type": "device_offline", }, ) diff --git a/homeassistant/const.py b/homeassistant/const.py index 315293def48..173a39080b7 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -288,6 +288,9 @@ ATTR_ENTITY_ID = "entity_id" # Contains one string or a list of strings, each being an area id ATTR_AREA_ID = "area_id" +# Contains one string, the device ID +ATTR_DEVICE_ID = "device_id" + # String with a friendly name for the entity ATTR_FRIENDLY_NAME = "friendly_name" diff --git a/tests/components/zha/test_channels.py b/tests/components/zha/test_channels.py index 8a6d934d373..afae1b661ab 100644 --- a/tests/components/zha/test_channels.py +++ b/tests/components/zha/test_channels.py @@ -15,6 +15,7 @@ import homeassistant.components.zha.core.registries as registries from .common import get_zha_gateway, make_zcl_header import tests.async_mock +from tests.common import async_capture_events @pytest.fixture @@ -451,10 +452,7 @@ async def test_poll_control_cluster_command(hass, poll_control_device): checkin_mock = tests.async_mock.AsyncMock() poll_control_ch = poll_control_device.channels.pools[0].all_channels["1:0x0020"] cluster = poll_control_ch.cluster - - events = [] - hass.bus.async_listen("zha_event", lambda x: events.append(x)) - await hass.async_block_till_done() + events = async_capture_events(hass, "zha_event") with mock.patch.object(poll_control_ch, "check_in_response", checkin_mock): tsn = 22 @@ -475,3 +473,4 @@ async def test_poll_control_cluster_command(hass, poll_control_device): assert data["args"][1] is mock.sentinel.args2 assert data["args"][2] is mock.sentinel.args3 assert data["unique_id"] == "00:11:22:33:44:55:66:77:1:0x0020" + assert data["device_id"] == poll_control_device.device_id