Add timestamp option for input_datetime.set_datetime (#39121)
This commit is contained in:
1 parent
2568932c1c
commit
4ff376cdd6
3 files changed
+96
-41
No files matched your search
@@ -37,8 +37,34 @@ DEFAULT_DATE = datetime.date(1970, 1, 1)
|
||||
DEFAULT_TIME = datetime.time(0, 0, 0)
|
||||
|
||||
ATTR_DATETIME = "datetime"
|
||||
ATTR_TIMESTAMP = "timestamp"
|
||||
|
||||
|
||||
def validate_set_datetime_attrs(config):
|
||||
"""Validate set_datetime service attributes."""
|
||||
has_date_or_time_attr = any(key in config for key in (ATTR_DATE, ATTR_TIME))
|
||||
if (
|
||||
sum([has_date_or_time_attr, ATTR_DATETIME in config, ATTR_TIMESTAMP in config])
|
||||
> 1
|
||||
):
|
||||
raise vol.Invalid(f"Cannot use together: {', '.join(config.keys())}")
|
||||
return config
|
||||
|
||||
|
||||
SERVICE_SET_DATETIME = "set_datetime"
|
||||
SERVICE_SET_DATETIME_SCHEMA = vol.All(
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Optional(ATTR_DATE): cv.date,
|
||||
vol.Optional(ATTR_TIME): cv.time,
|
||||
vol.Optional(ATTR_DATETIME): cv.datetime,
|
||||
vol.Optional(ATTR_TIMESTAMP): vol.Coerce(float),
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
),
|
||||
cv.has_at_least_one_key(ATTR_DATE, ATTR_TIME, ATTR_DATETIME, ATTR_TIMESTAMP),
|
||||
validate_set_datetime_attrs,
|
||||
)
|
||||
STORAGE_KEY = DOMAIN
|
||||
STORAGE_VERSION = 1
|
||||
|
||||
@@ -138,37 +164,29 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
||||
|
||||
async def async_set_datetime_service(entity, call):
|
||||
"""Handle a call to the input datetime 'set datetime' service."""
|
||||
time = call.data.get(ATTR_TIME)
|
||||
date = call.data.get(ATTR_DATE)
|
||||
time = call.data.get(ATTR_TIME)
|
||||
dttm = call.data.get(ATTR_DATETIME)
|
||||
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
|
||||
tmsp = call.data.get(ATTR_TIMESTAMP)
|
||||
|
||||
if tmsp:
|
||||
dttm = dt_util.as_local(dt_util.utc_from_timestamp(tmsp)).replace(
|
||||
tzinfo=None
|
||||
)
|
||||
if dttm:
|
||||
date = dttm.date()
|
||||
time = dttm.time()
|
||||
if not entity.has_date:
|
||||
date = None
|
||||
if not entity.has_time:
|
||||
time = None
|
||||
if not date and not time:
|
||||
raise vol.Invalid("Nothing to set")
|
||||
|
||||
entity.async_set_datetime(date, time)
|
||||
|
||||
component.async_register_entity_service(
|
||||
SERVICE_SET_DATETIME,
|
||||
{
|
||||
vol.Optional(ATTR_DATE): cv.date,
|
||||
vol.Optional(ATTR_TIME): cv.time,
|
||||
vol.Optional(ATTR_DATETIME): cv.datetime,
|
||||
},
|
||||
async_set_datetime_service,
|
||||
SERVICE_SET_DATETIME, SERVICE_SET_DATETIME_SCHEMA, async_set_datetime_service
|
||||
)
|
||||
|
||||
return True
|
||||
@@ -338,17 +356,11 @@ class InputDatetime(RestoreEntity):
|
||||
@callback
|
||||
def async_set_datetime(self, date_val, time_val):
|
||||
"""Set a new date / time."""
|
||||
if self.has_date and self.has_time and date_val and time_val:
|
||||
self._current_datetime = datetime.datetime.combine(date_val, time_val)
|
||||
elif self.has_date and not self.has_time and date_val:
|
||||
self._current_datetime = datetime.datetime.combine(
|
||||
date_val, self._current_datetime.time()
|
||||
)
|
||||
if self.has_time and not self.has_date and time_val:
|
||||
self._current_datetime = datetime.datetime.combine(
|
||||
self._current_datetime.date(), time_val
|
||||
)
|
||||
|
||||
if not date_val:
|
||||
date_val = self._current_datetime.date()
|
||||
if not time_val:
|
||||
time_val = self._current_datetime.time()
|
||||
self._current_datetime = datetime.datetime.combine(date_val, time_val)
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_update_config(self, config: typing.Dict) -> None:
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
set_datetime:
|
||||
description: This can be used to dynamically set the date and/or time.
|
||||
description: This can be used to dynamically set the date and/or time. Use date/time, datetime or timestamp.
|
||||
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. Do not use with datetime.
|
||||
description: The target date the entity should be set to.
|
||||
example: '"2019-04-20"'
|
||||
time:
|
||||
description: The target time the entity should be set to. Do not use with datetime.
|
||||
description: The target time the entity should be set to.
|
||||
example: '"05:04:20"'
|
||||
datetime:
|
||||
description: The target date & time the entity should be set to. Do not use with date or time.
|
||||
description: The target date & time the entity should be set to.
|
||||
example: '"2019-04-20 05:04:20"'
|
||||
timestamp:
|
||||
description: The target date & time the entity should be set to as expressed by a UNIX timestamp.
|
||||
example: 1598027400
|
||||
|
||||
reload:
|
||||
description: Reload the input_datetime configuration.
|
||||
Reference in new issue
Block a user