mirror of
https://github.com/home-assistant/core.git
synced 2026-11-28 08:38:33 -04:00
Migrate from pytz to python-dateutil (#49643)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
co-authored by
Paulus Schoutsen
parent
13fe837fd2
commit
bf2d40adfe
@@ -7,7 +7,6 @@ from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import pytz
|
||||
|
||||
from homeassistant.components.climacell.config_flow import (
|
||||
_get_config_schema,
|
||||
@@ -18,6 +17,7 @@ from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.const import ATTR_ATTRIBUTION
|
||||
from homeassistant.core import HomeAssistant, State, callback
|
||||
from homeassistant.helpers.entity_registry import async_get
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import API_V3_ENTRY_DATA, API_V4_ENTRY_DATA
|
||||
|
||||
@@ -59,7 +59,7 @@ async def _setup(hass: HomeAssistant, config: dict[str, Any]) -> State:
|
||||
"""Set up entry and return entity state."""
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow",
|
||||
return_value=datetime(2021, 3, 6, 23, 59, 59, tzinfo=pytz.UTC),
|
||||
return_value=datetime(2021, 3, 6, 23, 59, 59, tzinfo=dt_util.UTC),
|
||||
):
|
||||
data = _get_config_schema(hass)(config)
|
||||
config_entry = MockConfigEntry(
|
||||
|
||||
@@ -7,7 +7,6 @@ from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import pytz
|
||||
|
||||
from homeassistant.components.climacell.config_flow import (
|
||||
_get_config_schema,
|
||||
@@ -46,6 +45,7 @@ from homeassistant.components.weather import (
|
||||
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_FRIENDLY_NAME
|
||||
from homeassistant.core import HomeAssistant, State, callback
|
||||
from homeassistant.helpers.entity_registry import async_get
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import API_V3_ENTRY_DATA, API_V4_ENTRY_DATA
|
||||
|
||||
@@ -70,7 +70,7 @@ async def _setup(hass: HomeAssistant, config: dict[str, Any]) -> State:
|
||||
"""Set up entry and return entity state."""
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow",
|
||||
return_value=datetime(2021, 3, 6, 23, 59, 59, tzinfo=pytz.UTC),
|
||||
return_value=datetime(2021, 3, 6, 23, 59, 59, tzinfo=dt_util.UTC),
|
||||
):
|
||||
data = _get_config_schema(hass)(config)
|
||||
config_entry = MockConfigEntry(
|
||||
|
||||
@@ -57,7 +57,7 @@ async def test_websocket_core_update(hass, client):
|
||||
assert hass.config.elevation != 25
|
||||
assert hass.config.location_name != "Huis"
|
||||
assert hass.config.units.name != CONF_UNIT_SYSTEM_IMPERIAL
|
||||
assert hass.config.time_zone.zone != "America/New_York"
|
||||
assert hass.config.time_zone != "America/New_York"
|
||||
assert hass.config.external_url != "https://www.example.com"
|
||||
assert hass.config.internal_url != "http://example.com"
|
||||
|
||||
@@ -91,7 +91,7 @@ async def test_websocket_core_update(hass, client):
|
||||
assert hass.config.internal_url == "http://example.local"
|
||||
|
||||
assert len(mock_set_tz.mock_calls) == 1
|
||||
assert mock_set_tz.mock_calls[0][1][0].zone == "America/New_York"
|
||||
assert mock_set_tz.mock_calls[0][1][0] == dt_util.get_time_zone("America/New_York")
|
||||
|
||||
|
||||
async def test_websocket_core_update_not_admin(hass, hass_ws_client, hass_admin_user):
|
||||
|
||||
@@ -4,7 +4,6 @@ from os import path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import pytz
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config as hass_config
|
||||
@@ -37,6 +36,7 @@ import homeassistant.core as ha
|
||||
from homeassistant.core import DOMAIN as HASS_DOMAIN, CoreState, State, callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.unit_system import METRIC_SYSTEM
|
||||
|
||||
from tests.common import (
|
||||
@@ -691,9 +691,7 @@ async def test_temp_change_ac_trigger_on_not_long_enough(hass, setup_comp_4):
|
||||
|
||||
async def test_temp_change_ac_trigger_on_long_enough(hass, setup_comp_4):
|
||||
"""Test if temperature change turn ac on."""
|
||||
fake_changed = datetime.datetime(
|
||||
1918, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
|
||||
)
|
||||
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11, tzinfo=dt_util.UTC)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow", return_value=fake_changed
|
||||
):
|
||||
@@ -719,9 +717,7 @@ async def test_temp_change_ac_trigger_off_not_long_enough(hass, setup_comp_4):
|
||||
|
||||
async def test_temp_change_ac_trigger_off_long_enough(hass, setup_comp_4):
|
||||
"""Test if temperature change turn ac on."""
|
||||
fake_changed = datetime.datetime(
|
||||
1918, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
|
||||
)
|
||||
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11, tzinfo=dt_util.UTC)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow", return_value=fake_changed
|
||||
):
|
||||
@@ -801,9 +797,7 @@ async def test_temp_change_ac_trigger_on_not_long_enough_2(hass, setup_comp_5):
|
||||
|
||||
async def test_temp_change_ac_trigger_on_long_enough_2(hass, setup_comp_5):
|
||||
"""Test if temperature change turn ac on."""
|
||||
fake_changed = datetime.datetime(
|
||||
1918, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
|
||||
)
|
||||
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11, tzinfo=dt_util.UTC)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow", return_value=fake_changed
|
||||
):
|
||||
@@ -829,9 +823,7 @@ async def test_temp_change_ac_trigger_off_not_long_enough_2(hass, setup_comp_5):
|
||||
|
||||
async def test_temp_change_ac_trigger_off_long_enough_2(hass, setup_comp_5):
|
||||
"""Test if temperature change turn ac on."""
|
||||
fake_changed = datetime.datetime(
|
||||
1918, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
|
||||
)
|
||||
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11, tzinfo=dt_util.UTC)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow", return_value=fake_changed
|
||||
):
|
||||
@@ -919,9 +911,7 @@ async def test_temp_change_heater_trigger_on_not_long_enough(hass, setup_comp_6)
|
||||
|
||||
async def test_temp_change_heater_trigger_on_long_enough(hass, setup_comp_6):
|
||||
"""Test if temperature change turn heater on after min cycle."""
|
||||
fake_changed = datetime.datetime(
|
||||
1918, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
|
||||
)
|
||||
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11, tzinfo=dt_util.UTC)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow", return_value=fake_changed
|
||||
):
|
||||
@@ -938,9 +928,7 @@ async def test_temp_change_heater_trigger_on_long_enough(hass, setup_comp_6):
|
||||
|
||||
async def test_temp_change_heater_trigger_off_long_enough(hass, setup_comp_6):
|
||||
"""Test if temperature change turn heater off after min cycle."""
|
||||
fake_changed = datetime.datetime(
|
||||
1918, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
|
||||
)
|
||||
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11, tzinfo=dt_util.UTC)
|
||||
with patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow", return_value=fake_changed
|
||||
):
|
||||
@@ -1019,7 +1007,7 @@ async def test_temp_change_ac_trigger_on_long_enough_3(hass, setup_comp_7):
|
||||
_setup_sensor(hass, 30)
|
||||
await hass.async_block_till_done()
|
||||
await common.async_set_temperature(hass, 25)
|
||||
test_time = datetime.datetime.now(pytz.UTC)
|
||||
test_time = datetime.datetime.now(dt_util.UTC)
|
||||
async_fire_time_changed(hass, test_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@@ -1042,7 +1030,7 @@ async def test_temp_change_ac_trigger_off_long_enough_3(hass, setup_comp_7):
|
||||
_setup_sensor(hass, 20)
|
||||
await hass.async_block_till_done()
|
||||
await common.async_set_temperature(hass, 25)
|
||||
test_time = datetime.datetime.now(pytz.UTC)
|
||||
test_time = datetime.datetime.now(dt_util.UTC)
|
||||
async_fire_time_changed(hass, test_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@@ -1090,7 +1078,7 @@ async def test_temp_change_heater_trigger_on_long_enough_2(hass, setup_comp_8):
|
||||
_setup_sensor(hass, 20)
|
||||
await hass.async_block_till_done()
|
||||
await common.async_set_temperature(hass, 25)
|
||||
test_time = datetime.datetime.now(pytz.UTC)
|
||||
test_time = datetime.datetime.now(dt_util.UTC)
|
||||
async_fire_time_changed(hass, test_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@@ -1113,7 +1101,7 @@ async def test_temp_change_heater_trigger_off_long_enough_2(hass, setup_comp_8):
|
||||
_setup_sensor(hass, 30)
|
||||
await hass.async_block_till_done()
|
||||
await common.async_set_temperature(hass, 25)
|
||||
test_time = datetime.datetime.now(pytz.UTC)
|
||||
test_time = datetime.datetime.now(dt_util.UTC)
|
||||
async_fire_time_changed(hass, test_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
@@ -6,7 +6,6 @@ import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import pytz
|
||||
|
||||
from homeassistant import config as hass_config
|
||||
from homeassistant.components.history_stats import DOMAIN
|
||||
@@ -82,7 +81,7 @@ class TestHistoryStatsSensor(unittest.TestCase):
|
||||
)
|
||||
def test_period_parsing(self, mock):
|
||||
"""Test the conversion from templates to period."""
|
||||
now = datetime(2019, 1, 1, 23, 30, 0, tzinfo=pytz.utc)
|
||||
now = datetime(2019, 1, 1, 23, 30, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.now", return_value=now):
|
||||
today = Template(
|
||||
"{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}",
|
||||
|
||||
@@ -26,7 +26,9 @@ def make_nyc_test_params(dtime, results, havdalah_offset=0):
|
||||
if isinstance(results, dict):
|
||||
time_zone = dt_util.get_time_zone("America/New_York")
|
||||
results = {
|
||||
key: time_zone.localize(value) if isinstance(value, datetime) else value
|
||||
key: value.replace(tzinfo=time_zone)
|
||||
if isinstance(value, datetime)
|
||||
else value
|
||||
for key, value in results.items()
|
||||
}
|
||||
return (
|
||||
@@ -46,7 +48,9 @@ def make_jerusalem_test_params(dtime, results, havdalah_offset=0):
|
||||
if isinstance(results, dict):
|
||||
time_zone = dt_util.get_time_zone("Asia/Jerusalem")
|
||||
results = {
|
||||
key: time_zone.localize(value) if isinstance(value, datetime) else value
|
||||
key: value.replace(tzinfo=time_zone)
|
||||
if isinstance(value, datetime)
|
||||
else value
|
||||
for key, value in results.items()
|
||||
}
|
||||
return (
|
||||
|
||||
@@ -179,9 +179,9 @@ async def test_issur_melacha_sensor(
|
||||
):
|
||||
"""Test Issur Melacha sensor output."""
|
||||
time_zone = dt_util.get_time_zone(tzname)
|
||||
test_time = time_zone.localize(now)
|
||||
test_time = now.replace(tzinfo=time_zone)
|
||||
|
||||
hass.config.time_zone = time_zone
|
||||
hass.config.time_zone = tzname
|
||||
hass.config.latitude = latitude
|
||||
hass.config.longitude = longitude
|
||||
|
||||
@@ -214,7 +214,7 @@ async def test_issur_melacha_sensor(
|
||||
[
|
||||
latitude,
|
||||
longitude,
|
||||
time_zone,
|
||||
tzname,
|
||||
HDATE_DEFAULT_ALTITUDE,
|
||||
diaspora,
|
||||
"english",
|
||||
@@ -270,9 +270,9 @@ async def test_issur_melacha_sensor_update(
|
||||
):
|
||||
"""Test Issur Melacha sensor output."""
|
||||
time_zone = dt_util.get_time_zone(tzname)
|
||||
test_time = time_zone.localize(now)
|
||||
test_time = now.replace(tzinfo=time_zone)
|
||||
|
||||
hass.config.time_zone = time_zone
|
||||
hass.config.time_zone = tzname
|
||||
hass.config.latitude = latitude
|
||||
hass.config.longitude = longitude
|
||||
|
||||
|
||||
@@ -163,9 +163,9 @@ async def test_jewish_calendar_sensor(
|
||||
):
|
||||
"""Test Jewish calendar sensor output."""
|
||||
time_zone = dt_util.get_time_zone(tzname)
|
||||
test_time = time_zone.localize(now)
|
||||
test_time = now.replace(tzinfo=time_zone)
|
||||
|
||||
hass.config.time_zone = time_zone
|
||||
hass.config.time_zone = tzname
|
||||
hass.config.latitude = latitude
|
||||
hass.config.longitude = longitude
|
||||
|
||||
@@ -188,7 +188,9 @@ async def test_jewish_calendar_sensor(
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = (
|
||||
dt_util.as_utc(time_zone.localize(result)) if isinstance(result, dt) else result
|
||||
dt_util.as_utc(result.replace(tzinfo=time_zone))
|
||||
if isinstance(result, dt)
|
||||
else result
|
||||
)
|
||||
|
||||
sensor_object = hass.states.get(f"sensor.test_{sensor}")
|
||||
@@ -506,9 +508,9 @@ async def test_shabbat_times_sensor(
|
||||
):
|
||||
"""Test sensor output for upcoming shabbat/yomtov times."""
|
||||
time_zone = dt_util.get_time_zone(tzname)
|
||||
test_time = time_zone.localize(now)
|
||||
test_time = now.replace(tzinfo=time_zone)
|
||||
|
||||
hass.config.time_zone = time_zone
|
||||
hass.config.time_zone = tzname
|
||||
hass.config.latitude = latitude
|
||||
hass.config.longitude = longitude
|
||||
|
||||
@@ -559,7 +561,7 @@ async def test_shabbat_times_sensor(
|
||||
[
|
||||
latitude,
|
||||
longitude,
|
||||
time_zone,
|
||||
tzname,
|
||||
HDATE_DEFAULT_ALTITUDE,
|
||||
diaspora,
|
||||
language,
|
||||
@@ -593,7 +595,7 @@ OMER_TEST_IDS = [
|
||||
@pytest.mark.parametrize(["test_time", "result"], OMER_PARAMS, ids=OMER_TEST_IDS)
|
||||
async def test_omer_sensor(hass, legacy_patchable_time, test_time, result):
|
||||
"""Test Omer Count sensor output."""
|
||||
test_time = hass.config.time_zone.localize(test_time)
|
||||
test_time = test_time.replace(tzinfo=dt_util.get_time_zone(hass.config.time_zone))
|
||||
|
||||
with alter_time(test_time):
|
||||
assert await async_setup_component(
|
||||
@@ -627,7 +629,7 @@ DAFYOMI_TEST_IDS = [
|
||||
@pytest.mark.parametrize(["test_time", "result"], DAFYOMI_PARAMS, ids=DAFYOMI_TEST_IDS)
|
||||
async def test_dafyomi_sensor(hass, legacy_patchable_time, test_time, result):
|
||||
"""Test Daf Yomi sensor output."""
|
||||
test_time = hass.config.time_zone.localize(test_time)
|
||||
test_time = test_time.replace(tzinfo=dt_util.get_time_zone(hass.config.time_zone))
|
||||
|
||||
with alter_time(test_time):
|
||||
assert await async_setup_component(
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
from pytz import timezone
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow
|
||||
from homeassistant.components.pvpc_hourly_pricing import ATTR_TARIFF, DOMAIN
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .conftest import check_valid_state
|
||||
|
||||
@@ -26,7 +25,7 @@ async def test_config_flow(
|
||||
- Check abort when trying to config another with same tariff
|
||||
- Check removal and add again to check state restoration
|
||||
"""
|
||||
hass.config.time_zone = timezone("Europe/Madrid")
|
||||
hass.config.time_zone = dt_util.get_time_zone("Europe/Madrid")
|
||||
mock_data = {"return_time": datetime(2019, 10, 26, 14, 0, tzinfo=date_util.UTC)}
|
||||
|
||||
def mock_now():
|
||||
|
||||
@@ -3,12 +3,11 @@ from datetime import datetime, timedelta
|
||||
import logging
|
||||
from unittest.mock import patch
|
||||
|
||||
from pytz import timezone
|
||||
|
||||
from homeassistant.components.pvpc_hourly_pricing import ATTR_TARIFF, DOMAIN
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import ATTR_NOW, EVENT_TIME_CHANGED
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .conftest import check_valid_state
|
||||
|
||||
@@ -32,7 +31,7 @@ async def test_sensor_availability(
|
||||
hass, caplog, legacy_patchable_time, pvpc_aioclient_mock: AiohttpClientMocker
|
||||
):
|
||||
"""Test sensor availability and handling of cloud access."""
|
||||
hass.config.time_zone = timezone("Europe/Madrid")
|
||||
hass.config.time_zone = dt_util.get_time_zone("Europe/Madrid")
|
||||
config = {DOMAIN: [{CONF_NAME: "test_dst", ATTR_TARIFF: "discrimination"}]}
|
||||
mock_data = {"return_time": datetime(2019, 10, 27, 20, 0, 0, tzinfo=date_util.UTC)}
|
||||
|
||||
|
||||
@@ -170,5 +170,5 @@ def _process_timestamp(ts):
|
||||
if ts is None:
|
||||
return None
|
||||
if ts.tzinfo is None:
|
||||
return dt_util.UTC.localize(ts)
|
||||
return ts.replace(tzinfo=dt_util.UTC)
|
||||
return dt_util.as_utc(ts)
|
||||
|
||||
@@ -604,7 +604,7 @@ def test_auto_purge(hass_recorder):
|
||||
#
|
||||
# The clock is started at 4:15am then advanced forward below
|
||||
now = dt_util.utcnow()
|
||||
test_time = tz.localize(datetime(now.year + 2, 1, 1, 4, 15, 0))
|
||||
test_time = datetime(now.year + 2, 1, 1, 4, 15, 0, tzinfo=tz)
|
||||
run_tasks_at_time(hass, test_time)
|
||||
|
||||
with patch(
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
import pytz
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
|
||||
@@ -144,11 +143,11 @@ async def test_process_timestamp():
|
||||
"""Test processing time stamp to UTC."""
|
||||
datetime_with_tzinfo = datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC)
|
||||
datetime_without_tzinfo = datetime(2016, 7, 9, 11, 0, 0)
|
||||
est = pytz.timezone("US/Eastern")
|
||||
est = dt_util.get_time_zone("US/Eastern")
|
||||
datetime_est_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=est)
|
||||
nst = pytz.timezone("Canada/Newfoundland")
|
||||
nst = dt_util.get_time_zone("Canada/Newfoundland")
|
||||
datetime_nst_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=nst)
|
||||
hst = pytz.timezone("US/Hawaii")
|
||||
hst = dt_util.get_time_zone("US/Hawaii")
|
||||
datetime_hst_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=hst)
|
||||
|
||||
assert process_timestamp(datetime_with_tzinfo) == datetime(
|
||||
@@ -158,13 +157,13 @@ async def test_process_timestamp():
|
||||
2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC
|
||||
)
|
||||
assert process_timestamp(datetime_est_timezone) == datetime(
|
||||
2016, 7, 9, 15, 56, tzinfo=dt.UTC
|
||||
2016, 7, 9, 15, 0, tzinfo=dt.UTC
|
||||
)
|
||||
assert process_timestamp(datetime_nst_timezone) == datetime(
|
||||
2016, 7, 9, 14, 31, tzinfo=dt.UTC
|
||||
2016, 7, 9, 13, 30, tzinfo=dt.UTC
|
||||
)
|
||||
assert process_timestamp(datetime_hst_timezone) == datetime(
|
||||
2016, 7, 9, 21, 31, tzinfo=dt.UTC
|
||||
2016, 7, 9, 21, 0, tzinfo=dt.UTC
|
||||
)
|
||||
assert process_timestamp(None) is None
|
||||
|
||||
@@ -173,13 +172,13 @@ async def test_process_timestamp_to_utc_isoformat():
|
||||
"""Test processing time stamp to UTC isoformat."""
|
||||
datetime_with_tzinfo = datetime(2016, 7, 9, 11, 0, 0, tzinfo=dt.UTC)
|
||||
datetime_without_tzinfo = datetime(2016, 7, 9, 11, 0, 0)
|
||||
est = pytz.timezone("US/Eastern")
|
||||
est = dt_util.get_time_zone("US/Eastern")
|
||||
datetime_est_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=est)
|
||||
est = pytz.timezone("US/Eastern")
|
||||
est = dt_util.get_time_zone("US/Eastern")
|
||||
datetime_est_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=est)
|
||||
nst = pytz.timezone("Canada/Newfoundland")
|
||||
nst = dt_util.get_time_zone("Canada/Newfoundland")
|
||||
datetime_nst_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=nst)
|
||||
hst = pytz.timezone("US/Hawaii")
|
||||
hst = dt_util.get_time_zone("US/Hawaii")
|
||||
datetime_hst_timezone = datetime(2016, 7, 9, 11, 0, 0, tzinfo=hst)
|
||||
|
||||
assert (
|
||||
@@ -192,15 +191,15 @@ async def test_process_timestamp_to_utc_isoformat():
|
||||
)
|
||||
assert (
|
||||
process_timestamp_to_utc_isoformat(datetime_est_timezone)
|
||||
== "2016-07-09T15:56:00+00:00"
|
||||
== "2016-07-09T15:00:00+00:00"
|
||||
)
|
||||
assert (
|
||||
process_timestamp_to_utc_isoformat(datetime_nst_timezone)
|
||||
== "2016-07-09T14:31:00+00:00"
|
||||
== "2016-07-09T13:30:00+00:00"
|
||||
)
|
||||
assert (
|
||||
process_timestamp_to_utc_isoformat(datetime_hst_timezone)
|
||||
== "2016-07-09T21:31:00+00:00"
|
||||
== "2016-07-09T21:00:00+00:00"
|
||||
)
|
||||
assert process_timestamp_to_utc_isoformat(None) is None
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ def calls(hass):
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
dt_util.set_default_time_zone(hass.config.time_zone)
|
||||
hass.config.set_time_zone(hass.config.time_zone)
|
||||
hass.loop.run_until_complete(
|
||||
async_setup_component(hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
||||
)
|
||||
|
||||
@@ -3,7 +3,6 @@ from datetime import datetime, timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import pytz
|
||||
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
import homeassistant.core as ha
|
||||
@@ -61,7 +60,7 @@ async def test_setup_no_sensors(hass):
|
||||
|
||||
async def test_in_period_on_start(hass):
|
||||
"""Test simple setting."""
|
||||
test_time = datetime(2019, 1, 10, 18, 43, 0, tzinfo=hass.config.time_zone)
|
||||
test_time = datetime(2019, 1, 10, 18, 43, 0, tzinfo=dt_util.UTC)
|
||||
config = {
|
||||
"binary_sensor": [
|
||||
{
|
||||
@@ -85,7 +84,7 @@ async def test_in_period_on_start(hass):
|
||||
|
||||
async def test_midnight_turnover_before_midnight_inside_period(hass):
|
||||
"""Test midnight turnover setting before midnight inside period ."""
|
||||
test_time = datetime(2019, 1, 10, 22, 30, 0, tzinfo=hass.config.time_zone)
|
||||
test_time = datetime(2019, 1, 10, 22, 30, 0, tzinfo=dt_util.UTC)
|
||||
config = {
|
||||
"binary_sensor": [
|
||||
{"platform": "tod", "name": "Night", "after": "22:00", "before": "5:00"}
|
||||
@@ -104,9 +103,7 @@ async def test_midnight_turnover_before_midnight_inside_period(hass):
|
||||
|
||||
async def test_midnight_turnover_after_midnight_inside_period(hass):
|
||||
"""Test midnight turnover setting before midnight inside period ."""
|
||||
test_time = hass.config.time_zone.localize(
|
||||
datetime(2019, 1, 10, 21, 0, 0)
|
||||
).astimezone(pytz.UTC)
|
||||
test_time = datetime(2019, 1, 10, 21, 0, 0, tzinfo=dt_util.UTC)
|
||||
config = {
|
||||
"binary_sensor": [
|
||||
{"platform": "tod", "name": "Night", "after": "22:00", "before": "5:00"}
|
||||
@@ -140,9 +137,7 @@ async def test_midnight_turnover_after_midnight_inside_period(hass):
|
||||
|
||||
async def test_midnight_turnover_before_midnight_outside_period(hass):
|
||||
"""Test midnight turnover setting before midnight outside period."""
|
||||
test_time = hass.config.time_zone.localize(
|
||||
datetime(2019, 1, 10, 20, 30, 0)
|
||||
).astimezone(pytz.UTC)
|
||||
test_time = datetime(2019, 1, 10, 20, 30, 0, tzinfo=dt_util.UTC)
|
||||
config = {
|
||||
"binary_sensor": [
|
||||
{"platform": "tod", "name": "Night", "after": "22:00", "before": "5:00"}
|
||||
@@ -161,9 +156,7 @@ async def test_midnight_turnover_before_midnight_outside_period(hass):
|
||||
|
||||
async def test_midnight_turnover_after_midnight_outside_period(hass):
|
||||
"""Test midnight turnover setting before midnight inside period ."""
|
||||
test_time = hass.config.time_zone.localize(
|
||||
datetime(2019, 1, 10, 20, 0, 0)
|
||||
).astimezone(pytz.UTC)
|
||||
test_time = datetime(2019, 1, 10, 20, 0, 0, tzinfo=dt_util.UTC)
|
||||
|
||||
config = {
|
||||
"binary_sensor": [
|
||||
@@ -180,9 +173,7 @@ async def test_midnight_turnover_after_midnight_outside_period(hass):
|
||||
state = hass.states.get("binary_sensor.night")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
switchover_time = hass.config.time_zone.localize(
|
||||
datetime(2019, 1, 11, 4, 59, 0)
|
||||
).astimezone(pytz.UTC)
|
||||
switchover_time = datetime(2019, 1, 11, 4, 59, 0, tzinfo=dt_util.UTC)
|
||||
with patch(
|
||||
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
|
||||
return_value=switchover_time,
|
||||
@@ -210,9 +201,7 @@ async def test_midnight_turnover_after_midnight_outside_period(hass):
|
||||
|
||||
async def test_from_sunrise_to_sunset(hass):
|
||||
"""Test period from sunrise to sunset."""
|
||||
test_time = hass.config.time_zone.localize(datetime(2019, 1, 12)).astimezone(
|
||||
pytz.UTC
|
||||
)
|
||||
test_time = datetime(2019, 1, 12, tzinfo=dt_util.UTC)
|
||||
sunrise = dt_util.as_local(
|
||||
get_astral_event_date(hass, "sunrise", dt_util.as_utc(test_time))
|
||||
)
|
||||
@@ -311,9 +300,7 @@ async def test_from_sunrise_to_sunset(hass):
|
||||
|
||||
async def test_from_sunset_to_sunrise(hass):
|
||||
"""Test period from sunset to sunrise."""
|
||||
test_time = hass.config.time_zone.localize(datetime(2019, 1, 12)).astimezone(
|
||||
pytz.UTC
|
||||
)
|
||||
test_time = datetime(2019, 1, 12, tzinfo=dt_util.UTC)
|
||||
sunset = dt_util.as_local(get_astral_event_date(hass, "sunset", test_time))
|
||||
sunrise = dt_util.as_local(get_astral_event_next(hass, "sunrise", sunset))
|
||||
# assert sunset == sunrise
|
||||
@@ -405,13 +392,13 @@ async def test_from_sunset_to_sunrise(hass):
|
||||
|
||||
async def test_offset(hass):
|
||||
"""Test offset."""
|
||||
after = hass.config.time_zone.localize(datetime(2019, 1, 10, 18, 0, 0)).astimezone(
|
||||
pytz.UTC
|
||||
) + timedelta(hours=1, minutes=34)
|
||||
after = datetime(2019, 1, 10, 18, 0, 0, tzinfo=dt_util.UTC) + timedelta(
|
||||
hours=1, minutes=34
|
||||
)
|
||||
|
||||
before = hass.config.time_zone.localize(datetime(2019, 1, 10, 22, 0, 0)).astimezone(
|
||||
pytz.UTC
|
||||
) + timedelta(hours=1, minutes=45)
|
||||
before = datetime(2019, 1, 10, 22, 0, 0, tzinfo=dt_util.UTC) + timedelta(
|
||||
hours=1, minutes=45
|
||||
)
|
||||
|
||||
entity_id = "binary_sensor.evening"
|
||||
config = {
|
||||
@@ -484,9 +471,9 @@ async def test_offset(hass):
|
||||
|
||||
async def test_offset_overnight(hass):
|
||||
"""Test offset overnight."""
|
||||
after = hass.config.time_zone.localize(datetime(2019, 1, 10, 18, 0, 0)).astimezone(
|
||||
pytz.UTC
|
||||
) + timedelta(hours=1, minutes=34)
|
||||
after = datetime(2019, 1, 10, 18, 0, 0, tzinfo=dt_util.UTC) + timedelta(
|
||||
hours=1, minutes=34
|
||||
)
|
||||
entity_id = "binary_sensor.evening"
|
||||
config = {
|
||||
"binary_sensor": [
|
||||
@@ -528,9 +515,7 @@ async def test_norwegian_case_winter(hass):
|
||||
hass.config.latitude = 69.6
|
||||
hass.config.longitude = 18.8
|
||||
|
||||
test_time = hass.config.time_zone.localize(datetime(2010, 1, 1)).astimezone(
|
||||
pytz.UTC
|
||||
)
|
||||
test_time = datetime(2010, 1, 1, tzinfo=dt_util.UTC)
|
||||
sunrise = dt_util.as_local(
|
||||
get_astral_event_next(hass, "sunrise", dt_util.as_utc(test_time))
|
||||
)
|
||||
@@ -645,9 +630,7 @@ async def test_norwegian_case_summer(hass):
|
||||
hass.config.longitude = 18.8
|
||||
hass.config.elevation = 10.0
|
||||
|
||||
test_time = hass.config.time_zone.localize(datetime(2010, 6, 1)).astimezone(
|
||||
pytz.UTC
|
||||
)
|
||||
test_time = datetime(2010, 6, 1, tzinfo=dt_util.UTC)
|
||||
|
||||
sunrise = dt_util.as_local(
|
||||
get_astral_event_next(hass, "sunrise", dt_util.as_utc(test_time))
|
||||
@@ -759,9 +742,7 @@ async def test_norwegian_case_summer(hass):
|
||||
|
||||
async def test_sun_offset(hass):
|
||||
"""Test sun event with offset."""
|
||||
test_time = hass.config.time_zone.localize(datetime(2019, 1, 12)).astimezone(
|
||||
pytz.UTC
|
||||
)
|
||||
test_time = datetime(2019, 1, 12, tzinfo=dt_util.UTC)
|
||||
sunrise = dt_util.as_local(
|
||||
get_astral_event_date(hass, "sunrise", dt_util.as_utc(test_time))
|
||||
+ timedelta(hours=-1, minutes=-30)
|
||||
@@ -881,30 +862,27 @@ async def test_sun_offset(hass):
|
||||
|
||||
async def test_dst(hass):
|
||||
"""Test sun event with offset."""
|
||||
hass.config.time_zone = pytz.timezone("CET")
|
||||
test_time = hass.config.time_zone.localize(
|
||||
datetime(2019, 3, 30, 3, 0, 0)
|
||||
).astimezone(pytz.UTC)
|
||||
hass.config.time_zone = "CET"
|
||||
test_time = datetime(2019, 3, 30, 3, 0, 0, tzinfo=dt_util.UTC)
|
||||
config = {
|
||||
"binary_sensor": [
|
||||
{"platform": "tod", "name": "Day", "after": "2:30", "before": "2:40"}
|
||||
]
|
||||
}
|
||||
# Test DST:
|
||||
# after 2019-03-30 03:00 CET the next update should ge scheduled
|
||||
# at 3:30 not 2:30 local time
|
||||
# Internally the
|
||||
entity_id = "binary_sensor.day"
|
||||
testtime = test_time
|
||||
with patch(
|
||||
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
|
||||
return_value=testtime,
|
||||
return_value=test_time,
|
||||
):
|
||||
await async_setup_component(hass, "binary_sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.attributes["after"] == "2019-03-31T03:30:00+02:00"
|
||||
assert state.attributes["before"] == "2019-03-31T03:40:00+02:00"
|
||||
assert state.attributes["next_update"] == "2019-03-31T03:30:00+02:00"
|
||||
assert state.attributes["after"] == "2019-03-30T03:30:00+01:00"
|
||||
assert state.attributes["before"] == "2019-03-30T03:40:00+01:00"
|
||||
assert state.attributes["next_update"] == "2019-03-30T03:30:00+01:00"
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
@@ -7,7 +7,6 @@ from urllib.parse import urlparse
|
||||
|
||||
from aiohttp.test_utils import TestClient
|
||||
import arrow
|
||||
import pytz
|
||||
from withings_api.common import (
|
||||
MeasureGetMeasResponse,
|
||||
NotifyAppli,
|
||||
@@ -40,6 +39,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
from homeassistant.helpers.config_entry_oauth2_flow import AUTH_CALLBACK_PATH
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
@@ -77,7 +77,7 @@ def new_profile_config(
|
||||
measuregrps=[],
|
||||
more=False,
|
||||
offset=0,
|
||||
timezone=pytz.UTC,
|
||||
timezone=dt_util.UTC,
|
||||
updatetime=arrow.get(12345),
|
||||
),
|
||||
api_response_sleep_get_summary=api_response_sleep_get_summary
|
||||
|
||||
@@ -3,7 +3,6 @@ from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import arrow
|
||||
import pytz
|
||||
from withings_api.common import (
|
||||
GetSleepSummaryData,
|
||||
GetSleepSummarySerie,
|
||||
@@ -29,6 +28,7 @@ from homeassistant.components.withings.const import Measurement
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity_registry import EntityRegistry
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .common import ComponentFactory, new_profile_config
|
||||
|
||||
@@ -189,7 +189,7 @@ PERSON0 = new_profile_config(
|
||||
),
|
||||
),
|
||||
more=False,
|
||||
timezone=pytz.UTC,
|
||||
timezone=dt_util.UTC,
|
||||
updatetime=arrow.get("2019-08-01"),
|
||||
offset=0,
|
||||
),
|
||||
@@ -198,7 +198,7 @@ PERSON0 = new_profile_config(
|
||||
offset=0,
|
||||
series=(
|
||||
GetSleepSummarySerie(
|
||||
timezone=pytz.UTC,
|
||||
timezone=dt_util.UTC,
|
||||
model=SleepModel.SLEEP_MONITOR,
|
||||
startdate=arrow.get("2019-02-01"),
|
||||
enddate=arrow.get("2019-02-01"),
|
||||
@@ -225,7 +225,7 @@ PERSON0 = new_profile_config(
|
||||
),
|
||||
),
|
||||
GetSleepSummarySerie(
|
||||
timezone=pytz.UTC,
|
||||
timezone=dt_util.UTC,
|
||||
model=SleepModel.SLEEP_MONITOR,
|
||||
startdate=arrow.get("2019-02-01"),
|
||||
enddate=arrow.get("2019-02-01"),
|
||||
|
||||
@@ -5,7 +5,6 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
from miio import DeviceException
|
||||
import pytest
|
||||
from pytz import utc
|
||||
|
||||
from homeassistant.components.vacuum import (
|
||||
ATTR_BATTERY_ICON,
|
||||
@@ -55,6 +54,7 @@ from homeassistant.const import (
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .test_config_flow import TEST_MAC
|
||||
|
||||
@@ -106,12 +106,12 @@ def mirobo_is_got_error_fixture():
|
||||
mock_timer_1 = MagicMock()
|
||||
mock_timer_1.enabled = True
|
||||
mock_timer_1.cron = "5 5 1 8 1"
|
||||
mock_timer_1.next_schedule = datetime(2020, 5, 23, 13, 21, 10, tzinfo=utc)
|
||||
mock_timer_1.next_schedule = datetime(2020, 5, 23, 13, 21, 10, tzinfo=dt_util.UTC)
|
||||
|
||||
mock_timer_2 = MagicMock()
|
||||
mock_timer_2.enabled = False
|
||||
mock_timer_2.cron = "5 5 1 8 2"
|
||||
mock_timer_2.next_schedule = datetime(2020, 5, 23, 13, 21, 10, tzinfo=utc)
|
||||
mock_timer_2.next_schedule = datetime(2020, 5, 23, 13, 21, 10, tzinfo=dt_util.UTC)
|
||||
|
||||
mock_vacuum.timer.return_value = [mock_timer_1, mock_timer_2]
|
||||
|
||||
@@ -180,12 +180,12 @@ def mirobo_is_on_fixture():
|
||||
mock_timer_1 = MagicMock()
|
||||
mock_timer_1.enabled = True
|
||||
mock_timer_1.cron = "5 5 1 8 1"
|
||||
mock_timer_1.next_schedule = datetime(2020, 5, 23, 13, 21, 10, tzinfo=utc)
|
||||
mock_timer_1.next_schedule = datetime(2020, 5, 23, 13, 21, 10, tzinfo=dt_util.UTC)
|
||||
|
||||
mock_timer_2 = MagicMock()
|
||||
mock_timer_2.enabled = False
|
||||
mock_timer_2.cron = "5 5 1 8 2"
|
||||
mock_timer_2.next_schedule = datetime(2020, 5, 23, 13, 21, 10, tzinfo=utc)
|
||||
mock_timer_2.next_schedule = datetime(2020, 5, 23, 13, 21, 10, tzinfo=dt_util.UTC)
|
||||
|
||||
mock_vacuum.timer.return_value = [mock_timer_1, mock_timer_2]
|
||||
|
||||
@@ -255,12 +255,12 @@ async def test_xiaomi_vacuum_services(hass, caplog, mock_mirobo_is_got_error):
|
||||
{
|
||||
"enabled": True,
|
||||
"cron": "5 5 1 8 1",
|
||||
"next_schedule": datetime(2020, 5, 23, 13, 21, 10, tzinfo=utc),
|
||||
"next_schedule": datetime(2020, 5, 23, 13, 21, 10, tzinfo=dt_util.UTC),
|
||||
},
|
||||
{
|
||||
"enabled": False,
|
||||
"cron": "5 5 1 8 2",
|
||||
"next_schedule": datetime(2020, 5, 23, 13, 21, 10, tzinfo=utc),
|
||||
"next_schedule": datetime(2020, 5, 23, 13, 21, 10, tzinfo=dt_util.UTC),
|
||||
},
|
||||
]
|
||||
|
||||
@@ -353,12 +353,12 @@ async def test_xiaomi_specific_services(hass, caplog, mock_mirobo_is_on):
|
||||
{
|
||||
"enabled": True,
|
||||
"cron": "5 5 1 8 1",
|
||||
"next_schedule": datetime(2020, 5, 23, 13, 21, 10, tzinfo=utc),
|
||||
"next_schedule": datetime(2020, 5, 23, 13, 21, 10, tzinfo=dt_util.UTC),
|
||||
},
|
||||
{
|
||||
"enabled": False,
|
||||
"cron": "5 5 1 8 2",
|
||||
"next_schedule": datetime(2020, 5, 23, 13, 21, 10, tzinfo=utc),
|
||||
"next_schedule": datetime(2020, 5, 23, 13, 21, 10, tzinfo=dt_util.UTC),
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ from datetime import datetime
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from pytz import utc
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.bootstrap import async_setup_component
|
||||
@@ -19,6 +18,7 @@ from homeassistant.components.zwave import (
|
||||
from homeassistant.components.zwave.binary_sensor import get_device
|
||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import async_fire_time_changed, mock_registry
|
||||
from tests.mock.zwave import MockEntityValues, MockNetwork, MockNode, MockValue
|
||||
@@ -140,7 +140,7 @@ async def test_auto_heal_midnight(hass, mock_openzwave, legacy_patchable_time):
|
||||
network = hass.data[zwave.DATA_NETWORK]
|
||||
assert not network.heal.called
|
||||
|
||||
time = utc.localize(datetime(2017, 5, 6, 0, 0, 0))
|
||||
time = datetime(2017, 5, 6, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
async_fire_time_changed(hass, time)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
@@ -156,7 +156,7 @@ async def test_auto_heal_disabled(hass, mock_openzwave):
|
||||
network = hass.data[zwave.DATA_NETWORK]
|
||||
assert not network.heal.called
|
||||
|
||||
time = utc.localize(datetime(2017, 5, 6, 0, 0, 0))
|
||||
time = datetime(2017, 5, 6, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
async_fire_time_changed(hass, time)
|
||||
await hass.async_block_till_done()
|
||||
assert not network.heal.called
|
||||
|
||||
Reference in New Issue
Block a user