Make input_datetime better handle timezones (#43396)

* Make input_datetime better handle timezones

* Improve test coverage

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Revert change to time format

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Paulus Schoutsen 2020-11-26 20:20:10 +01:00 committed by GitHub
parent f3033ec01d
commit edf70e9f06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 227 additions and 142 deletions

View File

@ -1,5 +1,5 @@
"""Support to select a date and/or a time."""
import datetime
import datetime as py_datetime
import logging
import typing
@ -33,12 +33,16 @@ CONF_HAS_TIME = "has_time"
CONF_INITIAL = "initial"
DEFAULT_VALUE = "1970-01-01 00:00:00"
DEFAULT_DATE = datetime.date(1970, 1, 1)
DEFAULT_TIME = datetime.time(0, 0, 0)
DEFAULT_DATE = py_datetime.date(1970, 1, 1)
DEFAULT_TIME = py_datetime.time(0, 0, 0)
ATTR_DATETIME = "datetime"
ATTR_TIMESTAMP = "timestamp"
FMT_DATE = "%Y-%m-%d"
FMT_TIME = "%H:%M:%S"
FMT_DATETIME = f"{FMT_DATE} {FMT_TIME}"
def validate_set_datetime_attrs(config):
"""Validate set_datetime service attributes."""
@ -51,20 +55,6 @@ def validate_set_datetime_attrs(config):
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
@ -162,31 +152,24 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
schema=RELOAD_SERVICE_SCHEMA,
)
async def async_set_datetime_service(entity, call):
"""Handle a call to the input datetime 'set datetime' service."""
date = call.data.get(ATTR_DATE)
time = call.data.get(ATTR_TIME)
dttm = call.data.get(ATTR_DATETIME)
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, SERVICE_SET_DATETIME_SCHEMA, async_set_datetime_service
"set_datetime",
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,
),
"async_set_datetime",
)
return True
@ -221,16 +204,31 @@ class InputDatetime(RestoreEntity):
self._config = config
self.editable = True
self._current_datetime = None
initial = config.get(CONF_INITIAL)
if initial:
if self.has_date and self.has_time:
self._current_datetime = dt_util.parse_datetime(initial)
elif self.has_date:
date = dt_util.parse_date(initial)
self._current_datetime = datetime.datetime.combine(date, DEFAULT_TIME)
else:
time = dt_util.parse_time(initial)
self._current_datetime = datetime.datetime.combine(DEFAULT_DATE, time)
if not initial:
return
if self.has_date and self.has_time:
current_datetime = dt_util.parse_datetime(initial)
elif self.has_date:
date = dt_util.parse_date(initial)
current_datetime = py_datetime.datetime.combine(date, DEFAULT_TIME)
else:
time = dt_util.parse_time(initial)
current_datetime = py_datetime.datetime.combine(DEFAULT_DATE, time)
# If the user passed in an initial value with a timezone, convert it to right tz
if current_datetime.tzinfo is not None:
self._current_datetime = current_datetime.astimezone(
dt_util.DEFAULT_TIME_ZONE
)
else:
self._current_datetime = dt_util.DEFAULT_TIME_ZONE.localize(
current_datetime
)
@classmethod
def from_yaml(cls, config: typing.Dict) -> "InputDatetime":
@ -257,21 +255,27 @@ class InputDatetime(RestoreEntity):
if self.has_date and self.has_time:
date_time = dt_util.parse_datetime(old_state.state)
if date_time is None:
self._current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
return
self._current_datetime = date_time
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
else:
current_datetime = date_time
elif self.has_date:
date = dt_util.parse_date(old_state.state)
if date is None:
self._current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
return
self._current_datetime = datetime.datetime.combine(date, DEFAULT_TIME)
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
else:
current_datetime = py_datetime.datetime.combine(date, DEFAULT_TIME)
else:
time = dt_util.parse_time(old_state.state)
if time is None:
self._current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
return
self._current_datetime = datetime.datetime.combine(DEFAULT_DATE, time)
current_datetime = dt_util.parse_datetime(DEFAULT_VALUE)
else:
current_datetime = py_datetime.datetime.combine(DEFAULT_DATE, time)
self._current_datetime = current_datetime.replace(
tzinfo=dt_util.DEFAULT_TIME_ZONE
)
@property
def should_poll(self):
@ -305,10 +309,12 @@ class InputDatetime(RestoreEntity):
return None
if self.has_date and self.has_time:
return self._current_datetime
return self._current_datetime.strftime(FMT_DATETIME)
if self.has_date:
return self._current_datetime.date()
return self._current_datetime.time()
return self._current_datetime.strftime(FMT_DATE)
return self._current_datetime.strftime(FMT_TIME)
@property
def state_attributes(self):
@ -338,11 +344,13 @@ class InputDatetime(RestoreEntity):
+ self._current_datetime.minute * 60
+ self._current_datetime.second
)
elif not self.has_time:
extended = datetime.datetime.combine(
self._current_datetime, datetime.time(0, 0)
extended = py_datetime.datetime.combine(
self._current_datetime, py_datetime.time(0, 0)
)
attrs["timestamp"] = extended.timestamp()
else:
attrs["timestamp"] = self._current_datetime.timestamp()
@ -354,13 +362,35 @@ class InputDatetime(RestoreEntity):
return self._config[CONF_ID]
@callback
def async_set_datetime(self, date_val, time_val):
def async_set_datetime(self, date=None, time=None, datetime=None, timestamp=None):
"""Set a new date / time."""
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)
if timestamp:
datetime = dt_util.as_local(dt_util.utc_from_timestamp(timestamp)).replace(
tzinfo=None
)
if datetime:
date = datetime.date()
time = datetime.time()
if not self.has_date:
date = None
if not self.has_time:
time = None
if not date and not time:
raise vol.Invalid("Nothing to set")
if not date:
date = self._current_datetime.date()
if not time:
time = self._current_datetime.time()
self._current_datetime = py_datetime.datetime.combine(date, time).replace(
tzinfo=dt_util.DEFAULT_TIME_ZONE
)
self.async_write_ha_state()
async def async_update_config(self, config: typing.Dict) -> None:

View File

@ -8,15 +8,7 @@ from homeassistant.core import Context, State
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.util import dt as dt_util
from . import (
ATTR_DATE,
ATTR_DATETIME,
ATTR_TIME,
CONF_HAS_DATE,
CONF_HAS_TIME,
DOMAIN,
SERVICE_SET_DATETIME,
)
from . import ATTR_DATE, ATTR_DATETIME, ATTR_TIME, CONF_HAS_DATE, CONF_HAS_TIME, DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -53,22 +45,13 @@ async def _async_reproduce_state(
_LOGGER.warning("Unable to find entity %s", state.entity_id)
return
has_time = cur_state.attributes.get(CONF_HAS_TIME)
has_date = cur_state.attributes.get(CONF_HAS_DATE)
if not (
(
is_valid_datetime(state.state)
and cur_state.attributes.get(CONF_HAS_DATE)
and cur_state.attributes.get(CONF_HAS_TIME)
)
or (
is_valid_date(state.state)
and cur_state.attributes.get(CONF_HAS_DATE)
and not cur_state.attributes.get(CONF_HAS_TIME)
)
or (
is_valid_time(state.state)
and cur_state.attributes.get(CONF_HAS_TIME)
and not cur_state.attributes.get(CONF_HAS_DATE)
)
(is_valid_datetime(state.state) and has_date and has_time)
or (is_valid_date(state.state) and has_date and not has_time)
or (is_valid_time(state.state) and has_time and not has_date)
):
_LOGGER.warning(
"Invalid state specified for %s: %s", state.entity_id, state.state
@ -79,24 +62,17 @@ async def _async_reproduce_state(
if cur_state.state == state.state:
return
service = SERVICE_SET_DATETIME
service_data = {ATTR_ENTITY_ID: state.entity_id}
has_time = cur_state.attributes.get(CONF_HAS_TIME)
has_date = cur_state.attributes.get(CONF_HAS_DATE)
if has_time and has_date:
service_data[ATTR_DATETIME] = state.state
elif has_time:
service_data[ATTR_TIME] = state.state
elif has_date:
service_data[ATTR_DATE] = state.state
else:
_LOGGER.warning("input_datetime needs either has_date or has_time or both")
return
await hass.services.async_call(
DOMAIN, service, service_data, context=context, blocking=True
DOMAIN, "set_datetime", service_data, context=context, blocking=True
)

View File

@ -419,7 +419,9 @@ class HomeAssistant:
self._track_task = False
@callback
def async_run_hass_job(self, hassjob: HassJob, *args: Any) -> None:
def async_run_hass_job(
self, hassjob: HassJob, *args: Any
) -> Optional[asyncio.Future]:
"""Run a HassJob from within the event loop.
This method must be run in the event loop.
@ -429,13 +431,14 @@ class HomeAssistant:
"""
if hassjob.job_type == HassJobType.Callback:
hassjob.target(*args)
else:
self.async_add_hass_job(hassjob, *args)
return None
return self.async_add_hass_job(hassjob, *args)
@callback
def async_run_job(
self, target: Callable[..., Union[None, Awaitable]], *args: Any
) -> None:
) -> Optional[asyncio.Future]:
"""Run a job from within the event loop.
This method must be run in the event loop.
@ -444,10 +447,9 @@ class HomeAssistant:
args: parameters for method to call.
"""
if asyncio.iscoroutine(target):
self.async_create_task(cast(Coroutine, target))
return
return self.async_create_task(cast(Coroutine, target))
self.async_run_hass_job(HassJob(target), *args)
return self.async_run_hass_job(HassJob(target), *args)
def block_till_done(self) -> None:
"""Block until all pending work is done."""

View File

@ -192,7 +192,7 @@ class EntityComponent:
self,
name: str,
schema: Union[Dict[str, Any], vol.Schema],
func: str,
func: Union[str, Callable[..., Any]],
required_features: Optional[List[int]] = None,
) -> None:
"""Register an entity service."""

View File

@ -527,9 +527,9 @@ async def _handle_entity_call(
entity.async_set_context(context)
if isinstance(func, str):
result = hass.async_add_job(partial(getattr(entity, func), **data)) # type: ignore
result = hass.async_run_job(partial(getattr(entity, func), **data)) # type: ignore
else:
result = hass.async_add_job(func, entity, data)
result = hass.async_run_job(func, entity, data)
# Guard because callback functions do not return a task when passed to async_add_job.
if result is not None:

View File

@ -108,6 +108,9 @@ def start_of_local_day(
date: dt.date = now().date()
elif isinstance(dt_or_d, dt.datetime):
date = dt_or_d.date()
else:
date = dt_or_d
return DEFAULT_TIME_ZONE.localize( # type: ignore
dt.datetime.combine(date, dt.time())
)

View File

@ -16,10 +16,13 @@ from homeassistant.components.input_datetime import (
CONF_ID,
CONF_INITIAL,
CONF_NAME,
CONFIG_SCHEMA,
DEFAULT_TIME,
DOMAIN,
FMT_DATE,
FMT_DATETIME,
FMT_TIME,
SERVICE_RELOAD,
SERVICE_SET_DATETIME,
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, ATTR_NAME
from homeassistant.core import Context, CoreState, State
@ -35,6 +38,8 @@ INITIAL_DATE = "2020-01-10"
INITIAL_TIME = "23:45:56"
INITIAL_DATETIME = f"{INITIAL_DATE} {INITIAL_TIME}"
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
@pytest.fixture
def storage_setup(hass, hass_storage):
@ -74,7 +79,7 @@ async def async_set_date_and_time(hass, entity_id, dt_value):
"""Set date and / or time of input_datetime."""
await hass.services.async_call(
DOMAIN,
SERVICE_SET_DATETIME,
"set_datetime",
{
ATTR_ENTITY_ID: entity_id,
ATTR_DATE: dt_value.date(),
@ -88,7 +93,7 @@ async def async_set_datetime(hass, entity_id, dt_value):
"""Set date and / or time of input_datetime."""
await hass.services.async_call(
DOMAIN,
SERVICE_SET_DATETIME,
"set_datetime",
{ATTR_ENTITY_ID: entity_id, ATTR_DATETIME: dt_value},
blocking=True,
)
@ -98,22 +103,24 @@ async def async_set_timestamp(hass, entity_id, timestamp):
"""Set date and / or time of input_datetime."""
await hass.services.async_call(
DOMAIN,
SERVICE_SET_DATETIME,
"set_datetime",
{ATTR_ENTITY_ID: entity_id, ATTR_TIMESTAMP: timestamp},
blocking=True,
)
async def test_invalid_configs(hass):
"""Test config."""
invalid_configs = [
@pytest.mark.parametrize(
"config",
[
None,
{},
{"name with space": None},
{"test_no_value": {"has_time": False, "has_date": False}},
]
for cfg in invalid_configs:
assert not await async_setup_component(hass, DOMAIN, {DOMAIN: cfg})
],
)
def test_invalid_configs(config):
"""Test config."""
with pytest.raises(vol.Invalid):
CONFIG_SCHEMA({DOMAIN: config})
async def test_set_datetime(hass):
@ -129,7 +136,7 @@ async def test_set_datetime(hass):
await async_set_date_and_time(hass, entity_id, dt_obj)
state = hass.states.get(entity_id)
assert state.state == str(dt_obj)
assert state.state == dt_obj.strftime(FMT_DATETIME)
assert state.attributes["has_time"]
assert state.attributes["has_date"]
@ -155,7 +162,7 @@ async def test_set_datetime_2(hass):
await async_set_datetime(hass, entity_id, dt_obj)
state = hass.states.get(entity_id)
assert state.state == str(dt_obj)
assert state.state == dt_obj.strftime(FMT_DATETIME)
assert state.attributes["has_time"]
assert state.attributes["has_date"]
@ -181,7 +188,7 @@ async def test_set_datetime_3(hass):
await async_set_timestamp(hass, entity_id, dt_util.as_utc(dt_obj).timestamp())
state = hass.states.get(entity_id)
assert state.state == str(dt_obj)
assert state.state == dt_obj.strftime(FMT_DATETIME)
assert state.attributes["has_time"]
assert state.attributes["has_date"]
@ -203,12 +210,11 @@ async def test_set_datetime_time(hass):
entity_id = "input_datetime.test_time"
dt_obj = datetime.datetime(2017, 9, 7, 19, 46, 30)
time_portion = dt_obj.time()
await async_set_date_and_time(hass, entity_id, dt_obj)
state = hass.states.get(entity_id)
assert state.state == str(time_portion)
assert state.state == dt_obj.strftime(FMT_TIME)
assert state.attributes["has_time"]
assert not state.attributes["has_date"]
@ -240,7 +246,6 @@ async def test_set_invalid(hass):
{"entity_id": entity_id, "time": time_portion},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == initial
@ -329,7 +334,7 @@ async def test_restore_state(hass):
"test_bogus_data": {
"has_time": True,
"has_date": True,
"initial": str(initial),
"initial": initial.strftime(FMT_DATETIME),
},
"test_was_time": {"has_time": False, "has_date": True},
"test_was_date": {"has_time": True, "has_date": False},
@ -339,22 +344,22 @@ async def test_restore_state(hass):
dt_obj = datetime.datetime(2017, 9, 7, 19, 46)
state_time = hass.states.get("input_datetime.test_time")
assert state_time.state == str(dt_obj.time())
assert state_time.state == dt_obj.strftime(FMT_TIME)
state_date = hass.states.get("input_datetime.test_date")
assert state_date.state == str(dt_obj.date())
assert state_date.state == dt_obj.strftime(FMT_DATE)
state_datetime = hass.states.get("input_datetime.test_datetime")
assert state_datetime.state == str(dt_obj)
assert state_datetime.state == dt_obj.strftime(FMT_DATETIME)
state_bogus = hass.states.get("input_datetime.test_bogus_data")
assert state_bogus.state == str(initial)
assert state_bogus.state == initial.strftime(FMT_DATETIME)
state_was_time = hass.states.get("input_datetime.test_was_time")
assert state_was_time.state == str(default.date())
assert state_was_time.state == default.strftime(FMT_DATE)
state_was_date = hass.states.get("input_datetime.test_was_date")
assert state_was_date.state == str(default.time())
assert state_was_date.state == default.strftime(FMT_TIME)
async def test_default_value(hass):
@ -373,15 +378,15 @@ async def test_default_value(hass):
dt_obj = datetime.datetime(1970, 1, 1, 0, 0)
state_time = hass.states.get("input_datetime.test_time")
assert state_time.state == str(dt_obj.time())
assert state_time.state == dt_obj.strftime(FMT_TIME)
assert state_time.attributes.get("timestamp") is not None
state_date = hass.states.get("input_datetime.test_date")
assert state_date.state == str(dt_obj.date())
assert state_date.state == dt_obj.strftime(FMT_DATE)
assert state_date.attributes.get("timestamp") is not None
state_datetime = hass.states.get("input_datetime.test_datetime")
assert state_datetime.state == str(dt_obj)
assert state_datetime.state == dt_obj.strftime(FMT_DATETIME)
assert state_datetime.attributes.get("timestamp") is not None
@ -434,7 +439,7 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
assert state_1 is not None
assert state_2 is None
assert state_3 is not None
assert str(dt_obj.date()) == state_1.state
assert dt_obj.strftime(FMT_DATE) == state_1.state
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt1") == f"{DOMAIN}.dt1"
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt2") is None
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt3") == f"{DOMAIN}.dt3"
@ -473,8 +478,8 @@ async def test_reload(hass, hass_admin_user, hass_read_only_user):
assert state_1 is not None
assert state_2 is not None
assert state_3 is None
assert str(DEFAULT_TIME) == state_1.state
assert str(datetime.datetime(1970, 1, 1, 0, 0)) == state_2.state
assert state_1.state == DEFAULT_TIME.strftime(FMT_TIME)
assert state_2.state == datetime.datetime(1970, 1, 1, 0, 0).strftime(FMT_DATETIME)
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt1") == f"{DOMAIN}.dt1"
assert ent_reg.async_get_entity_id(DOMAIN, DOMAIN, "dt2") == f"{DOMAIN}.dt2"
@ -641,3 +646,66 @@ async def test_setup_no_config(hass, hass_admin_user):
await hass.async_block_till_done()
assert count_start == len(hass.states.async_entity_ids())
async def test_timestamp(hass):
"""Test timestamp."""
try:
dt_util.set_default_time_zone(dt_util.get_time_zone("America/Los_Angeles"))
assert await async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
"test_datetime_initial_with_tz": {
"has_time": True,
"has_date": True,
"initial": "2020-12-13 10:00:00+01:00",
},
"test_datetime_initial_without_tz": {
"has_time": True,
"has_date": True,
"initial": "2020-12-13 10:00:00",
},
"test_time_initial": {
"has_time": True,
"has_date": False,
"initial": "10:00:00",
},
}
},
)
# initial has been converted to the set timezone
state_with_tz = hass.states.get("input_datetime.test_datetime_initial_with_tz")
assert state_with_tz is not None
assert state_with_tz.state == "2020-12-13 01:00:00"
assert (
dt_util.as_local(
dt_util.utc_from_timestamp(state_with_tz.attributes[ATTR_TIMESTAMP])
).strftime(FMT_DATETIME)
== "2020-12-13 01:00:00"
)
# initial has been interpreted as being part of set timezone
state_without_tz = hass.states.get(
"input_datetime.test_datetime_initial_without_tz"
)
assert state_without_tz is not None
assert state_without_tz.state == "2020-12-13 10:00:00"
assert (
dt_util.as_local(
dt_util.utc_from_timestamp(state_without_tz.attributes[ATTR_TIMESTAMP])
).strftime(FMT_DATETIME)
== "2020-12-13 10:00:00"
)
# Test initial time sets timestamp correctly.
state_time = hass.states.get("input_datetime.test_time_initial")
assert state_time is not None
assert state_time.state == "10:00:00"
assert state_time.attributes[ATTR_TIMESTAMP] == 10 * 60 * 60
finally:
dt_util.set_default_time_zone(ORIG_TIMEZONE)

View File

@ -19,6 +19,11 @@ async def test_reproducing_states(hass, caplog):
"2010-10-10",
{"has_date": True, "has_time": False},
)
hass.states.async_set(
"input_datetime.invalid_data",
"unavailable",
{"has_date": False, "has_time": False},
)
datetime_calls = async_mock_service(hass, "input_datetime", "set_datetime")
@ -57,6 +62,7 @@ async def test_reproducing_states(hass, caplog):
State("input_datetime.entity_date", "2011-10-10"),
# Should not raise
State("input_datetime.non_existing", "2010-10-10 01:20:00"),
State("input_datetime.invalid_data", "2010-10-10 01:20:00"),
],
)