Entity Component to no longer generate automatic groups (#23789)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-01-07 17:30:53 +01:00
1 parent 13e6479b6e
commit 9aa02e35a7
41 files changed
+149 -377

No files matched your search

@@ -66,23 +66,41 @@ async def async_setup(hass, config):
person = hass.components.person
conf = config[DOMAIN]
disable_turn_off = conf.get(CONF_DISABLE_TURN_OFF)
light_group = conf.get(CONF_LIGHT_GROUP, light.ENTITY_ID_ALL_LIGHTS)
light_group = conf.get(CONF_LIGHT_GROUP)
light_profile = conf.get(CONF_LIGHT_PROFILE)
device_group = conf.get(CONF_DEVICE_GROUP, device_tracker.ENTITY_ID_ALL_DEVICES)
device_entity_ids = group.get_entity_ids(device_group, device_tracker.DOMAIN)
device_entity_ids.extend(group.get_entity_ids(device_group, person.DOMAIN))
device_group = conf.get(CONF_DEVICE_GROUP)
if device_group is None:
device_entity_ids = hass.states.async_entity_ids(device_tracker.DOMAIN)
else:
device_entity_ids = group.get_entity_ids(device_group, device_tracker.DOMAIN)
device_entity_ids.extend(group.get_entity_ids(device_group, person.DOMAIN))
if not device_entity_ids:
logger.error("No devices found to track")
return False
# Get the light IDs from the specified group
light_ids = group.get_entity_ids(light_group, light.DOMAIN)
if light_group is None:
light_ids = hass.states.async_entity_ids(light.DOMAIN)
else:
light_ids = group.get_entity_ids(light_group, light.DOMAIN)
if not light_ids:
logger.error("No lights found to turn on")
return False
@callback
def anyone_home():
"""Test if anyone is home."""
return any(device_tracker.is_on(dt_id) for dt_id in device_entity_ids)
@callback
def any_light_on():
"""Test if any light on."""
return any(light.is_on(light_id) for light_id in light_ids)
def calc_time_for_light_when_sunset():
"""Calculate the time when to start fading lights in when sun sets.
@@ -97,7 +115,7 @@ async def async_setup(hass, config):
def async_turn_on_before_sunset(light_id):
"""Turn on lights."""
if not device_tracker.is_on() or light.is_on(light_id):
if not anyone_home() or light.is_on(light_id):
return
hass.async_create_task(
hass.services.async_call(
@@ -153,7 +171,7 @@ async def async_setup(hass, config):
@callback
def check_light_on_dev_state_change(entity, old_state, new_state):
"""Handle tracked device state changes."""
lights_are_on = group.is_on(light_group)
lights_are_on = any_light_on()
light_needed = not (lights_are_on or is_up(hass))
# These variables are needed for the elif check
@@ -208,7 +226,12 @@ async def async_setup(hass, config):
@callback
def turn_off_lights_when_all_leave(entity, old_state, new_state):
"""Handle device group state change."""
if not group.is_on(light_group):
# Make sure there is not someone home
if anyone_home():
return
# Check if any light is on
if not any_light_on():
return
logger.info("Everyone has left but there are lights on. Turning them off")
@@ -219,7 +242,11 @@ async def async_setup(hass, config):
)
async_track_state_change(
hass, device_group, turn_off_lights_when_all_leave, STATE_HOME, STATE_NOT_HOME
hass,
device_entity_ids,
turn_off_lights_when_all_leave,
STATE_HOME,
STATE_NOT_HOME,
)
return True