Methods defined inside setup make more sense
parent
086cf77443
commit
c438f35bcd
|
@ -56,7 +56,7 @@ def setup(bus, statemachine, host):
|
|||
entity = ENTITY_ID_FORMAT.format(util.slugify(device.friendly_name))
|
||||
|
||||
if not bus.has_service(DOMAIN, components.SERVICE_TURN_OFF):
|
||||
def _turn_off_service(service):
|
||||
def turn_off_service(service):
|
||||
""" Service to exit any running app on the specified ChromeCast and
|
||||
shows idle screen. Will quit all ChromeCasts if nothing specified.
|
||||
"""
|
||||
|
@ -76,7 +76,7 @@ def setup(bus, statemachine, host):
|
|||
pass
|
||||
|
||||
bus.register_service(DOMAIN, components.SERVICE_TURN_OFF,
|
||||
_turn_off_service)
|
||||
turn_off_service)
|
||||
|
||||
bus.register_service(DOMAIN, "start_fireplace",
|
||||
lambda service:
|
||||
|
@ -91,7 +91,7 @@ def setup(bus, statemachine, host):
|
|||
pychromecast.play_youtube_video(
|
||||
host, service.data['video']))
|
||||
|
||||
def _update_chromecast_state(time): # pylint: disable=unused-argument
|
||||
def update_chromecast_state(time): # pylint: disable=unused-argument
|
||||
""" Retrieve state of Chromecast and update statemachine. """
|
||||
logger.info("Updating app status")
|
||||
status = pychromecast.get_app_status(host)
|
||||
|
@ -109,8 +109,8 @@ def setup(bus, statemachine, host):
|
|||
else:
|
||||
statemachine.set_state(entity, STATE_NO_APP, {ATTR_HOST: host})
|
||||
|
||||
ha.track_time_change(bus, _update_chromecast_state)
|
||||
ha.track_time_change(bus, update_chromecast_state)
|
||||
|
||||
_update_chromecast_state(None)
|
||||
update_chromecast_state(None)
|
||||
|
||||
return True
|
||||
|
|
|
@ -48,7 +48,7 @@ def setup(bus, statemachine, light_group=None):
|
|||
len(light_ids))
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def handle_sun_rising(entity, old_state, new_state):
|
||||
def schedule_light_on_sun_rise(entity, old_state, new_state):
|
||||
"""The moment sun sets we want to have all the lights on.
|
||||
We will schedule to have each light start after one another
|
||||
and slowly transition in."""
|
||||
|
@ -76,15 +76,16 @@ def setup(bus, statemachine, light_group=None):
|
|||
|
||||
# Track every time sun rises so we can schedule a time-based
|
||||
# pre-sun set event
|
||||
ha.track_state_change(bus, sun.ENTITY_ID, handle_sun_rising,
|
||||
ha.track_state_change(bus, sun.ENTITY_ID,
|
||||
schedule_light_on_sun_rise,
|
||||
sun.STATE_BELOW_HORIZON, sun.STATE_ABOVE_HORIZON)
|
||||
|
||||
# If the sun is already above horizon
|
||||
# schedule the time-based pre-sun set event
|
||||
if sun.is_on(statemachine):
|
||||
handle_sun_rising(None, None, None)
|
||||
schedule_light_on_sun_rise(None, None, None)
|
||||
|
||||
def _handle_device_state_change(entity, old_state, new_state):
|
||||
def check_light_on_dev_state_change(entity, old_state, new_state):
|
||||
""" Function to handle tracked device state changes. """
|
||||
lights_are_on = group.is_on(statemachine, light_group)
|
||||
|
||||
|
@ -139,12 +140,12 @@ def setup(bus, statemachine, light_group=None):
|
|||
|
||||
# Track home coming of each seperate device
|
||||
for entity in device_entity_ids:
|
||||
ha.track_state_change(bus, entity, _handle_device_state_change,
|
||||
ha.track_state_change(bus, entity, check_light_on_dev_state_change,
|
||||
components.STATE_NOT_HOME, components.STATE_HOME)
|
||||
|
||||
# Track when all devices are gone to shut down lights
|
||||
ha.track_state_change(bus, device_tracker.ENTITY_ID_ALL_DEVICES,
|
||||
_handle_device_state_change, components.STATE_HOME,
|
||||
components.STATE_NOT_HOME)
|
||||
check_light_on_dev_state_change,
|
||||
components.STATE_HOME, components.STATE_NOT_HOME)
|
||||
|
||||
return True
|
||||
|
|
|
@ -71,16 +71,19 @@ class DeviceTracker(object):
|
|||
|
||||
self._read_known_devices_file()
|
||||
|
||||
ha.track_time_change(bus,
|
||||
lambda time:
|
||||
self.update_devices(
|
||||
device_scanner.scan_devices()))
|
||||
# Wrap it in a func instead of lambda so it can be identified in
|
||||
# the bus by its __name__ attribute.
|
||||
def update_device_state(time): # pylint: disable=unused-argument
|
||||
""" Triggers update of the device states. """
|
||||
self.update_devices()
|
||||
|
||||
ha.track_time_change(bus, update_device_state)
|
||||
|
||||
bus.register_service(DOMAIN,
|
||||
SERVICE_DEVICE_TRACKER_RELOAD,
|
||||
lambda service: self._read_known_devices_file())
|
||||
|
||||
self.update_devices(device_scanner.scan_devices())
|
||||
self.update_devices()
|
||||
|
||||
group.setup(bus, statemachine, GROUP_NAME_ALL_DEVICES,
|
||||
list(self.device_entity_ids))
|
||||
|
@ -93,10 +96,12 @@ class DeviceTracker(object):
|
|||
in self.known_devices
|
||||
if self.known_devices[device]['track']])
|
||||
|
||||
def update_devices(self, found_devices):
|
||||
def update_devices(self, found_devices=None):
|
||||
""" Update device states based on the found devices. """
|
||||
self.lock.acquire()
|
||||
|
||||
found_devices = found_devices or self.device_scanner.scan_devices()
|
||||
|
||||
now = datetime.now()
|
||||
|
||||
known_dev = self.known_devices
|
||||
|
|
|
@ -114,7 +114,7 @@ def setup(bus, statemachine, name, entity_ids):
|
|||
state_attr = {STATE_ATTR_ENTITY_IDS: entity_ids}
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def _update_group_state(entity_id, old_state, new_state):
|
||||
def update_group_state(entity_id, old_state, new_state):
|
||||
""" Updates the group state based on a state change by a tracked
|
||||
entity. """
|
||||
|
||||
|
@ -136,13 +136,13 @@ def setup(bus, statemachine, name, entity_ids):
|
|||
statemachine.set_state(group_entity_id, group_off, state_attr)
|
||||
|
||||
for entity_id in entity_ids:
|
||||
ha.track_state_change(bus, entity_id, _update_group_state)
|
||||
ha.track_state_change(bus, entity_id, update_group_state)
|
||||
|
||||
# group.setup is called to setup each group. Only the first time will we
|
||||
# register a turn_on and turn_off method for groups.
|
||||
|
||||
if not bus.has_service(DOMAIN, components.SERVICE_TURN_ON):
|
||||
def _turn_group_on_service(service):
|
||||
def turn_group_on_service(service):
|
||||
""" Call components.turn_on for each entity_id from this group. """
|
||||
for entity_id in get_entity_ids(statemachine,
|
||||
service.data.get(
|
||||
|
@ -151,10 +151,10 @@ def setup(bus, statemachine, name, entity_ids):
|
|||
components.turn_on(bus, entity_id)
|
||||
|
||||
bus.register_service(DOMAIN, components.SERVICE_TURN_ON,
|
||||
_turn_group_on_service)
|
||||
turn_group_on_service)
|
||||
|
||||
if not bus.has_service(DOMAIN, components.SERVICE_TURN_OFF):
|
||||
def _turn_group_off_service(service):
|
||||
def turn_group_off_service(service):
|
||||
""" Call components.turn_off for each entity_id in this group. """
|
||||
for entity_id in get_entity_ids(statemachine,
|
||||
service.data.get(
|
||||
|
@ -163,7 +163,7 @@ def setup(bus, statemachine, name, entity_ids):
|
|||
components.turn_off(bus, entity_id)
|
||||
|
||||
bus.register_service(DOMAIN, components.SERVICE_TURN_OFF,
|
||||
_turn_group_off_service)
|
||||
turn_group_off_service)
|
||||
|
||||
statemachine.set_state(group_entity_id, group_state, state_attr)
|
||||
|
||||
|
|
Loading…
Reference in New Issue