Merge pull request #1503 from fabaff/pep257-automation

PEP257 - Automation
This commit is contained in:
Paulus Schoutsen 2016-03-07 17:09:11 -08:00
commit ba0a25aef6
9 changed files with 30 additions and 36 deletions

View File

@ -1,6 +1,5 @@
"""
Allows to setup simple automation rules via the config file.
Allow to setup simple automation rules via the config file.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/automation/
@ -39,20 +38,20 @@ def setup(hass, config):
found = 1
while config_key in config:
# check for one block syntax
# Check for one block syntax
if isinstance(config[config_key], dict):
config_block = _migrate_old_config(config[config_key])
name = config_block.get(CONF_ALIAS, config_key)
_setup_automation(hass, config_block, name, config)
# check for multiple block syntax
# Check for multiple block syntax
elif isinstance(config[config_key], list):
for list_no, config_block in enumerate(config[config_key]):
name = config_block.get(CONF_ALIAS,
"{}, {}".format(config_key, list_no))
_setup_automation(hass, config_block, name, config)
# any scalar value is incorrect
# Any scalar value is incorrect
else:
_LOGGER.error('Error in config in section %s.', config_key)
@ -131,7 +130,7 @@ def _migrate_old_config(config):
def _process_if(hass, config, p_config, action):
"""Processes if checks."""
"""Process if checks."""
cond_type = p_config.get(CONF_CONDITION_TYPE,
DEFAULT_CONDITION_TYPE).lower()

View File

@ -1,5 +1,5 @@
"""
Offers event listening automation rules.
Offer event listening automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#event-trigger
@ -23,7 +23,7 @@ def trigger(hass, config, action):
event_data = config.get(CONF_EVENT_DATA)
def handle_event(event):
"""Listens for events and calls the action when data matches."""
"""Listen for events and calls the action when data matches."""
if not event_data or all(val == event.data.get(key) for key, val
in event_data.items()):
action()

View File

@ -1,5 +1,5 @@
"""
Offers MQTT listening automation rules.
Offer MQTT listening automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#mqtt-trigger
@ -25,7 +25,7 @@ def trigger(hass, config, action):
return False
def mqtt_automation_listener(msg_topic, msg_payload, qos):
"""Listens for MQTT messages."""
"""Listen for MQTT messages."""
if payload is None or payload == msg_payload:
action()

View File

@ -1,7 +1,5 @@
"""
homeassistant.components.automation.numeric_state
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Offers numeric state listening automation rules.
Offer numeric state listening automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#numeric-state-trigger
@ -50,7 +48,7 @@ def trigger(hass, config, action):
# pylint: disable=unused-argument
def state_automation_listener(entity, from_s, to_s):
"""Listens for state changes and calls action."""
"""Listen for state changes and calls action."""
# Fire action if we go from outside range into range
if _in_range(above, below, renderer(to_s)) and \
(from_s is None or not _in_range(above, below, renderer(from_s))):
@ -63,7 +61,7 @@ def trigger(hass, config, action):
def if_action(hass, config):
"""Wraps action method with state based condition."""
"""Wrap action method with state based condition."""
entity_id = config.get(CONF_ENTITY_ID)
if entity_id is None:
@ -91,7 +89,7 @@ def if_action(hass, config):
def _in_range(range_start, range_end, value):
"""Checks if value is inside the range."""
"""Check if value is inside the range."""
try:
value = float(value)
except ValueError:

View File

@ -1,5 +1,5 @@
"""
Offers state listening automation rules.
Offer state listening automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#state-trigger
@ -70,18 +70,15 @@ def trigger(hass, config, action):
return None
def state_automation_listener(entity, from_s, to_s):
"""Listens for state changes and calls action."""
"""Listen for state changes and calls action."""
def state_for_listener(now):
"""Fires on state changes after a delay and calls action."""
"""Fire on state changes after a delay and calls action."""
hass.bus.remove_listener(
EVENT_STATE_CHANGED, for_state_listener)
action()
def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
"""
Fires on state changes and cancels for listener if state changed.
"""
"""Fire on changes and cancel for listener if changed."""
if inner_to_s == to_s:
return
hass.bus.remove_listener(EVENT_TIME_CHANGED, for_time_listener)
@ -105,7 +102,7 @@ def trigger(hass, config, action):
def if_action(hass, config):
"""Wraps action method with state based condition."""
"""Wrap action method with state based condition."""
entity_id = config.get(CONF_ENTITY_ID)
state = config.get(CONF_STATE)

View File

@ -1,5 +1,5 @@
"""
Offers sun based automation rules.
Offer sun based automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#sun-trigger
@ -53,7 +53,7 @@ def trigger(hass, config, action):
def if_action(hass, config):
"""Wraps action method with sun based condition."""
"""Wrap action method with sun based condition."""
before = config.get(CONF_BEFORE)
after = config.get(CONF_AFTER)

View File

@ -1,5 +1,5 @@
"""
Offers template automation rules.
Offer template automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#template-trigger
@ -25,7 +25,7 @@ def trigger(hass, config, action):
already_triggered = False
def event_listener(event):
"""Listens for state changes and calls action."""
"""Listen for state changes and calls action."""
nonlocal already_triggered
template_result = _check_template(hass, value_template)
@ -41,7 +41,7 @@ def trigger(hass, config, action):
def if_action(hass, config):
"""Wraps action method with state based condition."""
"""Wrap action method with state based condition."""
value_template = config.get(CONF_VALUE_TEMPLATE)
if value_template is None:
@ -52,7 +52,7 @@ def if_action(hass, config):
def _check_template(hass, value_template):
"""Checks if result of template is true."""
"""Check if result of template is true."""
try:
value = template.render(hass, value_template, {})
except TemplateError:

View File

@ -1,5 +1,5 @@
"""
Offers time listening automation rules.
Offer time listening automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#time-trigger
@ -40,7 +40,7 @@ def trigger(hass, config, action):
return False
def time_automation_listener(now):
"""Listens for time changes and calls action."""
"""Listen for time changes and calls action."""
action()
track_time_change(hass, time_automation_listener,
@ -50,7 +50,7 @@ def trigger(hass, config, action):
def if_action(hass, config):
"""Wraps action method with time based condition."""
"""Wrap action method with time based condition."""
before = config.get(CONF_BEFORE)
after = config.get(CONF_AFTER)
weekday = config.get(CONF_WEEKDAY)

View File

@ -1,5 +1,5 @@
"""
Offers zone automation rules.
Offer zone automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#zone-trigger
@ -33,7 +33,7 @@ def trigger(hass, config, action):
event = config.get(CONF_EVENT, DEFAULT_EVENT)
def zone_automation_listener(entity, from_s, to_s):
"""Listens for state changes and calls action."""
"""Listen for state changes and calls action."""
if from_s and None in (from_s.attributes.get(ATTR_LATITUDE),
from_s.attributes.get(ATTR_LONGITUDE)) or \
None in (to_s.attributes.get(ATTR_LATITUDE),
@ -55,7 +55,7 @@ def trigger(hass, config, action):
def if_action(hass, config):
"""Wraps action method with zone based condition."""
"""Wrap action method with zone based condition."""
entity_id = config.get(CONF_ENTITY_ID)
zone_entity_id = config.get(CONF_ZONE)