mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Black
This commit is contained in:
@@ -9,8 +9,14 @@ from homeassistant.loader import bind_hass
|
||||
from homeassistant.helpers.sun import get_astral_event_next
|
||||
from homeassistant.core import HomeAssistant, callback, CALLBACK_TYPE
|
||||
from homeassistant.const import (
|
||||
ATTR_NOW, EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL,
|
||||
SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET, EVENT_CORE_CONFIG_UPDATE)
|
||||
ATTR_NOW,
|
||||
EVENT_STATE_CHANGED,
|
||||
EVENT_TIME_CHANGED,
|
||||
MATCH_ALL,
|
||||
SUN_EVENT_SUNRISE,
|
||||
SUN_EVENT_SUNSET,
|
||||
EVENT_CORE_CONFIG_UPDATE,
|
||||
)
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.async_ import run_callback_threadsafe
|
||||
|
||||
@@ -23,16 +29,18 @@ from homeassistant.util.async_ import run_callback_threadsafe
|
||||
|
||||
def threaded_listener_factory(async_factory):
|
||||
"""Convert an async event helper to a threaded one."""
|
||||
|
||||
@ft.wraps(async_factory)
|
||||
def factory(*args, **kwargs):
|
||||
"""Call async event helper safely."""
|
||||
hass = args[0]
|
||||
|
||||
if not isinstance(hass, HomeAssistant):
|
||||
raise TypeError('First parameter needs to be a hass instance')
|
||||
raise TypeError("First parameter needs to be a hass instance")
|
||||
|
||||
async_remove = run_callback_threadsafe(
|
||||
hass.loop, ft.partial(async_factory, *args, **kwargs)).result()
|
||||
hass.loop, ft.partial(async_factory, *args, **kwargs)
|
||||
).result()
|
||||
|
||||
def remove():
|
||||
"""Threadsafe removal."""
|
||||
@@ -45,8 +53,7 @@ def threaded_listener_factory(async_factory):
|
||||
|
||||
@callback
|
||||
@bind_hass
|
||||
def async_track_state_change(hass, entity_ids, action, from_state=None,
|
||||
to_state=None):
|
||||
def async_track_state_change(hass, entity_ids, action, from_state=None, to_state=None):
|
||||
"""Track specific state changes.
|
||||
|
||||
entity_ids, from_state and to_state can be string or list.
|
||||
@@ -70,22 +77,24 @@ def async_track_state_change(hass, entity_ids, action, from_state=None,
|
||||
@callback
|
||||
def state_change_listener(event):
|
||||
"""Handle specific state changes."""
|
||||
if entity_ids != MATCH_ALL and \
|
||||
event.data.get('entity_id') not in entity_ids:
|
||||
if entity_ids != MATCH_ALL and event.data.get("entity_id") not in entity_ids:
|
||||
return
|
||||
|
||||
old_state = event.data.get('old_state')
|
||||
old_state = event.data.get("old_state")
|
||||
if old_state is not None:
|
||||
old_state = old_state.state
|
||||
|
||||
new_state = event.data.get('new_state')
|
||||
new_state = event.data.get("new_state")
|
||||
if new_state is not None:
|
||||
new_state = new_state.state
|
||||
|
||||
if match_from_state(old_state) and match_to_state(new_state):
|
||||
hass.async_run_job(action, event.data.get('entity_id'),
|
||||
event.data.get('old_state'),
|
||||
event.data.get('new_state'))
|
||||
hass.async_run_job(
|
||||
action,
|
||||
event.data.get("entity_id"),
|
||||
event.data.get("old_state"),
|
||||
event.data.get("new_state"),
|
||||
)
|
||||
|
||||
return hass.bus.async_listen(EVENT_STATE_CHANGED, state_change_listener)
|
||||
|
||||
@@ -116,8 +125,8 @@ def async_track_template(hass, template, action, variables=None):
|
||||
already_triggered = False
|
||||
|
||||
return async_track_state_change(
|
||||
hass, template.extract_entities(variables),
|
||||
template_condition_listener)
|
||||
hass, template.extract_entities(variables), template_condition_listener
|
||||
)
|
||||
|
||||
|
||||
track_template = threaded_listener_factory(async_track_template)
|
||||
@@ -125,8 +134,9 @@ track_template = threaded_listener_factory(async_track_template)
|
||||
|
||||
@callback
|
||||
@bind_hass
|
||||
def async_track_same_state(hass, period, action, async_check_same_func,
|
||||
entity_ids=MATCH_ALL):
|
||||
def async_track_same_state(
|
||||
hass, period, action, async_check_same_func, entity_ids=MATCH_ALL
|
||||
):
|
||||
"""Track the state of entities for a period and run an action.
|
||||
|
||||
If async_check_func is None it use the state of orig_value.
|
||||
@@ -162,10 +172,12 @@ def async_track_same_state(hass, period, action, async_check_same_func,
|
||||
clear_listener()
|
||||
|
||||
async_remove_state_for_listener = async_track_point_in_utc_time(
|
||||
hass, state_for_listener, dt_util.utcnow() + period)
|
||||
hass, state_for_listener, dt_util.utcnow() + period
|
||||
)
|
||||
|
||||
async_remove_state_for_cancel = async_track_state_change(
|
||||
hass, entity_ids, state_for_cancel_listener)
|
||||
hass, entity_ids, state_for_cancel_listener
|
||||
)
|
||||
|
||||
return clear_listener
|
||||
|
||||
@@ -184,8 +196,7 @@ def async_track_point_in_time(hass, action, point_in_time) -> CALLBACK_TYPE:
|
||||
"""Convert passed in UTC now to local now."""
|
||||
hass.async_run_job(action, dt_util.as_local(utc_now))
|
||||
|
||||
return async_track_point_in_utc_time(hass, utc_converter,
|
||||
utc_point_in_time)
|
||||
return async_track_point_in_utc_time(hass, utc_converter, utc_point_in_time)
|
||||
|
||||
|
||||
track_point_in_time = threaded_listener_factory(async_track_point_in_time)
|
||||
@@ -193,8 +204,7 @@ track_point_in_time = threaded_listener_factory(async_track_point_in_time)
|
||||
|
||||
@callback
|
||||
@bind_hass
|
||||
def async_track_point_in_utc_time(
|
||||
hass, action, point_in_time) -> CALLBACK_TYPE:
|
||||
def async_track_point_in_utc_time(hass, action, point_in_time) -> CALLBACK_TYPE:
|
||||
"""Add a listener that fires once after a specific point in UTC time."""
|
||||
# Ensure point_in_time is UTC
|
||||
point_in_time = dt_util.as_utc(point_in_time)
|
||||
@@ -204,7 +214,7 @@ def async_track_point_in_utc_time(
|
||||
"""Listen for matching time_changed events."""
|
||||
now = event.data[ATTR_NOW]
|
||||
|
||||
if now < point_in_time or hasattr(point_in_time_listener, 'run'):
|
||||
if now < point_in_time or hasattr(point_in_time_listener, "run"):
|
||||
return
|
||||
|
||||
# Set variable so that we will never run twice.
|
||||
@@ -217,14 +227,12 @@ def async_track_point_in_utc_time(
|
||||
|
||||
hass.async_run_job(action, now)
|
||||
|
||||
async_unsub = hass.bus.async_listen(EVENT_TIME_CHANGED,
|
||||
point_in_time_listener)
|
||||
async_unsub = hass.bus.async_listen(EVENT_TIME_CHANGED, point_in_time_listener)
|
||||
|
||||
return async_unsub
|
||||
|
||||
|
||||
track_point_in_utc_time = threaded_listener_factory(
|
||||
async_track_point_in_utc_time)
|
||||
track_point_in_utc_time = threaded_listener_factory(async_track_point_in_utc_time)
|
||||
|
||||
|
||||
@callback
|
||||
@@ -232,11 +240,11 @@ track_point_in_utc_time = threaded_listener_factory(
|
||||
def async_call_later(hass, delay, action):
|
||||
"""Add a listener that is called in <delay>."""
|
||||
return async_track_point_in_utc_time(
|
||||
hass, action, dt_util.utcnow() + timedelta(seconds=delay))
|
||||
hass, action, dt_util.utcnow() + timedelta(seconds=delay)
|
||||
)
|
||||
|
||||
|
||||
call_later = threaded_listener_factory(
|
||||
async_call_later)
|
||||
call_later = threaded_listener_factory(async_call_later)
|
||||
|
||||
|
||||
@callback
|
||||
@@ -253,12 +261,10 @@ def async_track_time_interval(hass, action, interval):
|
||||
def interval_listener(now):
|
||||
"""Handle elapsed intervals."""
|
||||
nonlocal remove
|
||||
remove = async_track_point_in_utc_time(
|
||||
hass, interval_listener, next_interval())
|
||||
remove = async_track_point_in_utc_time(hass, interval_listener, next_interval())
|
||||
hass.async_run_job(action, now)
|
||||
|
||||
remove = async_track_point_in_utc_time(
|
||||
hass, interval_listener, next_interval())
|
||||
remove = async_track_point_in_utc_time(hass, interval_listener, next_interval())
|
||||
|
||||
def remove_listener():
|
||||
"""Remove interval listener."""
|
||||
@@ -287,7 +293,8 @@ class SunListener:
|
||||
assert self._unsub_config is None
|
||||
|
||||
self._unsub_config = self.hass.bus.async_listen(
|
||||
EVENT_CORE_CONFIG_UPDATE, self._handle_config_event)
|
||||
EVENT_CORE_CONFIG_UPDATE, self._handle_config_event
|
||||
)
|
||||
|
||||
self._listen_next_sun_event()
|
||||
|
||||
@@ -308,8 +315,9 @@ class SunListener:
|
||||
assert self._unsub_sun is None
|
||||
|
||||
self._unsub_sun = async_track_point_in_utc_time(
|
||||
self.hass, self._handle_sun_event,
|
||||
get_astral_event_next(self.hass, self.event, offset=self.offset)
|
||||
self.hass,
|
||||
self._handle_sun_event,
|
||||
get_astral_event_next(self.hass, self.event, offset=self.offset),
|
||||
)
|
||||
|
||||
@callback
|
||||
@@ -354,13 +362,14 @@ track_sunset = threaded_listener_factory(async_track_sunset)
|
||||
|
||||
@callback
|
||||
@bind_hass
|
||||
def async_track_utc_time_change(hass, action,
|
||||
hour=None, minute=None, second=None,
|
||||
local=False):
|
||||
def async_track_utc_time_change(
|
||||
hass, action, hour=None, minute=None, second=None, local=False
|
||||
):
|
||||
"""Add a listener that will fire if time matches a pattern."""
|
||||
# We do not have to wrap the function with time pattern matching logic
|
||||
# if no pattern given
|
||||
if all(val is None for val in (hour, minute, second)):
|
||||
|
||||
@callback
|
||||
def time_change_listener(event):
|
||||
"""Fire every time event that comes in."""
|
||||
@@ -380,8 +389,8 @@ def async_track_utc_time_change(hass, action,
|
||||
|
||||
localized_now = dt_util.as_local(now) if local else now
|
||||
next_time = dt_util.find_next_time_expression_time(
|
||||
localized_now, matching_seconds, matching_minutes,
|
||||
matching_hours)
|
||||
localized_now, matching_seconds, matching_minutes, matching_hours
|
||||
)
|
||||
|
||||
# Make sure rolling back the clock doesn't prevent the timer from
|
||||
# triggering.
|
||||
@@ -407,8 +416,7 @@ def async_track_utc_time_change(hass, action,
|
||||
# We can't use async_track_point_in_utc_time here because it would
|
||||
# break in the case that the system time abruptly jumps backwards.
|
||||
# Our custom last_now logic takes care of resolving that scenario.
|
||||
return hass.bus.async_listen(EVENT_TIME_CHANGED,
|
||||
pattern_time_change_listener)
|
||||
return hass.bus.async_listen(EVENT_TIME_CHANGED, pattern_time_change_listener)
|
||||
|
||||
|
||||
track_utc_time_change = threaded_listener_factory(async_track_utc_time_change)
|
||||
@@ -418,8 +426,7 @@ track_utc_time_change = threaded_listener_factory(async_track_utc_time_change)
|
||||
@bind_hass
|
||||
def async_track_time_change(hass, action, hour=None, minute=None, second=None):
|
||||
"""Add a listener that will fire if UTC time matches a pattern."""
|
||||
return async_track_utc_time_change(hass, action, hour, minute, second,
|
||||
local=True)
|
||||
return async_track_utc_time_change(hass, action, hour, minute, second, local=True)
|
||||
|
||||
|
||||
track_time_change = threaded_listener_factory(async_track_time_change)
|
||||
@@ -430,7 +437,7 @@ def _process_state_match(parameter):
|
||||
if parameter is None or parameter == MATCH_ALL:
|
||||
return lambda _: True
|
||||
|
||||
if isinstance(parameter, str) or not hasattr(parameter, '__iter__'):
|
||||
if isinstance(parameter, str) or not hasattr(parameter, "__iter__"):
|
||||
return lambda state: state == parameter
|
||||
|
||||
parameter = tuple(parameter)
|
||||
|
||||
Reference in New Issue
Block a user