Import `util.dt` as `dt_util` in `components/[e-f]*` (#93758)

pull/93668/head
Ville Skyttä 2023-05-30 00:00:11 +03:00 committed by GitHub
parent 6aa01e1441
commit 70c49824d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 42 additions and 40 deletions

View File

@ -16,7 +16,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from .const import DOMAIN, LOGGER, SCAN_INTERVAL, THRESHOLD_HOUR
@ -47,7 +47,7 @@ class EasyEnergyDataUpdateCoordinator(DataUpdateCoordinator[EasyEnergyData]):
async def _async_update_data(self) -> EasyEnergyData:
"""Fetch data from easyEnergy."""
today = dt.now().date()
today = dt_util.now().date()
gas_today = None
energy_tomorrow = None
@ -62,7 +62,7 @@ class EasyEnergyDataUpdateCoordinator(DataUpdateCoordinator[EasyEnergyData]):
except EasyEnergyNoDataError:
LOGGER.debug("No data for gas prices for easyEnergy integration")
# Energy for tomorrow only after 14:00 UTC
if dt.utcnow().hour >= THRESHOLD_HOUR:
if dt_util.utcnow().hour >= THRESHOLD_HOUR:
tomorrow = today + timedelta(days=1)
try:
energy_tomorrow = await self.easyenergy.energy_prices(

View File

@ -16,7 +16,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from .const import DOMAIN, LOGGER, SCAN_INTERVAL, THRESHOLD_HOUR
@ -47,7 +47,7 @@ class EnergyZeroDataUpdateCoordinator(DataUpdateCoordinator[EnergyZeroData]):
async def _async_update_data(self) -> EnergyZeroData:
"""Fetch data from EnergyZero."""
today = dt.now().date()
today = dt_util.now().date()
gas_today = None
energy_tomorrow = None
@ -62,7 +62,7 @@ class EnergyZeroDataUpdateCoordinator(DataUpdateCoordinator[EnergyZeroData]):
except EnergyZeroNoDataError:
LOGGER.debug("No data for gas prices for EnergyZero integration")
# Energy for tomorrow only after 14:00 UTC
if dt.utcnow().hour >= THRESHOLD_HOUR:
if dt_util.utcnow().hour >= THRESHOLD_HOUR:
tomorrow = today + timedelta(days=1)
try:
energy_tomorrow = await self.energyzero.energy_prices(

View File

@ -33,7 +33,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from . import device_info
from .const import DOMAIN
@ -169,7 +169,7 @@ def get_forecast(ec_data, hourly):
return None
today = {
ATTR_FORECAST_TIME: dt.now().isoformat(),
ATTR_FORECAST_TIME: dt_util.now().isoformat(),
ATTR_FORECAST_CONDITION: icon_code_to_condition(
int(half_days[0]["icon_code"])
),
@ -201,7 +201,7 @@ def get_forecast(ec_data, hourly):
forecast_array.append(
{
ATTR_FORECAST_TIME: (
dt.now() + datetime.timedelta(days=day)
dt_util.now() + datetime.timedelta(days=day)
).isoformat(),
ATTR_FORECAST_NATIVE_TEMP: int(half_days[high]["temperature"]),
ATTR_FORECAST_NATIVE_TEMP_LOW: int(half_days[low]["temperature"]),

View File

@ -21,7 +21,7 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from homeassistant.util.enum import try_parse_enum
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
@ -81,7 +81,7 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
if self._state.missing_state:
return None
if self.device_class == SensorDeviceClass.TIMESTAMP:
return dt.utc_from_timestamp(self._state.state)
return dt_util.utc_from_timestamp(self._state.state)
return f"{self._state.state:.{self._static_info.accuracy_decimals}f}"
@property

View File

@ -11,7 +11,7 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import UpdateFailed
from homeassistant.setup import async_setup_component
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from .common import TEST_PASSWORD, TEST_USER_ID
@ -83,7 +83,7 @@ async def test_device(
call_count = aioclient_mock.call_count
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=90))
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=90))
await hass.async_block_till_done()
assert aioclient_mock.call_count == call_count + 6

View File

@ -6,7 +6,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
@ -96,5 +96,5 @@ async def enable_all_entities(hass, config_entry_id, time_till_next_update):
]:
registry.async_update_entity(entry.entity_id, **{"disabled_by": None})
await hass.async_block_till_done()
async_fire_time_changed(hass, dt.utcnow() + time_till_next_update)
async_fire_time_changed(hass, dt_util.utcnow() + time_till_next_update)
await hass.async_block_till_done()

View File

@ -7,7 +7,7 @@ from homeassistant.components.fronius.coordinator import (
FroniusInverterUpdateCoordinator,
)
from homeassistant.core import HomeAssistant
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from . import mock_responses, setup_fronius_integration
@ -26,7 +26,7 @@ async def test_adaptive_update_interval(
mock_inverter_data.reset_mock()
async_fire_time_changed(
hass, dt.utcnow() + FroniusInverterUpdateCoordinator.default_interval
hass, dt_util.utcnow() + FroniusInverterUpdateCoordinator.default_interval
)
await hass.async_block_till_done()
mock_inverter_data.assert_called_once()
@ -36,14 +36,15 @@ async def test_adaptive_update_interval(
# first 3 bad requests at default interval - 4th has different interval
for _ in range(3):
async_fire_time_changed(
hass, dt.utcnow() + FroniusInverterUpdateCoordinator.default_interval
hass,
dt_util.utcnow() + FroniusInverterUpdateCoordinator.default_interval,
)
await hass.async_block_till_done()
assert mock_inverter_data.call_count == 3
mock_inverter_data.reset_mock()
async_fire_time_changed(
hass, dt.utcnow() + FroniusInverterUpdateCoordinator.error_interval
hass, dt_util.utcnow() + FroniusInverterUpdateCoordinator.error_interval
)
await hass.async_block_till_done()
assert mock_inverter_data.call_count == 1
@ -52,14 +53,14 @@ async def test_adaptive_update_interval(
mock_inverter_data.side_effect = None
# next successful request resets to default interval
async_fire_time_changed(
hass, dt.utcnow() + FroniusInverterUpdateCoordinator.error_interval
hass, dt_util.utcnow() + FroniusInverterUpdateCoordinator.error_interval
)
await hass.async_block_till_done()
mock_inverter_data.assert_called_once()
mock_inverter_data.reset_mock()
async_fire_time_changed(
hass, dt.utcnow() + FroniusInverterUpdateCoordinator.default_interval
hass, dt_util.utcnow() + FroniusInverterUpdateCoordinator.default_interval
)
await hass.async_block_till_done()
mock_inverter_data.assert_called_once()
@ -70,7 +71,8 @@ async def test_adaptive_update_interval(
# first 3 requests at default interval - 4th has different interval
for _ in range(3):
async_fire_time_changed(
hass, dt.utcnow() + FroniusInverterUpdateCoordinator.default_interval
hass,
dt_util.utcnow() + FroniusInverterUpdateCoordinator.default_interval,
)
await hass.async_block_till_done()
# BadStatusError does 3 silent retries for inverter endpoint * 3 request intervals = 9

View File

@ -8,7 +8,7 @@ from homeassistant.components.fronius.coordinator import (
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from . import enable_all_entities, mock_responses, setup_fronius_integration
@ -43,7 +43,7 @@ async def test_symo_inverter(
# Second test at daytime when inverter is producing
mock_responses(aioclient_mock, night=False)
async_fire_time_changed(
hass, dt.utcnow() + FroniusInverterUpdateCoordinator.default_interval
hass, dt_util.utcnow() + FroniusInverterUpdateCoordinator.default_interval
)
await hass.async_block_till_done()
assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 56
@ -65,7 +65,7 @@ async def test_symo_inverter(
# Third test at nighttime - additional AC entities default to 0
mock_responses(aioclient_mock, night=True)
async_fire_time_changed(
hass, dt.utcnow() + FroniusInverterUpdateCoordinator.default_interval
hass, dt_util.utcnow() + FroniusInverterUpdateCoordinator.default_interval
)
await hass.async_block_till_done()
assert_state("sensor.symo_20_ac_current", 0)
@ -150,7 +150,7 @@ async def test_symo_power_flow(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test Fronius Symo power flow entities."""
async_fire_time_changed(hass, dt.utcnow())
async_fire_time_changed(hass, dt_util.utcnow())
def assert_state(entity_id, expected_state):
state = hass.states.get(entity_id)
@ -176,7 +176,7 @@ async def test_symo_power_flow(
# Second test at daytime when inverter is producing
mock_responses(aioclient_mock, night=False)
async_fire_time_changed(
hass, dt.utcnow() + FroniusPowerFlowUpdateCoordinator.default_interval
hass, dt_util.utcnow() + FroniusPowerFlowUpdateCoordinator.default_interval
)
await hass.async_block_till_done()
# 54 because power_flow `rel_SelfConsumption` and `P_PV` is not `null` anymore
@ -193,7 +193,7 @@ async def test_symo_power_flow(
# Third test at nighttime - default values are used
mock_responses(aioclient_mock, night=True)
async_fire_time_changed(
hass, dt.utcnow() + FroniusPowerFlowUpdateCoordinator.default_interval
hass, dt_util.utcnow() + FroniusPowerFlowUpdateCoordinator.default_interval
)
await hass.async_block_till_done()
assert len(hass.states.async_all(domain_filter=SENSOR_DOMAIN)) == 54

View File

@ -21,7 +21,7 @@ from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.core import HomeAssistant
from homeassistant.loader import async_get_integration
from homeassistant.setup import async_setup_component
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from tests.common import MockUser, async_capture_events, async_fire_time_changed
from tests.typing import WebSocketGenerator
@ -228,7 +228,7 @@ async def test_themes_save_storage(
)
# To trigger the call_later
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=60))
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=60))
# To execute the save
await hass.async_block_till_done()

View File

@ -15,7 +15,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed
@ -76,7 +76,7 @@ async def test_binary_sensors(
# Test unknown/missing data
mock_fully_kiosk.getDeviceInfo.return_value = {}
async_fire_time_changed(hass, dt.utcnow() + UPDATE_INTERVAL)
async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.amazon_fire_plugged_in")
@ -85,7 +85,7 @@ async def test_binary_sensors(
# Test failed update
mock_fully_kiosk.getDeviceInfo.side_effect = FullyKioskError("error", "status")
async_fire_time_changed(hass, dt.utcnow() + UPDATE_INTERVAL)
async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.amazon_fire_plugged_in")

View File

@ -6,7 +6,7 @@ import homeassistant.components.number as number
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed
@ -52,7 +52,7 @@ async def test_numbers(
# Test invalid numeric data
mock_fully_kiosk.getSettings.return_value = {"screenBrightness": "invalid"}
async_fire_time_changed(hass, dt.utcnow() + UPDATE_INTERVAL)
async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("number.amazon_fire_screen_brightness")
@ -61,7 +61,7 @@ async def test_numbers(
# Test unknown/missing data
mock_fully_kiosk.getSettings.return_value = {}
async_fire_time_changed(hass, dt.utcnow() + UPDATE_INTERVAL)
async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("number.amazon_fire_screensaver_timer")

View File

@ -18,7 +18,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.util import dt
from homeassistant.util import dt as dt_util
from tests.common import MockConfigEntry, async_fire_time_changed
@ -141,7 +141,7 @@ async def test_sensors_sensors(
# Test unknown/missing data
mock_fully_kiosk.getDeviceInfo.return_value = {}
async_fire_time_changed(hass, dt.utcnow() + UPDATE_INTERVAL)
async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("sensor.amazon_fire_internal_storage_free_space")
@ -150,7 +150,7 @@ async def test_sensors_sensors(
# Test failed update
mock_fully_kiosk.getDeviceInfo.side_effect = FullyKioskError("error", "status")
async_fire_time_changed(hass, dt.utcnow() + UPDATE_INTERVAL)
async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("sensor.amazon_fire_internal_storage_free_space")
@ -176,7 +176,7 @@ async def test_url_sensor_truncating(
mock_fully_kiosk.getDeviceInfo.return_value = {
"currentPage": long_url,
}
async_fire_time_changed(hass, dt.utcnow() + UPDATE_INTERVAL)
async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()
state = hass.states.get("sensor.amazon_fire_current_page")