Add datetime option to input_datetime.set_datetime service (#24975)

This commit is contained in:
Phil Bruckner
2019-07-08 15:18:42 -05:00
committed by Paulus Schoutsen
parent b11171aaeb
commit 1431fd6fbd
3 changed files with 103 additions and 27 deletions

View File

@@ -4,7 +4,8 @@ import datetime
import voluptuous as vol
from homeassistant.const import ATTR_ENTITY_ID, CONF_ICON, CONF_NAME
from homeassistant.const import (
ATTR_DATE, ATTR_ENTITY_ID, ATTR_TIME, CONF_ICON, CONF_NAME)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import RestoreEntity
@@ -22,8 +23,7 @@ CONF_INITIAL = 'initial'
DEFAULT_VALUE = '1970-01-01 00:00:00'
ATTR_DATE = 'date'
ATTR_TIME = 'time'
ATTR_DATETIME = 'datetime'
SERVICE_SET_DATETIME = 'set_datetime'
@@ -31,6 +31,7 @@ SERVICE_SET_DATETIME_SCHEMA = vol.Schema({
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
vol.Optional(ATTR_DATE): cv.date,
vol.Optional(ATTR_TIME): cv.time,
vol.Optional(ATTR_DATETIME): cv.datetime,
})
@@ -77,12 +78,19 @@ async def async_setup(hass, config):
"""Handle a call to the input datetime 'set datetime' service."""
time = call.data.get(ATTR_TIME)
date = call.data.get(ATTR_DATE)
if (entity.has_date and not date) or (entity.has_time and not time):
dttm = call.data.get(ATTR_DATETIME)
# pylint: disable=too-many-boolean-expressions
if (dttm and (date or time)
or entity.has_date and not (date or dttm)
or entity.has_time and not (time or dttm)):
_LOGGER.error("Invalid service data for %s "
"input_datetime.set_datetime: %s",
entity.entity_id, str(call.data))
return
if dttm:
date = dttm.date()
time = dttm.time()
entity.async_set_datetime(date, time)
component.async_register_entity_service(

View File

@@ -3,7 +3,9 @@ set_datetime:
fields:
entity_id: {description: Entity id of the input datetime to set the new value.,
example: input_datetime.test_date_time}
date: {description: The target date the entity should be set to.,
date: {description: The target date the entity should be set to. Do not use with datetime.,
example: '"date": "2019-04-22"'}
time: {description: The target time the entity should be set to.,
time: {description: The target time the entity should be set to. Do not use with datetime.,
example: '"time": "05:30:00"'}
datetime: {description: The target date & time the entity should be set to. Do not use with date or time.,
example: '"datetime": "2019-04-22 05:30:00"'}