mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Improve the performance of dt_util.utcnow() (#39145)
This commit is contained in:
parent
3cc099af80
commit
42227c1c53
@ -52,7 +52,7 @@ def get_time_zone(time_zone_str: str) -> Optional[dt.tzinfo]:
|
|||||||
|
|
||||||
def utcnow() -> dt.datetime:
|
def utcnow() -> dt.datetime:
|
||||||
"""Get now in UTC time."""
|
"""Get now in UTC time."""
|
||||||
return dt.datetime.now(UTC)
|
return dt.datetime.utcnow().replace(tzinfo=UTC)
|
||||||
|
|
||||||
|
|
||||||
def now(time_zone: Optional[dt.tzinfo] = None) -> dt.datetime:
|
def now(time_zone: Optional[dt.tzinfo] = None) -> dt.datetime:
|
||||||
|
@ -1 +1,12 @@
|
|||||||
"""Tests for the metoffice component."""
|
"""Tests for the metoffice component."""
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class NewDateTime(datetime.datetime):
|
||||||
|
"""Patch time to a specific point."""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def now(cls, *args, **kwargs): # pylint: disable=signature-differs
|
||||||
|
"""Overload datetime.datetime.now."""
|
||||||
|
return cls(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
"""The tests for the Met Office sensor component."""
|
"""The tests for the Met Office sensor component."""
|
||||||
from datetime import datetime, timezone
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from homeassistant.components.metoffice.const import ATTRIBUTION, DOMAIN
|
from homeassistant.components.metoffice.const import ATTRIBUTION, DOMAIN
|
||||||
|
|
||||||
|
from . import NewDateTime
|
||||||
from .const import (
|
from .const import (
|
||||||
DATETIME_FORMAT,
|
DATETIME_FORMAT,
|
||||||
KINGSLYNN_SENSOR_RESULTS,
|
KINGSLYNN_SENSOR_RESULTS,
|
||||||
@ -15,13 +15,12 @@ from .const import (
|
|||||||
WAVERTREE_SENSOR_RESULTS,
|
WAVERTREE_SENSOR_RESULTS,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.async_mock import Mock, patch
|
from tests.async_mock import patch
|
||||||
from tests.common import MockConfigEntry, load_fixture
|
from tests.common import MockConfigEntry, load_fixture
|
||||||
|
|
||||||
|
|
||||||
@patch(
|
@patch(
|
||||||
"datapoint.Forecast.datetime.datetime",
|
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
|
||||||
)
|
)
|
||||||
async def test_one_sensor_site_running(hass, requests_mock, legacy_patchable_time):
|
async def test_one_sensor_site_running(hass, requests_mock, legacy_patchable_time):
|
||||||
"""Test the Met Office sensor platform."""
|
"""Test the Met Office sensor platform."""
|
||||||
@ -58,8 +57,7 @@ async def test_one_sensor_site_running(hass, requests_mock, legacy_patchable_tim
|
|||||||
|
|
||||||
|
|
||||||
@patch(
|
@patch(
|
||||||
"datapoint.Forecast.datetime.datetime",
|
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
|
||||||
)
|
)
|
||||||
async def test_two_sensor_sites_running(hass, requests_mock, legacy_patchable_time):
|
async def test_two_sensor_sites_running(hass, requests_mock, legacy_patchable_time):
|
||||||
"""Test we handle two sets of sensors running for two different sites."""
|
"""Test we handle two sets of sensors running for two different sites."""
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
"""The tests for the Met Office sensor component."""
|
"""The tests for the Met Office sensor component."""
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import timedelta
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from homeassistant.components.metoffice.const import DOMAIN
|
from homeassistant.components.metoffice.const import DOMAIN
|
||||||
from homeassistant.const import STATE_UNAVAILABLE
|
from homeassistant.const import STATE_UNAVAILABLE
|
||||||
from homeassistant.util import utcnow
|
from homeassistant.util import utcnow
|
||||||
|
|
||||||
|
from . import NewDateTime
|
||||||
from .const import (
|
from .const import (
|
||||||
METOFFICE_CONFIG_KINGSLYNN,
|
METOFFICE_CONFIG_KINGSLYNN,
|
||||||
METOFFICE_CONFIG_WAVERTREE,
|
METOFFICE_CONFIG_WAVERTREE,
|
||||||
WAVERTREE_SENSOR_RESULTS,
|
WAVERTREE_SENSOR_RESULTS,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.async_mock import Mock, patch
|
from tests.async_mock import patch
|
||||||
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
||||||
|
|
||||||
|
|
||||||
@patch(
|
@patch(
|
||||||
"datapoint.Forecast.datetime.datetime",
|
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
|
||||||
)
|
)
|
||||||
async def test_site_cannot_connect(hass, requests_mock, legacy_patchable_time):
|
async def test_site_cannot_connect(hass, requests_mock, legacy_patchable_time):
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle cannot connect error."""
|
||||||
@ -39,8 +39,7 @@ async def test_site_cannot_connect(hass, requests_mock, legacy_patchable_time):
|
|||||||
|
|
||||||
|
|
||||||
@patch(
|
@patch(
|
||||||
"datapoint.Forecast.datetime.datetime",
|
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
|
||||||
)
|
)
|
||||||
async def test_site_cannot_update(hass, requests_mock, legacy_patchable_time):
|
async def test_site_cannot_update(hass, requests_mock, legacy_patchable_time):
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle cannot connect error."""
|
||||||
@ -74,8 +73,7 @@ async def test_site_cannot_update(hass, requests_mock, legacy_patchable_time):
|
|||||||
|
|
||||||
|
|
||||||
@patch(
|
@patch(
|
||||||
"datapoint.Forecast.datetime.datetime",
|
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
|
||||||
)
|
)
|
||||||
async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_time):
|
async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_time):
|
||||||
"""Test the Met Office weather platform."""
|
"""Test the Met Office weather platform."""
|
||||||
@ -108,8 +106,7 @@ async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_ti
|
|||||||
|
|
||||||
|
|
||||||
@patch(
|
@patch(
|
||||||
"datapoint.Forecast.datetime.datetime",
|
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
|
||||||
)
|
)
|
||||||
async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_time):
|
async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_time):
|
||||||
"""Test we handle two different weather sites both running."""
|
"""Test we handle two different weather sites both running."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user