From bc83e307614b0263dd772c9eb2ff7c4117cc0d6b Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sat, 5 Dec 2020 11:53:43 +0100 Subject: [PATCH] Fix device refresh service can always add devices (#43950) --- homeassistant/components/deconz/gateway.py | 6 ++++-- homeassistant/components/deconz/services.py | 8 +++---- tests/components/deconz/test_binary_sensor.py | 21 ++++++++++++++++++- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/deconz/gateway.py b/homeassistant/components/deconz/gateway.py index dc41bb778ec..881ea883c4c 100644 --- a/homeassistant/components/deconz/gateway.py +++ b/homeassistant/components/deconz/gateway.py @@ -121,9 +121,11 @@ class DeconzGateway: async_dispatcher_send(self.hass, self.signal_reachable, True) @callback - def async_add_device_callback(self, device_type, device=None) -> None: + def async_add_device_callback( + self, device_type, device=None, force: bool = False + ) -> None: """Handle event of new device creation in deCONZ.""" - if not self.option_allow_new_devices: + if not force and not self.option_allow_new_devices: return args = [] diff --git a/homeassistant/components/deconz/services.py b/homeassistant/components/deconz/services.py index 2c286fac0a1..d524354ff0b 100644 --- a/homeassistant/components/deconz/services.py +++ b/homeassistant/components/deconz/services.py @@ -146,10 +146,10 @@ async def async_refresh_devices_service(hass, data): await gateway.api.refresh_state() gateway.ignore_state_updates = False - gateway.async_add_device_callback(NEW_GROUP) - gateway.async_add_device_callback(NEW_LIGHT) - gateway.async_add_device_callback(NEW_SCENE) - gateway.async_add_device_callback(NEW_SENSOR) + gateway.async_add_device_callback(NEW_GROUP, force=True) + gateway.async_add_device_callback(NEW_LIGHT, force=True) + gateway.async_add_device_callback(NEW_SCENE, force=True) + gateway.async_add_device_callback(NEW_SENSOR, force=True) async def async_remove_orphaned_entries_service(hass, data): diff --git a/tests/components/deconz/test_binary_sensor.py b/tests/components/deconz/test_binary_sensor.py index 5038c5bf3f2..78a4f1e937d 100644 --- a/tests/components/deconz/test_binary_sensor.py +++ b/tests/components/deconz/test_binary_sensor.py @@ -10,15 +10,19 @@ from homeassistant.components.binary_sensor import ( from homeassistant.components.deconz.const import ( CONF_ALLOW_CLIP_SENSOR, CONF_ALLOW_NEW_DEVICES, + CONF_MASTER_GATEWAY, DOMAIN as DECONZ_DOMAIN, ) from homeassistant.components.deconz.gateway import get_gateway_from_config_entry +from homeassistant.components.deconz.services import SERVICE_DEVICE_REFRESH from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.helpers.entity_registry import async_entries_for_config_entry from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration +from tests.async_mock import patch + SENSORS = { "1": { "id": "Presence sensor id", @@ -172,7 +176,7 @@ async def test_add_new_binary_sensor_ignored(hass): """Test that adding a new binary sensor is not allowed.""" config_entry = await setup_deconz_integration( hass, - options={CONF_ALLOW_NEW_DEVICES: False}, + options={CONF_MASTER_GATEWAY: True, CONF_ALLOW_NEW_DEVICES: False}, ) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 @@ -188,8 +192,23 @@ async def test_add_new_binary_sensor_ignored(hass): await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 + assert not hass.states.get("binary_sensor.presence_sensor") entity_registry = await hass.helpers.entity_registry.async_get_registry() assert ( len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) == 0 ) + + with patch( + "pydeconz.DeconzSession.request", + return_value={ + "groups": {}, + "lights": {}, + "sensors": {"1": deepcopy(SENSORS["1"])}, + }, + ): + await hass.services.async_call(DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 1 + assert hass.states.get("binary_sensor.presence_sensor")