Methods defined inside setup make more sense

This commit is contained in:
Paulus Schoutsen 2014-01-29 22:47:50 -08:00
parent 086cf77443
commit c438f35bcd
4 changed files with 30 additions and 24 deletions

View file

@ -56,7 +56,7 @@ def setup(bus, statemachine, host):
entity = ENTITY_ID_FORMAT.format(util.slugify(device.friendly_name)) entity = ENTITY_ID_FORMAT.format(util.slugify(device.friendly_name))
if not bus.has_service(DOMAIN, components.SERVICE_TURN_OFF): 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 """ Service to exit any running app on the specified ChromeCast and
shows idle screen. Will quit all ChromeCasts if nothing specified. shows idle screen. Will quit all ChromeCasts if nothing specified.
""" """
@ -76,7 +76,7 @@ def setup(bus, statemachine, host):
pass pass
bus.register_service(DOMAIN, components.SERVICE_TURN_OFF, bus.register_service(DOMAIN, components.SERVICE_TURN_OFF,
_turn_off_service) turn_off_service)
bus.register_service(DOMAIN, "start_fireplace", bus.register_service(DOMAIN, "start_fireplace",
lambda service: lambda service:
@ -91,7 +91,7 @@ def setup(bus, statemachine, host):
pychromecast.play_youtube_video( pychromecast.play_youtube_video(
host, service.data['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. """ """ Retrieve state of Chromecast and update statemachine. """
logger.info("Updating app status") logger.info("Updating app status")
status = pychromecast.get_app_status(host) status = pychromecast.get_app_status(host)
@ -109,8 +109,8 @@ def setup(bus, statemachine, host):
else: else:
statemachine.set_state(entity, STATE_NO_APP, {ATTR_HOST: host}) 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 return True

View file

@ -48,7 +48,7 @@ def setup(bus, statemachine, light_group=None):
len(light_ids)) len(light_ids))
# pylint: disable=unused-argument # 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. """The moment sun sets we want to have all the lights on.
We will schedule to have each light start after one another We will schedule to have each light start after one another
and slowly transition in.""" 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 # Track every time sun rises so we can schedule a time-based
# pre-sun set event # 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) sun.STATE_BELOW_HORIZON, sun.STATE_ABOVE_HORIZON)
# If the sun is already above horizon # If the sun is already above horizon
# schedule the time-based pre-sun set event # schedule the time-based pre-sun set event
if sun.is_on(statemachine): 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. """ """ Function to handle tracked device state changes. """
lights_are_on = group.is_on(statemachine, light_group) 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 # Track home coming of each seperate device
for entity in device_entity_ids: 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) components.STATE_NOT_HOME, components.STATE_HOME)
# Track when all devices are gone to shut down lights # Track when all devices are gone to shut down lights
ha.track_state_change(bus, device_tracker.ENTITY_ID_ALL_DEVICES, ha.track_state_change(bus, device_tracker.ENTITY_ID_ALL_DEVICES,
_handle_device_state_change, components.STATE_HOME, check_light_on_dev_state_change,
components.STATE_NOT_HOME) components.STATE_HOME, components.STATE_NOT_HOME)
return True return True

View file

@ -71,16 +71,19 @@ class DeviceTracker(object):
self._read_known_devices_file() self._read_known_devices_file()
ha.track_time_change(bus, # Wrap it in a func instead of lambda so it can be identified in
lambda time: # the bus by its __name__ attribute.
self.update_devices( def update_device_state(time): # pylint: disable=unused-argument
device_scanner.scan_devices())) """ Triggers update of the device states. """
self.update_devices()
ha.track_time_change(bus, update_device_state)
bus.register_service(DOMAIN, bus.register_service(DOMAIN,
SERVICE_DEVICE_TRACKER_RELOAD, SERVICE_DEVICE_TRACKER_RELOAD,
lambda service: self._read_known_devices_file()) 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, group.setup(bus, statemachine, GROUP_NAME_ALL_DEVICES,
list(self.device_entity_ids)) list(self.device_entity_ids))
@ -93,10 +96,12 @@ class DeviceTracker(object):
in self.known_devices in self.known_devices
if self.known_devices[device]['track']]) 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. """ """ Update device states based on the found devices. """
self.lock.acquire() self.lock.acquire()
found_devices = found_devices or self.device_scanner.scan_devices()
now = datetime.now() now = datetime.now()
known_dev = self.known_devices known_dev = self.known_devices

View file

@ -114,7 +114,7 @@ def setup(bus, statemachine, name, entity_ids):
state_attr = {STATE_ATTR_ENTITY_IDS: entity_ids} state_attr = {STATE_ATTR_ENTITY_IDS: entity_ids}
# pylint: disable=unused-argument # 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 """ Updates the group state based on a state change by a tracked
entity. """ entity. """
@ -136,13 +136,13 @@ def setup(bus, statemachine, name, entity_ids):
statemachine.set_state(group_entity_id, group_off, state_attr) statemachine.set_state(group_entity_id, group_off, state_attr)
for entity_id in entity_ids: 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 # group.setup is called to setup each group. Only the first time will we
# register a turn_on and turn_off method for groups. # register a turn_on and turn_off method for groups.
if not bus.has_service(DOMAIN, components.SERVICE_TURN_ON): 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. """ """ Call components.turn_on for each entity_id from this group. """
for entity_id in get_entity_ids(statemachine, for entity_id in get_entity_ids(statemachine,
service.data.get( service.data.get(
@ -151,10 +151,10 @@ def setup(bus, statemachine, name, entity_ids):
components.turn_on(bus, entity_id) components.turn_on(bus, entity_id)
bus.register_service(DOMAIN, components.SERVICE_TURN_ON, 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): 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. """ """ Call components.turn_off for each entity_id in this group. """
for entity_id in get_entity_ids(statemachine, for entity_id in get_entity_ids(statemachine,
service.data.get( service.data.get(
@ -163,7 +163,7 @@ def setup(bus, statemachine, name, entity_ids):
components.turn_off(bus, entity_id) components.turn_off(bus, entity_id)
bus.register_service(DOMAIN, components.SERVICE_TURN_OFF, 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) statemachine.set_state(group_entity_id, group_state, state_attr)