From 8e0addd216c8691af3e57ff2ec340a5fdc965ca8 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Mon, 18 Jan 2021 15:00:27 +0100 Subject: [PATCH] Fix error with Axis light events without representation in light control (#45277) --- homeassistant/components/axis/light.py | 5 ++++- tests/components/axis/test_light.py | 27 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/axis/light.py b/homeassistant/components/axis/light.py index df4c00415ff..75a42b13cbf 100644 --- a/homeassistant/components/axis/light.py +++ b/homeassistant/components/axis/light.py @@ -18,7 +18,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): """Set up a Axis light.""" device = hass.data[AXIS_DOMAIN][config_entry.unique_id] - if not device.api.vapix.light_control: + if ( + device.api.vapix.light_control is None + or len(device.api.vapix.light_control) == 0 + ): return @callback diff --git a/tests/components/axis/test_light.py b/tests/components/axis/test_light.py index 37b251d8ede..db4ba86ceae 100644 --- a/tests/components/axis/test_light.py +++ b/tests/components/axis/test_light.py @@ -14,7 +14,12 @@ from homeassistant.const import ( ) from homeassistant.setup import async_setup_component -from .test_device import API_DISCOVERY_RESPONSE, NAME, setup_axis_integration +from .test_device import ( + API_DISCOVERY_RESPONSE, + LIGHT_CONTROL_RESPONSE, + NAME, + setup_axis_integration, +) API_DISCOVERY_LIGHT_CONTROL = { "id": "light-control", @@ -57,6 +62,26 @@ async def test_no_lights(hass): assert not hass.states.async_entity_ids(LIGHT_DOMAIN) +async def test_no_light_entity_without_light_control_representation(hass): + """Verify no lights entities get created without light control representation.""" + api_discovery = deepcopy(API_DISCOVERY_RESPONSE) + api_discovery["data"]["apiList"].append(API_DISCOVERY_LIGHT_CONTROL) + + light_control = deepcopy(LIGHT_CONTROL_RESPONSE) + light_control["data"]["items"] = [] + + with patch.dict(API_DISCOVERY_RESPONSE, api_discovery), patch.dict( + LIGHT_CONTROL_RESPONSE, light_control + ): + config_entry = await setup_axis_integration(hass) + device = hass.data[AXIS_DOMAIN][config_entry.unique_id] + + device.api.event.update([EVENT_ON]) + await hass.async_block_till_done() + + assert not hass.states.async_entity_ids(LIGHT_DOMAIN) + + async def test_lights(hass): """Test that lights are loaded properly.""" api_discovery = deepcopy(API_DISCOVERY_RESPONSE)