Simplify time zone setting in tests (#68330)

* Simplify timezone setting in tests

* Fix typo

* Adjust caldav tests

* Adjust input_datetime tests

* Adjust time_date tests

* Adjust tod tests

* Adjust helper tests

* Adjust recorder tests

* Adjust risco tests

* Adjust aemet tests

* Adjust flux tests

* Adjust forecast_solar tests

* Revert unnecessary change in forecast_solar test

* Adjust climacell tests

* Adjust google tests

* Adjust sensor tests

* Adjust sonarr tests

* Adjust template tests

* Adjust zodiac tests

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Erik Montnemery
2022-03-20 10:25:15 +01:00
committed by GitHub
parent dbeec1f7da
commit cf4033b1bc
33 changed files with 256 additions and 320 deletions

View File

@ -38,8 +38,6 @@ 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):
@ -131,7 +129,9 @@ async def test_set_datetime(hass):
entity_id = "input_datetime.test_datetime"
dt_obj = datetime.datetime(2017, 9, 7, 19, 46, 30, tzinfo=datetime.timezone.utc)
dt_obj = datetime.datetime(
2017, 9, 7, 19, 46, 30, tzinfo=dt_util.get_time_zone(hass.config.time_zone)
)
await async_set_date_and_time(hass, entity_id, dt_obj)
@ -157,7 +157,9 @@ async def test_set_datetime_2(hass):
entity_id = "input_datetime.test_datetime"
dt_obj = datetime.datetime(2017, 9, 7, 19, 46, 30, tzinfo=datetime.timezone.utc)
dt_obj = datetime.datetime(
2017, 9, 7, 19, 46, 30, tzinfo=dt_util.get_time_zone(hass.config.time_zone)
)
await async_set_datetime(hass, entity_id, dt_obj)
@ -183,7 +185,9 @@ async def test_set_datetime_3(hass):
entity_id = "input_datetime.test_datetime"
dt_obj = datetime.datetime(2017, 9, 7, 19, 46, 30, tzinfo=datetime.timezone.utc)
dt_obj = datetime.datetime(
2017, 9, 7, 19, 46, 30, tzinfo=dt_util.get_time_zone(hass.config.time_zone)
)
await async_set_timestamp(hass, entity_id, dt_util.as_utc(dt_obj).timestamp())
@ -649,101 +653,97 @@ async def test_setup_no_config(hass, hass_admin_user):
async def test_timestamp(hass):
"""Test timestamp."""
try:
dt_util.set_default_time_zone(dt_util.get_time_zone("America/Los_Angeles"))
hass.config.set_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",
},
}
},
)
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
# Timezone LA is UTC-8 => timestamp carries +01:00 => delta is -9 => 10:00 - 09:00 => 01:00
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 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
# Timezone LA is UTC-8 => timestamp carries +01:00 => delta is -9 => 10:00 - 09:00 => 01:00
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"
# Timezone LA is UTC-8 => timestamp has no zone (= assumed local) => delta to UTC is +8 => 10:00 + 08:00 => 18:00
assert (
dt_util.utc_from_timestamp(
state_without_tz.attributes[ATTR_TIMESTAMP]
).strftime(FMT_DATETIME)
== "2020-12-13 18: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"
)
# Use datetime.datetime.fromtimestamp
assert (
dt_util.as_local(
datetime.datetime.fromtimestamp(
state_without_tz.attributes[ATTR_TIMESTAMP], datetime.timezone.utc
)
).strftime(FMT_DATETIME)
== "2020-12-13 10: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"
# Timezone LA is UTC-8 => timestamp has no zone (= assumed local) => delta to UTC is +8 => 10:00 + 08:00 => 18:00
assert (
dt_util.utc_from_timestamp(
state_without_tz.attributes[ATTR_TIMESTAMP]
).strftime(FMT_DATETIME)
== "2020-12-13 18: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"
)
# Use datetime.datetime.fromtimestamp
assert (
dt_util.as_local(
datetime.datetime.fromtimestamp(
state_without_tz.attributes[ATTR_TIMESTAMP], datetime.timezone.utc
)
).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
# 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
# Test that setting the timestamp of an entity works.
await hass.services.async_call(
DOMAIN,
"set_datetime",
{
ATTR_ENTITY_ID: "input_datetime.test_datetime_initial_with_tz",
ATTR_TIMESTAMP: state_without_tz.attributes[ATTR_TIMESTAMP],
},
blocking=True,
)
state_with_tz_updated = hass.states.get(
"input_datetime.test_datetime_initial_with_tz"
)
assert state_with_tz_updated.state == "2020-12-13 10:00:00"
assert (
state_with_tz_updated.attributes[ATTR_TIMESTAMP]
== state_without_tz.attributes[ATTR_TIMESTAMP]
)
finally:
dt_util.set_default_time_zone(ORIG_TIMEZONE)
# Test that setting the timestamp of an entity works.
await hass.services.async_call(
DOMAIN,
"set_datetime",
{
ATTR_ENTITY_ID: "input_datetime.test_datetime_initial_with_tz",
ATTR_TIMESTAMP: state_without_tz.attributes[ATTR_TIMESTAMP],
},
blocking=True,
)
state_with_tz_updated = hass.states.get(
"input_datetime.test_datetime_initial_with_tz"
)
assert state_with_tz_updated.state == "2020-12-13 10:00:00"
assert (
state_with_tz_updated.attributes[ATTR_TIMESTAMP]
== state_without_tz.attributes[ATTR_TIMESTAMP]
)
@pytest.mark.parametrize(