Fix device refresh service can always add devices (#43950)

pull/44175/head
Robert Svensson 2020-12-05 11:53:43 +01:00 committed by Franck Nijhof
parent 4c2dfa54da
commit 625f219d6b
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 28 additions and 7 deletions

View File

@ -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 = []

View File

@ -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):

View File

@ -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")