Custom component loading cleanup (#14211)
* Clean up custom component loading * Fix some tests * Fix some stuff * Make imports work again * Fix tests * Remove debug print * Lint
This commit is contained in:
parent
5d96751168
commit
83d300fd11
50 changed files with 315 additions and 392 deletions
|
@ -16,7 +16,6 @@ from homeassistant.const import STATE_HOME, STATE_NOT_HOME
|
|||
from homeassistant.helpers.event import (
|
||||
async_track_point_in_utc_time, async_track_state_change)
|
||||
from homeassistant.helpers.sun import is_up, get_astral_event_next
|
||||
from homeassistant.loader import get_component
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
DOMAIN = 'device_sun_light_trigger'
|
||||
|
@ -48,9 +47,9 @@ CONFIG_SCHEMA = vol.Schema({
|
|||
def async_setup(hass, config):
|
||||
"""Set up the triggers to control lights based on device presence."""
|
||||
logger = logging.getLogger(__name__)
|
||||
device_tracker = get_component('device_tracker')
|
||||
group = get_component('group')
|
||||
light = get_component('light')
|
||||
device_tracker = hass.components.device_tracker
|
||||
group = hass.components.group
|
||||
light = hass.components.light
|
||||
conf = config[DOMAIN]
|
||||
disable_turn_off = conf.get(CONF_DISABLE_TURN_OFF)
|
||||
light_group = conf.get(CONF_LIGHT_GROUP, light.ENTITY_ID_ALL_LIGHTS)
|
||||
|
@ -58,14 +57,14 @@ def async_setup(hass, config):
|
|||
device_group = conf.get(
|
||||
CONF_DEVICE_GROUP, device_tracker.ENTITY_ID_ALL_DEVICES)
|
||||
device_entity_ids = group.get_entity_ids(
|
||||
hass, device_group, device_tracker.DOMAIN)
|
||||
device_group, device_tracker.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(hass, light_group, light.DOMAIN)
|
||||
light_ids = group.get_entity_ids(light_group, light.DOMAIN)
|
||||
|
||||
if not light_ids:
|
||||
logger.error("No lights found to turn on")
|
||||
|
@ -85,9 +84,9 @@ def async_setup(hass, config):
|
|||
|
||||
def async_turn_on_before_sunset(light_id):
|
||||
"""Turn on lights."""
|
||||
if not device_tracker.is_on(hass) or light.is_on(hass, light_id):
|
||||
if not device_tracker.is_on() or light.is_on(light_id):
|
||||
return
|
||||
light.async_turn_on(hass, light_id,
|
||||
light.async_turn_on(light_id,
|
||||
transition=LIGHT_TRANSITION_TIME.seconds,
|
||||
profile=light_profile)
|
||||
|
||||
|
@ -129,7 +128,7 @@ 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(hass, light_group)
|
||||
lights_are_on = group.is_on(light_group)
|
||||
light_needed = not (lights_are_on or is_up(hass))
|
||||
|
||||
# These variables are needed for the elif check
|
||||
|
@ -139,7 +138,7 @@ def async_setup(hass, config):
|
|||
# Do we need lights?
|
||||
if light_needed:
|
||||
logger.info("Home coming event for %s. Turning lights on", entity)
|
||||
light.async_turn_on(hass, light_ids, profile=light_profile)
|
||||
light.async_turn_on(light_ids, profile=light_profile)
|
||||
|
||||
# Are we in the time span were we would turn on the lights
|
||||
# if someone would be home?
|
||||
|
@ -152,7 +151,7 @@ def async_setup(hass, config):
|
|||
# when the fading in started and turn it on if so
|
||||
for index, light_id in enumerate(light_ids):
|
||||
if now > start_point + index * LIGHT_TRANSITION_TIME:
|
||||
light.async_turn_on(hass, light_id)
|
||||
light.async_turn_on(light_id)
|
||||
|
||||
else:
|
||||
# If this light didn't happen to be turned on yet so
|
||||
|
@ -169,12 +168,12 @@ 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(hass, light_group):
|
||||
if not group.is_on(light_group):
|
||||
return
|
||||
|
||||
logger.info(
|
||||
"Everyone has left but there are lights on. Turning them off")
|
||||
light.async_turn_off(hass, light_ids)
|
||||
light.async_turn_off(light_ids)
|
||||
|
||||
async_track_state_change(
|
||||
hass, device_group, turn_off_lights_when_all_leave,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue