Use area_entities to extract entity_ids from area

This commit is contained in:
Bas Nijholt 2022-08-29 18:51:24 -07:00
commit b64c44fc8d
2 changed files with 18 additions and 2 deletions

View file

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

View file

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