diff --git a/homeassistant/components/deconz/deconz_event.py b/homeassistant/components/deconz/deconz_event.py index 81d3aa94d31..706850477d8 100644 --- a/homeassistant/components/deconz/deconz_event.py +++ b/homeassistant/components/deconz/deconz_event.py @@ -106,8 +106,11 @@ class DeconzEvent(DeconzBase): self.gateway.hass.bus.async_fire(CONF_DECONZ_EVENT, data) - async def async_update_device_registry(self): + async def async_update_device_registry(self) -> None: """Update device registry.""" + if not self.device_info: + return + device_registry = ( await self.gateway.hass.helpers.device_registry.async_get_registry() ) diff --git a/tests/components/deconz/test_deconz_event.py b/tests/components/deconz/test_deconz_event.py index 8a2e6a1d465..fc7544f3918 100644 --- a/tests/components/deconz/test_deconz_event.py +++ b/tests/components/deconz/test_deconz_event.py @@ -171,3 +171,33 @@ async def test_deconz_events(hass, aioclient_mock, mock_deconz_websocket): await hass.config_entries.async_remove(config_entry.entry_id) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 + + +async def test_deconz_events_bad_unique_id(hass, aioclient_mock, mock_deconz_websocket): + """Verify no devices are created if unique id is bad or missing.""" + data = { + "sensors": { + "1": { + "name": "Switch 1 no unique id", + "type": "ZHASwitch", + "state": {"buttonevent": 1000}, + "config": {}, + }, + "2": { + "name": "Switch 2 bad unique id", + "type": "ZHASwitch", + "state": {"buttonevent": 1000}, + "config": {"battery": 100}, + "uniqueid": "00:00-00", + }, + } + } + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration(hass, aioclient_mock) + + device_registry = await hass.helpers.device_registry.async_get_registry() + + assert len(hass.states.async_all()) == 1 + assert ( + len(async_entries_for_config_entry(device_registry, config_entry.entry_id)) == 2 + ) diff --git a/tests/components/deconz/test_services.py b/tests/components/deconz/test_services.py index a631327351f..249f4dbbb57 100644 --- a/tests/components/deconz/test_services.py +++ b/tests/components/deconz/test_services.py @@ -8,6 +8,7 @@ from homeassistant.components.deconz.const import ( CONF_BRIDGE_ID, DOMAIN as DECONZ_DOMAIN, ) +from homeassistant.components.deconz.deconz_event import CONF_DECONZ_EVENT from homeassistant.components.deconz.services import ( DECONZ_SERVICES, SERVICE_CONFIGURE_DEVICE, @@ -31,6 +32,8 @@ from .test_gateway import ( setup_deconz_integration, ) +from tests.common import async_capture_events + async def test_service_setup(hass): """Verify service setup works.""" @@ -229,6 +232,70 @@ async def test_service_refresh_devices(hass, aioclient_mock): assert len(hass.states.async_all()) == 4 +async def test_service_refresh_devices_trigger_no_state_update(hass, aioclient_mock): + """Verify that gateway.ignore_state_updates are honored.""" + data = { + "sensors": { + "1": { + "name": "Switch 1", + "type": "ZHASwitch", + "state": {"buttonevent": 1000}, + "config": {"battery": 100}, + "uniqueid": "00:00:00:00:00:00:00:01-00", + } + } + } + with patch.dict(DECONZ_WEB_REQUEST, data): + config_entry = await setup_deconz_integration(hass, aioclient_mock) + + assert len(hass.states.async_all()) == 1 + + captured_events = async_capture_events(hass, CONF_DECONZ_EVENT) + + aioclient_mock.clear_requests() + + data = { + "groups": { + "1": { + "id": "Group 1 id", + "name": "Group 1 name", + "type": "LightGroup", + "state": {}, + "action": {}, + "scenes": [{"id": "1", "name": "Scene 1"}], + "lights": ["1"], + } + }, + "lights": { + "1": { + "name": "Light 1 name", + "state": {"reachable": True}, + "type": "Light", + "uniqueid": "00:00:00:00:00:00:00:01-00", + } + }, + "sensors": { + "1": { + "name": "Switch 1", + "type": "ZHASwitch", + "state": {"buttonevent": 1000}, + "config": {"battery": 100}, + "uniqueid": "00:00:00:00:00:00:00:01-00", + } + }, + } + + mock_deconz_request(aioclient_mock, config_entry.data, data) + + await hass.services.async_call( + DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH, service_data={CONF_BRIDGE_ID: BRIDGEID} + ) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 4 + assert len(captured_events) == 0 + + async def test_remove_orphaned_entries_service(hass, aioclient_mock): """Test service works and also don't remove more than expected.""" data = {