mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Merge pull request #1503 from fabaff/pep257-automation
PEP257 - Automation
This commit is contained in:
commit
ba0a25aef6
@ -1,6 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
|
Allow to setup simple automation rules via the config file.
|
||||||
Allows to setup simple automation rules via the config file.
|
|
||||||
|
|
||||||
For more details about this component, please refer to the documentation at
|
For more details about this component, please refer to the documentation at
|
||||||
https://home-assistant.io/components/automation/
|
https://home-assistant.io/components/automation/
|
||||||
@ -39,20 +38,20 @@ def setup(hass, config):
|
|||||||
found = 1
|
found = 1
|
||||||
|
|
||||||
while config_key in config:
|
while config_key in config:
|
||||||
# check for one block syntax
|
# Check for one block syntax
|
||||||
if isinstance(config[config_key], dict):
|
if isinstance(config[config_key], dict):
|
||||||
config_block = _migrate_old_config(config[config_key])
|
config_block = _migrate_old_config(config[config_key])
|
||||||
name = config_block.get(CONF_ALIAS, config_key)
|
name = config_block.get(CONF_ALIAS, config_key)
|
||||||
_setup_automation(hass, config_block, name, config)
|
_setup_automation(hass, config_block, name, config)
|
||||||
|
|
||||||
# check for multiple block syntax
|
# Check for multiple block syntax
|
||||||
elif isinstance(config[config_key], list):
|
elif isinstance(config[config_key], list):
|
||||||
for list_no, config_block in enumerate(config[config_key]):
|
for list_no, config_block in enumerate(config[config_key]):
|
||||||
name = config_block.get(CONF_ALIAS,
|
name = config_block.get(CONF_ALIAS,
|
||||||
"{}, {}".format(config_key, list_no))
|
"{}, {}".format(config_key, list_no))
|
||||||
_setup_automation(hass, config_block, name, config)
|
_setup_automation(hass, config_block, name, config)
|
||||||
|
|
||||||
# any scalar value is incorrect
|
# Any scalar value is incorrect
|
||||||
else:
|
else:
|
||||||
_LOGGER.error('Error in config in section %s.', config_key)
|
_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):
|
def _process_if(hass, config, p_config, action):
|
||||||
"""Processes if checks."""
|
"""Process if checks."""
|
||||||
cond_type = p_config.get(CONF_CONDITION_TYPE,
|
cond_type = p_config.get(CONF_CONDITION_TYPE,
|
||||||
DEFAULT_CONDITION_TYPE).lower()
|
DEFAULT_CONDITION_TYPE).lower()
|
||||||
|
|
||||||
|
@ -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
|
For more details about this automation rule, please refer to the documentation
|
||||||
at https://home-assistant.io/components/automation/#event-trigger
|
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)
|
event_data = config.get(CONF_EVENT_DATA)
|
||||||
|
|
||||||
def handle_event(event):
|
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
|
if not event_data or all(val == event.data.get(key) for key, val
|
||||||
in event_data.items()):
|
in event_data.items()):
|
||||||
action()
|
action()
|
||||||
|
@ -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
|
For more details about this automation rule, please refer to the documentation
|
||||||
at https://home-assistant.io/components/automation/#mqtt-trigger
|
at https://home-assistant.io/components/automation/#mqtt-trigger
|
||||||
@ -25,7 +25,7 @@ def trigger(hass, config, action):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def mqtt_automation_listener(msg_topic, msg_payload, qos):
|
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:
|
if payload is None or payload == msg_payload:
|
||||||
action()
|
action()
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
homeassistant.components.automation.numeric_state
|
Offer numeric state listening automation rules.
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
Offers numeric state listening automation rules.
|
|
||||||
|
|
||||||
For more details about this automation rule, please refer to the documentation
|
For more details about this automation rule, please refer to the documentation
|
||||||
at https://home-assistant.io/components/automation/#numeric-state-trigger
|
at https://home-assistant.io/components/automation/#numeric-state-trigger
|
||||||
@ -50,7 +48,7 @@ def trigger(hass, config, action):
|
|||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def state_automation_listener(entity, from_s, to_s):
|
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
|
# Fire action if we go from outside range into range
|
||||||
if _in_range(above, below, renderer(to_s)) and \
|
if _in_range(above, below, renderer(to_s)) and \
|
||||||
(from_s is None or not _in_range(above, below, renderer(from_s))):
|
(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):
|
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)
|
entity_id = config.get(CONF_ENTITY_ID)
|
||||||
|
|
||||||
if entity_id is None:
|
if entity_id is None:
|
||||||
@ -91,7 +89,7 @@ def if_action(hass, config):
|
|||||||
|
|
||||||
|
|
||||||
def _in_range(range_start, range_end, value):
|
def _in_range(range_start, range_end, value):
|
||||||
"""Checks if value is inside the range."""
|
"""Check if value is inside the range."""
|
||||||
try:
|
try:
|
||||||
value = float(value)
|
value = float(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -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
|
For more details about this automation rule, please refer to the documentation
|
||||||
at https://home-assistant.io/components/automation/#state-trigger
|
at https://home-assistant.io/components/automation/#state-trigger
|
||||||
@ -70,18 +70,15 @@ def trigger(hass, config, action):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def state_automation_listener(entity, from_s, to_s):
|
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):
|
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(
|
hass.bus.remove_listener(
|
||||||
EVENT_STATE_CHANGED, for_state_listener)
|
EVENT_STATE_CHANGED, for_state_listener)
|
||||||
action()
|
action()
|
||||||
|
|
||||||
def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
|
def state_for_cancel_listener(entity, inner_from_s, inner_to_s):
|
||||||
"""
|
"""Fire on changes and cancel for listener if changed."""
|
||||||
Fires on state changes and cancels for listener if state changed.
|
|
||||||
"""
|
|
||||||
if inner_to_s == to_s:
|
if inner_to_s == to_s:
|
||||||
return
|
return
|
||||||
hass.bus.remove_listener(EVENT_TIME_CHANGED, for_time_listener)
|
hass.bus.remove_listener(EVENT_TIME_CHANGED, for_time_listener)
|
||||||
@ -105,7 +102,7 @@ def trigger(hass, config, action):
|
|||||||
|
|
||||||
|
|
||||||
def if_action(hass, config):
|
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)
|
entity_id = config.get(CONF_ENTITY_ID)
|
||||||
state = config.get(CONF_STATE)
|
state = config.get(CONF_STATE)
|
||||||
|
|
||||||
|
@ -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
|
For more details about this automation rule, please refer to the documentation
|
||||||
at https://home-assistant.io/components/automation/#sun-trigger
|
at https://home-assistant.io/components/automation/#sun-trigger
|
||||||
@ -53,7 +53,7 @@ def trigger(hass, config, action):
|
|||||||
|
|
||||||
|
|
||||||
def if_action(hass, config):
|
def if_action(hass, config):
|
||||||
"""Wraps action method with sun based condition."""
|
"""Wrap action method with sun based condition."""
|
||||||
before = config.get(CONF_BEFORE)
|
before = config.get(CONF_BEFORE)
|
||||||
after = config.get(CONF_AFTER)
|
after = config.get(CONF_AFTER)
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Offers template automation rules.
|
Offer template automation rules.
|
||||||
|
|
||||||
For more details about this automation rule, please refer to the documentation
|
For more details about this automation rule, please refer to the documentation
|
||||||
at https://home-assistant.io/components/automation/#template-trigger
|
at https://home-assistant.io/components/automation/#template-trigger
|
||||||
@ -25,7 +25,7 @@ def trigger(hass, config, action):
|
|||||||
already_triggered = False
|
already_triggered = False
|
||||||
|
|
||||||
def event_listener(event):
|
def event_listener(event):
|
||||||
"""Listens for state changes and calls action."""
|
"""Listen for state changes and calls action."""
|
||||||
nonlocal already_triggered
|
nonlocal already_triggered
|
||||||
template_result = _check_template(hass, value_template)
|
template_result = _check_template(hass, value_template)
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ def trigger(hass, config, action):
|
|||||||
|
|
||||||
|
|
||||||
def if_action(hass, config):
|
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)
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
||||||
|
|
||||||
if value_template is None:
|
if value_template is None:
|
||||||
@ -52,7 +52,7 @@ def if_action(hass, config):
|
|||||||
|
|
||||||
|
|
||||||
def _check_template(hass, value_template):
|
def _check_template(hass, value_template):
|
||||||
"""Checks if result of template is true."""
|
"""Check if result of template is true."""
|
||||||
try:
|
try:
|
||||||
value = template.render(hass, value_template, {})
|
value = template.render(hass, value_template, {})
|
||||||
except TemplateError:
|
except TemplateError:
|
||||||
|
@ -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
|
For more details about this automation rule, please refer to the documentation
|
||||||
at https://home-assistant.io/components/automation/#time-trigger
|
at https://home-assistant.io/components/automation/#time-trigger
|
||||||
@ -40,7 +40,7 @@ def trigger(hass, config, action):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def time_automation_listener(now):
|
def time_automation_listener(now):
|
||||||
"""Listens for time changes and calls action."""
|
"""Listen for time changes and calls action."""
|
||||||
action()
|
action()
|
||||||
|
|
||||||
track_time_change(hass, time_automation_listener,
|
track_time_change(hass, time_automation_listener,
|
||||||
@ -50,7 +50,7 @@ def trigger(hass, config, action):
|
|||||||
|
|
||||||
|
|
||||||
def if_action(hass, config):
|
def if_action(hass, config):
|
||||||
"""Wraps action method with time based condition."""
|
"""Wrap action method with time based condition."""
|
||||||
before = config.get(CONF_BEFORE)
|
before = config.get(CONF_BEFORE)
|
||||||
after = config.get(CONF_AFTER)
|
after = config.get(CONF_AFTER)
|
||||||
weekday = config.get(CONF_WEEKDAY)
|
weekday = config.get(CONF_WEEKDAY)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Offers zone automation rules.
|
Offer zone automation rules.
|
||||||
|
|
||||||
For more details about this automation rule, please refer to the documentation
|
For more details about this automation rule, please refer to the documentation
|
||||||
at https://home-assistant.io/components/automation/#zone-trigger
|
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)
|
event = config.get(CONF_EVENT, DEFAULT_EVENT)
|
||||||
|
|
||||||
def zone_automation_listener(entity, from_s, to_s):
|
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),
|
if from_s and None in (from_s.attributes.get(ATTR_LATITUDE),
|
||||||
from_s.attributes.get(ATTR_LONGITUDE)) or \
|
from_s.attributes.get(ATTR_LONGITUDE)) or \
|
||||||
None in (to_s.attributes.get(ATTR_LATITUDE),
|
None in (to_s.attributes.get(ATTR_LATITUDE),
|
||||||
@ -55,7 +55,7 @@ def trigger(hass, config, action):
|
|||||||
|
|
||||||
|
|
||||||
def if_action(hass, config):
|
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)
|
entity_id = config.get(CONF_ENTITY_ID)
|
||||||
zone_entity_id = config.get(CONF_ZONE)
|
zone_entity_id = config.get(CONF_ZONE)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user