Use datetime.UTC alias [3.11] (#97067)

pull/97071/head
Marc Mueller 2023-07-23 00:05:11 +02:00 committed by GitHub
parent da6802b009
commit fe0fe19be9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 38 additions and 80 deletions

View File

@ -51,9 +51,7 @@ class DeviceAuth(AuthImplementation):
async def async_resolve_external_data(self, external_data: Any) -> dict: async def async_resolve_external_data(self, external_data: Any) -> dict:
"""Resolve a Google API Credentials object to Home Assistant token.""" """Resolve a Google API Credentials object to Home Assistant token."""
creds: Credentials = external_data[DEVICE_AUTH_CREDS] creds: Credentials = external_data[DEVICE_AUTH_CREDS]
delta = ( delta = creds.token_expiry.replace(tzinfo=datetime.UTC) - dt_util.utcnow()
creds.token_expiry.replace(tzinfo=datetime.timezone.utc) - dt_util.utcnow()
)
_LOGGER.debug( _LOGGER.debug(
"Token expires at %s (in %s)", creds.token_expiry, delta.total_seconds() "Token expires at %s (in %s)", creds.token_expiry, delta.total_seconds()
) )
@ -116,7 +114,7 @@ class DeviceFlow:
# For some reason, oauth.step1_get_device_and_user_codes() returns a datetime # For some reason, oauth.step1_get_device_and_user_codes() returns a datetime
# object without tzinfo. For the comparison below to work, it needs one. # object without tzinfo. For the comparison below to work, it needs one.
user_code_expiry = self._device_flow_info.user_code_expiry.replace( user_code_expiry = self._device_flow_info.user_code_expiry.replace(
tzinfo=datetime.timezone.utc tzinfo=datetime.UTC
) )
expiration_time = min(user_code_expiry, max_timeout) expiration_time = min(user_code_expiry, max_timeout)

View File

@ -449,7 +449,7 @@ class DataManager:
0, 0,
0, 0,
0, 0,
datetime.timezone.utc, datetime.UTC,
) )
def get_sleep_summary() -> SleepGetSummaryResponse: def get_sleep_summary() -> SleepGetSummaryResponse:

View File

@ -58,8 +58,8 @@ async def test_setup(hass: HomeAssistant) -> None:
alert_level="Alert Level 1", alert_level="Alert Level 1",
country="Country 1", country="Country 1",
attribution="Attribution 1", attribution="Attribution 1",
from_date=datetime.datetime(2020, 1, 10, 8, 0, tzinfo=datetime.timezone.utc), from_date=datetime.datetime(2020, 1, 10, 8, 0, tzinfo=datetime.UTC),
to_date=datetime.datetime(2020, 1, 20, 8, 0, tzinfo=datetime.timezone.utc), to_date=datetime.datetime(2020, 1, 20, 8, 0, tzinfo=datetime.UTC),
duration_in_week=1, duration_in_week=1,
population="Population 1", population="Population 1",
severity="Severity 1", severity="Severity 1",
@ -120,12 +120,8 @@ async def test_setup(hass: HomeAssistant) -> None:
ATTR_DESCRIPTION: "Description 1", ATTR_DESCRIPTION: "Description 1",
ATTR_COUNTRY: "Country 1", ATTR_COUNTRY: "Country 1",
ATTR_ATTRIBUTION: "Attribution 1", ATTR_ATTRIBUTION: "Attribution 1",
ATTR_FROM_DATE: datetime.datetime( ATTR_FROM_DATE: datetime.datetime(2020, 1, 10, 8, 0, tzinfo=datetime.UTC),
2020, 1, 10, 8, 0, tzinfo=datetime.timezone.utc ATTR_TO_DATE: datetime.datetime(2020, 1, 20, 8, 0, tzinfo=datetime.UTC),
),
ATTR_TO_DATE: datetime.datetime(
2020, 1, 20, 8, 0, tzinfo=datetime.timezone.utc
),
ATTR_DURATION_IN_WEEK: 1, ATTR_DURATION_IN_WEEK: 1,
ATTR_ALERT_LEVEL: "Alert Level 1", ATTR_ALERT_LEVEL: "Alert Level 1",
ATTR_POPULATION: "Population 1", ATTR_POPULATION: "Population 1",

View File

@ -869,9 +869,7 @@ async def test_humidity_change_dry_trigger_on_long_enough(
hass: HomeAssistant, setup_comp_4 hass: HomeAssistant, setup_comp_4
) -> None: ) -> None:
"""Test if humidity change turn dry on.""" """Test if humidity change turn dry on."""
fake_changed = datetime.datetime( fake_changed = datetime.datetime(1970, 11, 11, 11, 11, 11, tzinfo=datetime.UTC)
1970, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
)
with freeze_time(fake_changed): with freeze_time(fake_changed):
calls = await _setup_switch(hass, False) calls = await _setup_switch(hass, False)
_setup_sensor(hass, 35) _setup_sensor(hass, 35)
@ -905,9 +903,7 @@ async def test_humidity_change_dry_trigger_off_long_enough(
hass: HomeAssistant, setup_comp_4 hass: HomeAssistant, setup_comp_4
) -> None: ) -> None:
"""Test if humidity change turn dry on.""" """Test if humidity change turn dry on."""
fake_changed = datetime.datetime( fake_changed = datetime.datetime(1970, 11, 11, 11, 11, 11, tzinfo=datetime.UTC)
1970, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
)
with freeze_time(fake_changed): with freeze_time(fake_changed):
calls = await _setup_switch(hass, True) calls = await _setup_switch(hass, True)
_setup_sensor(hass, 45) _setup_sensor(hass, 45)
@ -1031,9 +1027,7 @@ async def test_humidity_change_humidifier_trigger_on_long_enough(
hass: HomeAssistant, setup_comp_6 hass: HomeAssistant, setup_comp_6
) -> None: ) -> None:
"""Test if humidity change turn humidifier on after min cycle.""" """Test if humidity change turn humidifier on after min cycle."""
fake_changed = datetime.datetime( fake_changed = datetime.datetime(1970, 11, 11, 11, 11, 11, tzinfo=datetime.UTC)
1970, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
)
with freeze_time(fake_changed): with freeze_time(fake_changed):
calls = await _setup_switch(hass, False) calls = await _setup_switch(hass, False)
_setup_sensor(hass, 45) _setup_sensor(hass, 45)
@ -1053,9 +1047,7 @@ async def test_humidity_change_humidifier_trigger_off_long_enough(
hass: HomeAssistant, setup_comp_6 hass: HomeAssistant, setup_comp_6
) -> None: ) -> None:
"""Test if humidity change turn humidifier off after min cycle.""" """Test if humidity change turn humidifier off after min cycle."""
fake_changed = datetime.datetime( fake_changed = datetime.datetime(1970, 11, 11, 11, 11, 11, tzinfo=datetime.UTC)
1970, 11, 11, 11, 11, 11, tzinfo=datetime.timezone.utc
)
with freeze_time(fake_changed): with freeze_time(fake_changed):
calls = await _setup_switch(hass, True) calls = await _setup_switch(hass, True)
_setup_sensor(hass, 35) _setup_sensor(hass, 35)

View File

@ -48,7 +48,7 @@ async def test_setup(hass: HomeAssistant) -> None:
(38.0, -3.0), (38.0, -3.0),
locality="Locality 1", locality="Locality 1",
attribution="Attribution 1", attribution="Attribution 1",
time=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc), time=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.UTC),
magnitude=5.7, magnitude=5.7,
mmi=5, mmi=5,
depth=10.5, depth=10.5,
@ -93,9 +93,7 @@ async def test_setup(hass: HomeAssistant) -> None:
ATTR_FRIENDLY_NAME: "Title 1", ATTR_FRIENDLY_NAME: "Title 1",
ATTR_LOCALITY: "Locality 1", ATTR_LOCALITY: "Locality 1",
ATTR_ATTRIBUTION: "Attribution 1", ATTR_ATTRIBUTION: "Attribution 1",
ATTR_TIME: datetime.datetime( ATTR_TIME: datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.UTC),
2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc
),
ATTR_MAGNITUDE: 5.7, ATTR_MAGNITUDE: 5.7,
ATTR_DEPTH: 10.5, ATTR_DEPTH: 10.5,
ATTR_MMI: 5, ATTR_MMI: 5,

View File

@ -41,7 +41,7 @@ async def test_setup(hass: HomeAssistant) -> None:
(38.0, -3.0), (38.0, -3.0),
locality="Locality 1", locality="Locality 1",
attribution="Attribution 1", attribution="Attribution 1",
time=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc), time=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.UTC),
magnitude=5.7, magnitude=5.7,
mmi=5, mmi=5,
depth=10.5, depth=10.5,

View File

@ -81,7 +81,7 @@ async def test_setup(hass: HomeAssistant) -> None:
(38.0, -3.0), (38.0, -3.0),
region="Region 1", region="Region 1",
attribution="Attribution 1", attribution="Attribution 1",
published=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc), published=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.UTC),
magnitude=5.7, magnitude=5.7,
image_url="http://image.url/map.jpg", image_url="http://image.url/map.jpg",
) )
@ -125,7 +125,7 @@ async def test_setup(hass: HomeAssistant) -> None:
ATTR_REGION: "Region 1", ATTR_REGION: "Region 1",
ATTR_ATTRIBUTION: "Attribution 1", ATTR_ATTRIBUTION: "Attribution 1",
ATTR_PUBLICATION_DATE: datetime.datetime( ATTR_PUBLICATION_DATE: datetime.datetime(
2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc 2018, 9, 22, 8, 0, tzinfo=datetime.UTC
), ),
ATTR_IMAGE_URL: "http://image.url/map.jpg", ATTR_IMAGE_URL: "http://image.url/map.jpg",
ATTR_MAGNITUDE: 5.7, ATTR_MAGNITUDE: 5.7,

View File

@ -736,7 +736,7 @@ async def test_timestamp(hass: HomeAssistant) -> None:
assert ( assert (
dt_util.as_local( dt_util.as_local(
datetime.datetime.fromtimestamp( datetime.datetime.fromtimestamp(
state_without_tz.attributes[ATTR_TIMESTAMP], datetime.timezone.utc state_without_tz.attributes[ATTR_TIMESTAMP], datetime.UTC
) )
).strftime(FORMAT_DATETIME) ).strftime(FORMAT_DATETIME)
== "2020-12-13 10:00:00" == "2020-12-13 10:00:00"

View File

@ -15,9 +15,7 @@ from .const import DOMAIN, METOFFICE_CONFIG_WAVERTREE, TEST_COORDINATES_WAVERTRE
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
("old_unique_id", "new_unique_id", "migration_needed"), ("old_unique_id", "new_unique_id", "migration_needed"),
[ [

View File

@ -24,9 +24,7 @@ from .const import (
from tests.common import MockConfigEntry, load_fixture from tests.common import MockConfigEntry, load_fixture
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
)
async def test_one_sensor_site_running( async def test_one_sensor_site_running(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant, requests_mock: requests_mock.Mocker
) -> None: ) -> None:
@ -74,9 +72,7 @@ async def test_one_sensor_site_running(
assert sensor.attributes.get("attribution") == ATTRIBUTION assert sensor.attributes.get("attribution") == ATTRIBUTION
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
)
async def test_two_sensor_sites_running( async def test_two_sensor_sites_running(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant, requests_mock: requests_mock.Mocker
) -> None: ) -> None:

View File

@ -23,9 +23,7 @@ from .const import (
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
)
async def test_site_cannot_connect( async def test_site_cannot_connect(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant, requests_mock: requests_mock.Mocker
) -> None: ) -> None:
@ -54,9 +52,7 @@ async def test_site_cannot_connect(
assert sensor is None assert sensor is None
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
)
async def test_site_cannot_update( async def test_site_cannot_update(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant, requests_mock: requests_mock.Mocker
) -> None: ) -> None:
@ -104,9 +100,7 @@ async def test_site_cannot_update(
assert weather.state == STATE_UNAVAILABLE assert weather.state == STATE_UNAVAILABLE
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
)
async def test_one_weather_site_running( async def test_one_weather_site_running(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant, requests_mock: requests_mock.Mocker
) -> None: ) -> None:
@ -189,9 +183,7 @@ async def test_one_weather_site_running(
assert weather.attributes.get("forecast")[3]["wind_bearing"] == "SE" assert weather.attributes.get("forecast")[3]["wind_bearing"] == "SE"
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.UTC))
datetime.datetime(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
)
async def test_two_weather_sites_running( async def test_two_weather_sites_running(
hass: HomeAssistant, requests_mock: requests_mock.Mocker hass: HomeAssistant, requests_mock: requests_mock.Mocker
) -> None: ) -> None:

View File

@ -101,9 +101,7 @@ async def test_setup(hass: HomeAssistant) -> None:
category="Category 1", category="Category 1",
location="Location 1", location="Location 1",
attribution="Attribution 1", attribution="Attribution 1",
publication_date=datetime.datetime( publication_date=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.UTC),
2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc
),
council_area="Council Area 1", council_area="Council Area 1",
status="Status 1", status="Status 1",
entry_type="Type 1", entry_type="Type 1",
@ -148,7 +146,7 @@ async def test_setup(hass: HomeAssistant) -> None:
ATTR_LOCATION: "Location 1", ATTR_LOCATION: "Location 1",
ATTR_ATTRIBUTION: "Attribution 1", ATTR_ATTRIBUTION: "Attribution 1",
ATTR_PUBLICATION_DATE: datetime.datetime( ATTR_PUBLICATION_DATE: datetime.datetime(
2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc 2018, 9, 22, 8, 0, tzinfo=datetime.UTC
), ),
ATTR_FIRE: True, ATTR_FIRE: True,
ATTR_COUNCIL_AREA: "Council Area 1", ATTR_COUNCIL_AREA: "Council Area 1",

View File

@ -80,8 +80,8 @@ async def test_setup(hass: HomeAssistant) -> None:
(38.0, -3.0), (38.0, -3.0),
category="Category 1", category="Category 1",
attribution="Attribution 1", attribution="Attribution 1",
published=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc), published=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.UTC),
updated=datetime.datetime(2018, 9, 22, 8, 10, tzinfo=datetime.timezone.utc), updated=datetime.datetime(2018, 9, 22, 8, 10, tzinfo=datetime.UTC),
status="Status 1", status="Status 1",
) )
mock_entry_2 = _generate_mock_feed_entry("2345", "Title 2", 20.5, (38.1, -3.1)) mock_entry_2 = _generate_mock_feed_entry("2345", "Title 2", 20.5, (38.1, -3.1))
@ -119,10 +119,10 @@ async def test_setup(hass: HomeAssistant) -> None:
ATTR_CATEGORY: "Category 1", ATTR_CATEGORY: "Category 1",
ATTR_ATTRIBUTION: "Attribution 1", ATTR_ATTRIBUTION: "Attribution 1",
ATTR_PUBLICATION_DATE: datetime.datetime( ATTR_PUBLICATION_DATE: datetime.datetime(
2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc 2018, 9, 22, 8, 0, tzinfo=datetime.UTC
), ),
ATTR_UPDATED_DATE: datetime.datetime( ATTR_UPDATED_DATE: datetime.datetime(
2018, 9, 22, 8, 10, tzinfo=datetime.timezone.utc 2018, 9, 22, 8, 10, tzinfo=datetime.UTC
), ),
ATTR_STATUS: "Status 1", ATTR_STATUS: "Status 1",
ATTR_UNIT_OF_MEASUREMENT: UnitOfLength.KILOMETERS, ATTR_UNIT_OF_MEASUREMENT: UnitOfLength.KILOMETERS,

View File

@ -215,9 +215,7 @@ async def test_statistics_during_period(
} }
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2022, 10, 21, 7, 25, tzinfo=datetime.UTC))
datetime.datetime(2022, 10, 21, 7, 25, tzinfo=datetime.timezone.utc)
)
@pytest.mark.parametrize("offset", (0, 1, 2)) @pytest.mark.parametrize("offset", (0, 1, 2))
async def test_statistic_during_period( async def test_statistic_during_period(
recorder_mock: Recorder, recorder_mock: Recorder,
@ -632,9 +630,7 @@ async def test_statistic_during_period(
} }
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2022, 10, 21, 7, 25, tzinfo=datetime.UTC))
datetime.datetime(2022, 10, 21, 7, 25, tzinfo=datetime.timezone.utc)
)
async def test_statistic_during_period_hole( async def test_statistic_during_period_hole(
recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None: ) -> None:
@ -797,9 +793,7 @@ async def test_statistic_during_period_hole(
} }
@pytest.mark.freeze_time( @pytest.mark.freeze_time(datetime.datetime(2022, 10, 21, 7, 25, tzinfo=datetime.UTC))
datetime.datetime(2022, 10, 21, 7, 25, tzinfo=datetime.timezone.utc)
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
("calendar_period", "start_time", "end_time"), ("calendar_period", "start_time", "end_time"),
( (

View File

@ -102,8 +102,8 @@ async def test_setup(hass: HomeAssistant) -> None:
(-31.0, 150.0), (-31.0, 150.0),
place="Location 1", place="Location 1",
attribution="Attribution 1", attribution="Attribution 1",
time=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc), time=datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.UTC),
updated=datetime.datetime(2018, 9, 22, 9, 0, tzinfo=datetime.timezone.utc), updated=datetime.datetime(2018, 9, 22, 9, 0, tzinfo=datetime.UTC),
magnitude=5.7, magnitude=5.7,
status="Status 1", status="Status 1",
entry_type="Type 1", entry_type="Type 1",
@ -143,12 +143,8 @@ async def test_setup(hass: HomeAssistant) -> None:
ATTR_FRIENDLY_NAME: "Title 1", ATTR_FRIENDLY_NAME: "Title 1",
ATTR_PLACE: "Location 1", ATTR_PLACE: "Location 1",
ATTR_ATTRIBUTION: "Attribution 1", ATTR_ATTRIBUTION: "Attribution 1",
ATTR_TIME: datetime.datetime( ATTR_TIME: datetime.datetime(2018, 9, 22, 8, 0, tzinfo=datetime.UTC),
2018, 9, 22, 8, 0, tzinfo=datetime.timezone.utc ATTR_UPDATED: datetime.datetime(2018, 9, 22, 9, 0, tzinfo=datetime.UTC),
),
ATTR_UPDATED: datetime.datetime(
2018, 9, 22, 9, 0, tzinfo=datetime.timezone.utc
),
ATTR_STATUS: "Status 1", ATTR_STATUS: "Status 1",
ATTR_TYPE: "Type 1", ATTR_TYPE: "Type 1",
ATTR_ALERT: "Alert 1", ATTR_ALERT: "Alert 1",

View File

@ -111,7 +111,7 @@ asyncio.set_event_loop_policy = lambda policy: None
def _utcnow() -> datetime.datetime: def _utcnow() -> datetime.datetime:
"""Make utcnow patchable by freezegun.""" """Make utcnow patchable by freezegun."""
return datetime.datetime.now(datetime.timezone.utc) return datetime.datetime.now(datetime.UTC)
dt_util.utcnow = _utcnow # type: ignore[assignment] dt_util.utcnow = _utcnow # type: ignore[assignment]