diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 73f6ed31..a8349f65 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -48,6 +48,7 @@ from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( + ATTR_AREA_ID, ATTR_DOMAIN, ATTR_ENTITY_ID, ATTR_SERVICE, @@ -80,6 +81,7 @@ from homeassistant.helpers.event import ( ) from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.sun import get_astral_location +from homeassistant.helpers.template import area_entities from homeassistant.util import slugify from homeassistant.util.color import ( color_RGB_to_xy, @@ -1276,7 +1278,16 @@ class TurnOnOffListener: service = event.data[ATTR_SERVICE] service_data = event.data[ATTR_SERVICE_DATA] - entity_ids = cv.ensure_list_csv(service_data[ATTR_ENTITY_ID]) + if ATTR_ENTITY_ID in service_data: + entity_ids = cv.ensure_list_csv(service_data[ATTR_ENTITY_ID]) + elif ATTR_AREA_ID in service_data: + area_ids = cv.ensure_list_csv(service_data[ATTR_AREA_ID]) + entity_ids = [] + for area_id in area_ids: + entity_ids.extend(area_entities(self.hass, area_id)) + _LOGGER.debug( + "Found entity_ids '%s' in area area_id %s: %s", entity_ids, area_id + ) if not any(eid in self.lights for eid in entity_ids): return diff --git a/tests/test_switch.py b/tests/test_switch.py index 1473a29e..1de95909 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -871,7 +871,7 @@ async def test_separate_turn_on_commands(hass, separate_turn_on_commands): async def test_area(hass): - _, (light, *_) = await setup_lights_and_switch(hass) + switch, (light, *_) = await setup_lights_and_switch(hass) device_in_area = device_registry.DeviceEntry(area_id="test-area") mock_device_registry(hass, {device_in_area.id: device_in_area}) @@ -897,3 +897,8 @@ async def test_area(hass): blocking=True, ) await hass.async_block_till_done() + + _LOGGER.debug( + "switch.turn_on_off_listener.last_service_data: %s", + switch.turn_on_off_listener.last_service_data, + )