Skip to content

Commit

Permalink
Merge pull request #321 from basnijholt/area_id
Browse files Browse the repository at this point in the history
Add support for areas and add a test that reproduce issue of #75 #283
  • Loading branch information
basnijholt authored Aug 30, 2022
2 parents 7cabb0d + 48b7bc4 commit bc0f84f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
16 changes: 15 additions & 1 deletion custom_components/adaptive_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
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,
Expand Down Expand Up @@ -80,6 +81,7 @@
)
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,
Expand Down Expand Up @@ -1276,7 +1278,19 @@ async def turn_on_off_event_listener(self, event: Event) -> None:

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
Expand Down
37 changes: 36 additions & 1 deletion tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
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,
Expand All @@ -59,11 +60,12 @@
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__)
Expand Down Expand Up @@ -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

0 comments on commit bc0f84f

Please sign in to comment.