diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 73f6ed31..00555ef0 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,19 @@ 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: + area_entity_ids = area_entities(self.hass, area_id) + for entity_id in area_entity_ids: + if entity_id.startswith(LIGHT_DOMAIN): + entity_ids.append(entity_id) + _LOGGER.debug( + "Found entity_ids '%s' for area_id '%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 2038fe8d..4739855e 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -50,6 +50,7 @@ from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN import homeassistant.config as config_util from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ( + ATTR_AREA_ID, ATTR_ENTITY_ID, CONF_LIGHTS, CONF_NAME, @@ -59,11 +60,12 @@ from homeassistant.const import ( STATE_ON, ) from homeassistant.core import Context, State +from homeassistant.helpers import entity_registry from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util import pytest -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, mock_area_registry from tests.components.demo.test_light import ENTITY_LIGHT _LOGGER = logging.getLogger(__name__) @@ -867,3 +869,36 @@ async def test_separate_turn_on_commands(hass, separate_turn_on_commands): assert sleep_brightness != brightness assert sleep_color_temp != color_temp + + +async def test_area(hass): + switch, (light, *_) = await setup_lights_and_switch(hass) + + area_registry = mock_area_registry(hass) + area_registry.async_create("test_area") + + entity = entity_registry.async_get(hass).async_get_or_create( + LIGHT_DOMAIN, "demo", light.unique_id, area_id="test_area" + ) + _LOGGER.debug("test_area entity: %s", entity) + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_AREA_ID: entity.area_id}, + blocking=True, + ) + await hass.async_block_till_done() + assert light.entity_id in switch.turn_on_off_listener.last_service_data + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_AREA_ID: entity.area_id}, + 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, + ) + assert light.entity_id not in switch.turn_on_off_listener.last_service_data