-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split time_pattern triggers from time trigger (#19825)
* Split interval triggers from time trigger * Default smaller interval units to zero * Rename interval to schedule * Rename schedule to time_pattern
- Loading branch information
Showing
4 changed files
with
277 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Offer time listening automation rules. | ||
For more details about this automation rule, please refer to the documentation | ||
at https://home-assistant.io/docs/automation/trigger/#time-trigger | ||
""" | ||
import logging | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.core import callback | ||
from homeassistant.const import CONF_PLATFORM | ||
from homeassistant.helpers import config_validation as cv | ||
from homeassistant.helpers.event import async_track_time_change | ||
|
||
CONF_HOURS = 'hours' | ||
CONF_MINUTES = 'minutes' | ||
CONF_SECONDS = 'seconds' | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
TRIGGER_SCHEMA = vol.All(vol.Schema({ | ||
vol.Required(CONF_PLATFORM): 'time_pattern', | ||
CONF_HOURS: vol.Any(vol.Coerce(int), vol.Coerce(str)), | ||
CONF_MINUTES: vol.Any(vol.Coerce(int), vol.Coerce(str)), | ||
CONF_SECONDS: vol.Any(vol.Coerce(int), vol.Coerce(str)), | ||
}), cv.has_at_least_one_key(CONF_HOURS, CONF_MINUTES, CONF_SECONDS)) | ||
|
||
|
||
async def async_trigger(hass, config, action, automation_info): | ||
"""Listen for state changes based on configuration.""" | ||
hours = config.get(CONF_HOURS) | ||
minutes = config.get(CONF_MINUTES) | ||
seconds = config.get(CONF_SECONDS) | ||
|
||
# If larger units are specified, default the smaller units to zero | ||
if minutes is None and hours is not None: | ||
minutes = 0 | ||
if seconds is None and minutes is not None: | ||
seconds = 0 | ||
|
||
@callback | ||
def time_automation_listener(now): | ||
"""Listen for time changes and calls action.""" | ||
hass.async_run_job(action, { | ||
'trigger': { | ||
'platform': 'time_pattern', | ||
'now': now, | ||
}, | ||
}) | ||
|
||
return async_track_time_change(hass, time_automation_listener, | ||
hour=hours, minute=minutes, second=seconds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.