async def test_setup_entry_multiple_gateways(hass): """Test setup entry is successful with multiple gateways.""" config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) data = deepcopy(DECONZ_WEB_REQUEST) data["config"]["bridgeid"] = "01234E56789B" config_entry2 = await setup_deconz_integration(hass, get_state_response=data, entry_id="2") gateway2 = get_gateway_from_config_entry(hass, config_entry2) assert len(hass.data[DECONZ_DOMAIN]) == 2 assert hass.data[DECONZ_DOMAIN][gateway.bridgeid].master assert not hass.data[DECONZ_DOMAIN][gateway2.bridgeid].master
async def test_service_refresh_devices(hass): """Test that service can refresh devices.""" config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) data = {CONF_BRIDGE_ID: BRIDGEID} with patch( "pydeconz.DeconzSession.request", return_value={ "groups": GROUP, "lights": LIGHT, "sensors": SENSOR }, ): await hass.services.async_call(DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH, service_data=data) await hass.async_block_till_done() assert gateway.deconz_ids == { "light.group_1_name": "/groups/1", "light.light_1_name": "/lights/1", "scene.group_1_name_scene_1": "/groups/1/scenes/1", "sensor.sensor_1_name": "/sensors/1", }
async def test_only_new_scenes_are_created(hass, aioclient_mock): """Test that scenes works.""" data = { "groups": { "1": { "id": "Light group id", "name": "Light group", "type": "LightGroup", "state": {"all_on": False, "any_on": True}, "action": {}, "scenes": [{"id": "1", "name": "Scene"}], "lights": [], } } } with patch.dict(DECONZ_WEB_REQUEST, data): config_entry = await setup_deconz_integration(hass, aioclient_mock) assert len(hass.states.async_all()) == 1 gateway = get_gateway_from_config_entry(hass, config_entry) async_dispatcher_send(hass, gateway.signal_new_scene) await hass.async_block_till_done() assert len(hass.states.async_all()) == 1
async def test_configure_service_with_entity_and_field(hass, aioclient_mock): """Test that service invokes pydeconz with the correct path and data.""" config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = get_gateway_from_config_entry(hass, config_entry) gateway.deconz_ids["light.test"] = "/lights/1" data = { SERVICE_ENTITY: "light.test", SERVICE_FIELD: "/state", SERVICE_DATA: { "on": True, "attr1": 10, "attr2": 20 }, } mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/1/state") await hass.services.async_call(DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data, blocking=True) assert aioclient_mock.mock_calls[1][2] == { "on": True, "attr1": 10, "attr2": 20 }
async def test_service_refresh_devices(hass, aioclient_mock): """Test that service can refresh devices.""" config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = get_gateway_from_config_entry(hass, config_entry) aioclient_mock.clear_requests() data = {CONF_BRIDGE_ID: BRIDGEID} mock_deconz_request( aioclient_mock, config_entry.data, { "groups": GROUP, "lights": LIGHT, "sensors": SENSOR }, ) await hass.services.async_call(DECONZ_DOMAIN, SERVICE_DEVICE_REFRESH, service_data=data) await hass.async_block_till_done() assert gateway.deconz_ids == { "light.group_1_name": "/groups/1", "light.light_1_name": "/lights/1", "scene.group_1_name_scene_1": "/groups/1/scenes/1", "sensor.sensor_1_name": "/sensors/1", }
async def test_gateway_setup(hass, aioclient_mock): """Successful setup.""" with patch( "homeassistant.config_entries.ConfigEntries.async_forward_entry_setup", return_value=True, ) as forward_entry_setup: config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway.bridgeid == BRIDGEID assert gateway.master is True assert gateway.option_allow_clip_sensor is False assert gateway.option_allow_deconz_groups is True assert gateway.option_allow_new_devices is True assert len(gateway.deconz_ids) == 0 assert len(hass.states.async_all()) == 0 assert forward_entry_setup.mock_calls[0][1] == ( config_entry, BINARY_SENSOR_DOMAIN, ) assert forward_entry_setup.mock_calls[1][1] == (config_entry, CLIMATE_DOMAIN) assert forward_entry_setup.mock_calls[2][1] == (config_entry, COVER_DOMAIN) assert forward_entry_setup.mock_calls[3][1] == (config_entry, FAN_DOMAIN) assert forward_entry_setup.mock_calls[4][1] == (config_entry, LIGHT_DOMAIN) assert forward_entry_setup.mock_calls[5][1] == (config_entry, LOCK_DOMAIN) assert forward_entry_setup.mock_calls[6][1] == (config_entry, SCENE_DOMAIN) assert forward_entry_setup.mock_calls[7][1] == (config_entry, SENSOR_DOMAIN) assert forward_entry_setup.mock_calls[8][1] == (config_entry, SWITCH_DOMAIN)
async def test_scenes(hass): """Test that scenes works.""" data = deepcopy(DECONZ_WEB_REQUEST) data["groups"] = deepcopy(GROUPS) config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 1 assert hass.states.get("scene.light_group_scene") # Verify service calls group_scene = gateway.api.groups["1"].scenes["1"] # Service turn on scene with patch.object(group_scene, "_request", return_value=True) as set_callback: await hass.services.async_call( SCENE_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: "scene.light_group_scene"}, blocking=True, ) await hass.async_block_till_done() set_callback.assert_called_with("put", "/groups/1/scenes/1/recall", json={}) await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0
async def test_update_address(hass, aioclient_mock): """Make sure that connection status triggers a dispatcher send.""" config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway.api.host == "1.2.3.4" with patch( "homeassistant.components.deconz.async_setup_entry", return_value=True, ) as mock_setup_entry: await hass.config_entries.flow.async_init( DECONZ_DOMAIN, data=ssdp.SsdpServiceInfo( ssdp_st="mock_st", ssdp_usn="mock_usn", ssdp_location="http://2.3.4.5:80/", upnp={ ATTR_UPNP_MANUFACTURER_URL: DECONZ_MANUFACTURERURL, ATTR_UPNP_SERIAL: BRIDGEID, ATTR_UPNP_UDN: "uuid:456DEF", }, ), context={"source": SOURCE_SSDP}, ) await hass.async_block_till_done() assert gateway.api.host == "2.3.4.5" assert len(mock_setup_entry.mock_calls) == 1
async def test_binary_sensors(hass): """Test successful creation of binary sensor entities.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 3 presence_sensor = hass.states.get("binary_sensor.presence_sensor") assert presence_sensor.state == STATE_OFF assert presence_sensor.attributes["device_class"] == DEVICE_CLASS_MOTION assert hass.states.get("binary_sensor.temperature_sensor") is None assert hass.states.get("binary_sensor.clip_presence_sensor") is None vibration_sensor = hass.states.get("binary_sensor.vibration_sensor") assert vibration_sensor.state == STATE_ON assert vibration_sensor.attributes["device_class"] == DEVICE_CLASS_VIBRATION state_changed_event = { "t": "event", "e": "changed", "r": "sensors", "id": "1", "state": {"presence": True}, } gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() assert hass.states.get("binary_sensor.presence_sensor").state == STATE_ON await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0
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}, ) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { "t": "event", "e": "added", "r": "sensors", "id": "1", "sensor": deepcopy(SENSORS["1"]), } gateway.api.event_handler(state_added_event) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0 entity_registry = await hass.helpers.entity_registry.async_get_registry() assert ( len(async_entries_for_config_entry(entity_registry, config_entry.entry_id)) == 0 )
async def test_verify_state_update(hass): """Test that state update properly.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) assert hass.states.get("climate.thermostat").state == "auto" state_changed_event = { "t": "event", "e": "changed", "r": "sensors", "id": "1", "state": { "on": False }, } gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() assert hass.states.get("climate.thermostat").state == "auto" assert gateway.api.sensors["1"].changed_keys == { "state", "r", "t", "on", "e", "id" }
async def test_configure_service_with_entity_and_field(hass): """Test that service invokes pydeconz with the correct path and data.""" config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) gateway.deconz_ids["light.test"] = "/light/1" data = { SERVICE_ENTITY: "light.test", SERVICE_FIELD: "/state", SERVICE_DATA: { "on": True, "attr1": 10, "attr2": 20 }, } with patch("pydeconz.DeconzSession.request", return_value=Mock(True)) as put_state: await hass.services.async_call(DECONZ_DOMAIN, SERVICE_CONFIGURE_DEVICE, service_data=data) await hass.async_block_till_done() put_state.assert_called_with("put", "/light/1/state", json={ "on": True, "attr1": 10, "attr2": 20 })
async def test_gateway_setup(hass): """Successful setup.""" with patch( "homeassistant.config_entries.ConfigEntries.async_forward_entry_setup", return_value=True, ) as forward_entry_setup: config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway.bridgeid == BRIDGEID assert gateway.master is True assert gateway.option_allow_clip_sensor is False assert gateway.option_allow_deconz_groups is True assert gateway.option_allow_new_devices is True assert len(gateway.deconz_ids) == 0 assert len(hass.states.async_all()) == 0 assert forward_entry_setup.mock_calls[0][1] == (config_entry, "binary_sensor") assert forward_entry_setup.mock_calls[1][1] == (config_entry, "climate") assert forward_entry_setup.mock_calls[2][1] == (config_entry, "cover") assert forward_entry_setup.mock_calls[3][1] == (config_entry, "light") assert forward_entry_setup.mock_calls[4][1] == (config_entry, "lock") assert forward_entry_setup.mock_calls[5][1] == (config_entry, "scene") assert forward_entry_setup.mock_calls[6][1] == (config_entry, "sensor") assert forward_entry_setup.mock_calls[7][1] == (config_entry, "switch")
async def test_power_plugs(hass): """Test that all supported switch entities are created.""" data = deepcopy(DECONZ_WEB_REQUEST) data["lights"] = deepcopy(POWER_PLUGS) config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 4 assert hass.states.get("switch.on_off_switch").state == STATE_ON assert hass.states.get("switch.smart_plug").state == STATE_OFF assert hass.states.get("switch.on_off_relay").state == STATE_ON assert hass.states.get("switch.unsupported_switch") is None state_changed_event = { "t": "event", "e": "changed", "r": "lights", "id": "1", "state": {"on": False}, } gateway.api.event_handler(state_changed_event) assert hass.states.get("switch.on_off_switch").state == STATE_OFF # Verify service calls on_off_switch_device = gateway.api.lights["1"] # Service turn on power plug with patch.object( on_off_switch_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: "switch.on_off_switch"}, blocking=True, ) await hass.async_block_till_done() set_callback.assert_called_with("put", "/lights/1/state", json={"on": True}) # Service turn off power plug with patch.object( on_off_switch_device, "_request", return_value=True ) as set_callback: await hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "switch.on_off_switch"}, blocking=True, ) await hass.async_block_till_done() set_callback.assert_called_with("put", "/lights/1/state", json={"on": False}) await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0
async def test_gateway_setup_fails(hass): """Retry setup.""" with patch( "homeassistant.components.deconz.gateway.get_gateway", side_effect=Exception ): config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway is None
async def test_setup_entry_successful(hass): """Test setup entry is successful.""" config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) assert hass.data[DECONZ_DOMAIN] assert gateway.bridgeid in hass.data[DECONZ_DOMAIN] assert hass.data[DECONZ_DOMAIN][gateway.bridgeid].master
async def test_reset_after_successful_setup(hass, aioclient_mock): """Make sure that connection status triggers a dispatcher send.""" config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = get_gateway_from_config_entry(hass, config_entry) result = await gateway.async_reset() await hass.async_block_till_done() assert result is True
async def test_lidl_christmas_light(hass): """Test that lights or groups entities are created.""" data = deepcopy(DECONZ_WEB_REQUEST) data["lights"] = { "0": { "etag": "87a89542bf9b9d0aa8134919056844f8", "hascolor": True, "lastannounced": None, "lastseen": "2020-12-05T22:57Z", "manufacturername": "_TZE200_s8gkrkxk", "modelid": "TS0601", "name": "xmas light", "state": { "bri": 25, "colormode": "hs", "effect": "none", "hue": 53691, "on": True, "reachable": True, "sat": 141, }, "swversion": None, "type": "Color dimmable light", "uniqueid": "58:8e:81:ff:fe:db:7b:be-01", } } config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) xmas_light_device = gateway.api.lights["0"] assert len(hass.states.async_all()) == 1 with patch.object(xmas_light_device, "_request", return_value=True) as set_callback: await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, { ATTR_ENTITY_ID: "light.xmas_light", ATTR_HS_COLOR: (20, 30), }, blocking=True, ) await hass.async_block_till_done() set_callback.assert_called_with( "put", "/lights/0/state", json={ "on": True, "hue": 3640, "sat": 76 }, ) assert hass.states.get("light.xmas_light")
async def test_locks(hass): """Test that all supported lock entities are created.""" data = deepcopy(DECONZ_WEB_REQUEST) data["lights"] = deepcopy(LOCKS) config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 1 assert hass.states.get("lock.door_lock").state == STATE_UNLOCKED door_lock = hass.states.get("lock.door_lock") assert door_lock.state == STATE_UNLOCKED state_changed_event = { "t": "event", "e": "changed", "r": "lights", "id": "1", "state": {"on": True}, } gateway.api.event_handler(state_changed_event) await hass.async_block_till_done() assert hass.states.get("lock.door_lock").state == STATE_LOCKED # Verify service calls door_lock_device = gateway.api.lights["1"] # Service lock door with patch.object(door_lock_device, "_request", return_value=True) as set_callback: await hass.services.async_call( LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: "lock.door_lock"}, blocking=True, ) await hass.async_block_till_done() set_callback.assert_called_with("put", "/lights/1/state", json={"on": True}) # Service unlock door with patch.object(door_lock_device, "_request", return_value=True) as set_callback: await hass.services.async_call( LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: "lock.door_lock"}, blocking=True, ) await hass.async_block_till_done() set_callback.assert_called_with("put", "/lights/1/state", json={"on": False}) await hass.config_entries.async_unload(config_entry.entry_id) assert len(hass.states.async_all()) == 0
async def test_gateway_setup(hass, aioclient_mock): """Successful setup.""" with patch( "homeassistant.config_entries.ConfigEntries.async_forward_entry_setup", return_value=True, ) as forward_entry_setup: config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway.bridgeid == BRIDGEID assert gateway.master is True assert gateway.option_allow_clip_sensor is False assert gateway.option_allow_deconz_groups is True assert gateway.option_allow_new_devices is True assert len(gateway.deconz_ids) == 0 assert len(hass.states.async_all()) == 0 assert forward_entry_setup.mock_calls[0][1] == ( config_entry, ALARM_CONTROL_PANEL_DOMAIN, ) assert forward_entry_setup.mock_calls[1][1] == ( config_entry, BINARY_SENSOR_DOMAIN, ) assert forward_entry_setup.mock_calls[2][1] == (config_entry, BUTTON_DOMAIN) assert forward_entry_setup.mock_calls[3][1] == (config_entry, CLIMATE_DOMAIN) assert forward_entry_setup.mock_calls[4][1] == (config_entry, COVER_DOMAIN) assert forward_entry_setup.mock_calls[5][1] == (config_entry, FAN_DOMAIN) assert forward_entry_setup.mock_calls[6][1] == (config_entry, LIGHT_DOMAIN) assert forward_entry_setup.mock_calls[7][1] == (config_entry, LOCK_DOMAIN) assert forward_entry_setup.mock_calls[8][1] == (config_entry, NUMBER_DOMAIN) assert forward_entry_setup.mock_calls[9][1] == (config_entry, SCENE_DOMAIN) assert forward_entry_setup.mock_calls[10][1] == (config_entry, SENSOR_DOMAIN) assert forward_entry_setup.mock_calls[11][1] == (config_entry, SIREN_DOMAIN) assert forward_entry_setup.mock_calls[12][1] == (config_entry, SWITCH_DOMAIN) device_registry = dr.async_get(hass) gateway_entry = device_registry.async_get_device( identifiers={(DECONZ_DOMAIN, gateway.bridgeid)}) assert gateway_entry.configuration_url == f"http://{HOST}:{PORT}" assert gateway_entry.entry_type is dr.DeviceEntryType.SERVICE
async def test_helper_successful(hass): """Verify trigger helper.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = deepcopy(SENSORS) config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) device_id = gateway.events[0].device_id deconz_event = device_trigger._get_deconz_event_from_device_id( hass, device_id) assert deconz_event == gateway.events[0]
async def test_connection_status_signalling(hass): """Make sure that connection status triggers a dispatcher send.""" config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) event_call = Mock() unsub = async_dispatcher_connect(hass, gateway.signal_reachable, event_call) gateway.async_connection_status_callback(False) await hass.async_block_till_done() assert gateway.available is False assert len(event_call.mock_calls) == 1 unsub()
async def test_gateway_device_no_configuration_url_when_addon( hass, aioclient_mock): """Successful setup.""" with patch( "homeassistant.config_entries.ConfigEntries.async_forward_entry_setup", return_value=True, ): config_entry = await setup_deconz_integration(hass, aioclient_mock, source=SOURCE_HASSIO) gateway = get_gateway_from_config_entry(hass, config_entry) device_registry = dr.async_get(hass) gateway_entry = device_registry.async_get_device( identifiers={(DECONZ_DOMAIN, gateway.bridgeid)}) assert not gateway_entry.configuration_url
async def test_unload_entry_multiple_gateways(hass): """Test being able to unload an entry and master gateway gets moved.""" config_entry = await setup_deconz_integration(hass) data = deepcopy(DECONZ_WEB_REQUEST) data["config"]["bridgeid"] = "01234E56789B" config_entry2 = await setup_deconz_integration(hass, get_state_response=data, entry_id="2") gateway2 = get_gateway_from_config_entry(hass, config_entry2) assert len(hass.data[DECONZ_DOMAIN]) == 2 assert await async_unload_entry(hass, config_entry) assert len(hass.data[DECONZ_DOMAIN]) == 1 assert hass.data[DECONZ_DOMAIN][gateway2.bridgeid].master
async def test_add_new_binary_sensor_ignored(hass, aioclient_mock): """Test that adding a new binary sensor is not allowed.""" config_entry = await setup_deconz_integration( hass, aioclient_mock, 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 state_added_event = { "t": "event", "e": "added", "r": "sensors", "id": "1", "sensor": deepcopy(SENSORS["1"]), } gateway.api.event_handler(state_added_event) 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) aioclient_mock.clear_requests() data = { "groups": {}, "lights": {}, "sensors": { "1": deepcopy(SENSORS["1"]) }, } mock_deconz_request(aioclient_mock, config_entry.data, data) 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")
async def test_add_new_binary_sensor(hass): """Test that adding a new binary sensor works.""" config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { "t": "event", "e": "added", "r": "sensors", "id": "1", "sensor": deepcopy(SENSORS["1"]), } gateway.api.event_handler(state_added_event) await hass.async_block_till_done() assert len(hass.states.async_all()) == 1 assert hass.states.get("binary_sensor.presence_sensor").state == STATE_OFF
async def test_add_new_sensor(hass, aioclient_mock): """Test that adding a new sensor works.""" config_entry = await setup_deconz_integration(hass, aioclient_mock) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { "t": "event", "e": "added", "r": "sensors", "id": "1", "sensor": deepcopy(SENSORS["1"]), } gateway.api.event_handler(state_added_event) await hass.async_block_till_done() assert len(hass.states.async_all()) == 1 assert hass.states.get("sensor.light_level_sensor").state == "999.8"
async def test_add_new_climate_device(hass): """Test that adding a new climate device works.""" config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) assert len(hass.states.async_all()) == 0 state_added_event = { "t": "event", "e": "added", "r": "sensors", "id": "1", "sensor": deepcopy(SENSORS["1"]), } gateway.api.event_handler(state_added_event) await hass.async_block_till_done() assert len(hass.states.async_all()) == 2 assert hass.states.get("climate.thermostat").state == "auto" assert hass.states.get("sensor.thermostat_battery_level").state == "100"
async def test_add_battery_later(hass): """Test that a sensor without an initial battery state creates a battery sensor once state exist.""" data = deepcopy(DECONZ_WEB_REQUEST) data["sensors"] = {"1": deepcopy(SENSORS["3"])} config_entry = await setup_deconz_integration(hass, get_state_response=data) gateway = get_gateway_from_config_entry(hass, config_entry) remote = gateway.api.sensors["1"] assert len(hass.states.async_all()) == 0 assert len(gateway.events) == 1 assert len(remote._callbacks) == 2 # Event and battery tracker remote.update({"config": {"battery": 50}}) await hass.async_block_till_done() assert len(hass.states.async_all()) == 1 assert len(gateway.events) == 1 assert len(remote._callbacks) == 2 # Event and battery entity assert hass.states.get("sensor.switch_1_battery_level")
async def test_update_address(hass): """Make sure that connection status triggers a dispatcher send.""" config_entry = await setup_deconz_integration(hass) gateway = get_gateway_from_config_entry(hass, config_entry) assert gateway.api.host == "1.2.3.4" with patch( "homeassistant.components.deconz.async_setup_entry", return_value=True, ) as mock_setup_entry: await hass.config_entries.flow.async_init( deconz.config_flow.DOMAIN, data={ ssdp.ATTR_SSDP_LOCATION: "http://2.3.4.5:80/", ssdp.ATTR_UPNP_MANUFACTURER_URL: deconz.config_flow.DECONZ_MANUFACTURERURL, ssdp.ATTR_UPNP_SERIAL: BRIDGEID, ssdp.ATTR_UPNP_UDN: "uuid:456DEF", }, context={"source": "ssdp"}, ) await hass.async_block_till_done() assert gateway.api.host == "2.3.4.5" assert len(mock_setup_entry.mock_calls) == 1