Skip to content

Commit ebc9f62

Browse files
Remove deprecated utcnow
1 parent 8a47dc3 commit ebc9f62

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

custom_components/battery_notes/button.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from homeassistant.helpers.reload import async_setup_reload_service
3535

3636
from . import PLATFORMS
37+
from .common import utcnow_no_timezone
3738
from .const import (
3839
ATTR_BATTERY_QUANTITY,
3940
ATTR_BATTERY_TYPE,
@@ -240,7 +241,7 @@ async def async_added_to_hass(self) -> None:
240241

241242
async def async_press(self) -> None:
242243
"""Press the button."""
243-
self.coordinator.last_replaced = datetime.utcnow()
244+
self.coordinator.last_replaced = utcnow_no_timezone()
244245

245246
self.hass.bus.async_fire(
246247
EVENT_BATTERY_REPLACED,

custom_components/battery_notes/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Common functions for battery_notes."""
22

3+
import datetime
4+
35
from homeassistant.helpers.device_registry import DeviceEntry
6+
from homeassistant.util import dt as dt_util
47

58

69
def validate_is_float(num):
@@ -13,6 +16,10 @@ def validate_is_float(num):
1316
return False
1417
return False
1518

19+
def utcnow_no_timezone() -> datetime:
20+
"""Return UTC now without timezone information."""
21+
22+
return dt_util.utcnow().replace(tzinfo=None)
1623

1724
def get_device_model_id(device_entry: DeviceEntry) -> str | None:
1825
"""Get the device model if available."""

custom_components/battery_notes/coordinator.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
from homeassistant.helpers import issue_registry as ir
2525
from homeassistant.helpers.entity_registry import RegistryEntry
2626
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
27-
from homeassistant.util import dt as dt_util
2827
from homeassistant.util.hass_dict import HassKey
2928

30-
from .common import validate_is_float
29+
from .common import utcnow_no_timezone, validate_is_float
3130
from .const import (
3231
ATTR_BATTERY_LAST_REPLACED,
3332
ATTR_BATTERY_LEVEL,
@@ -194,7 +193,7 @@ def __init__(
194193

195194
# If there is not a last_reported set to now
196195
if not self.last_reported:
197-
last_reported = datetime.utcnow()
196+
last_reported = utcnow_no_timezone()
198197
_LOGGER.debug(
199198
"Defaulting %s battery last reported to %s",
200199
self.source_entity_id or self.device_id,
@@ -580,7 +579,7 @@ def current_battery_level(self, value):
580579
_LOGGER.debug("battery_increased event fired")
581580

582581
if self._current_battery_level not in [STATE_UNAVAILABLE, STATE_UNKNOWN]:
583-
self.last_reported = dt_util.utcnow().replace(tzinfo=None)
582+
self.last_reported = utcnow_no_timezone()
584583
self.last_reported_level = cast(float, self._current_battery_level)
585584
self._previous_battery_low = self.battery_low
586585
self._previous_battery_level = self._current_battery_level

custom_components/battery_notes/filters.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import logging
44
import statistics
55
from collections import Counter, deque
6-
from datetime import datetime, timedelta
6+
from datetime import timedelta
77
from numbers import Number
88
from typing import cast
99

10+
from .common import utcnow_no_timezone
1011
from .const import WINDOW_SIZE_UNIT_NUMBER_EVENTS, WINDOW_SIZE_UNIT_TIME
1112

1213
_LOGGER = logging.getLogger(__name__)
@@ -19,7 +20,7 @@ class FilterState:
1920

2021
def __init__(self, state: str | float | int) -> None:
2122
"""Initialize with HA State object."""
22-
self.timestamp = datetime.utcnow()
23+
self.timestamp = utcnow_no_timezone()
2324
try:
2425
self.state = float(state)
2526
except ValueError:

custom_components/battery_notes/sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
CoordinatorEntity,
5454
)
5555

56-
from .common import validate_is_float
56+
from .common import utcnow_no_timezone, validate_is_float
5757
from .const import (
5858
ATTR_BATTERY_LAST_REPLACED,
5959
ATTR_BATTERY_LAST_REPORTED,
@@ -403,7 +403,7 @@ async def async_state_reported_listener(
403403

404404
await self.coordinator.async_request_refresh()
405405

406-
self.coordinator.last_reported = datetime.utcnow()
406+
self.coordinator.last_reported = utcnow_no_timezone()
407407

408408
_LOGGER.debug(
409409
"Entity id %s has been reported.",

custom_components/battery_notes/services.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from homeassistant.helpers import entity_registry as er
1414
from homeassistant.util import dt as dt_util
1515

16+
from .common import utcnow_no_timezone
1617
from .const import (
1718
ATTR_BATTERY_LAST_REPLACED,
1819
ATTR_BATTERY_LAST_REPORTED,
@@ -59,7 +60,7 @@ async def handle_battery_replaced(call: ServiceCall) -> ServiceResponse:
5960
tzinfo=None
6061
)
6162
else:
62-
datetime_replaced = datetime.utcnow()
63+
datetime_replaced = utcnow_no_timezone()
6364

6465
entity_registry = er.async_get(hass)
6566
device_registry = dr.async_get(hass)
@@ -173,7 +174,7 @@ async def handle_battery_last_reported(call: ServiceCall) -> ServiceResponse:
173174

174175
if coordinator.wrapped_battery and coordinator.last_reported:
175176
time_since_lastreported = (
176-
datetime.fromisoformat(str(datetime.utcnow()) + "+00:00")
177+
datetime.fromisoformat(str(utcnow_no_timezone()) + "+00:00")
177178
- coordinator.last_reported
178179
)
179180

0 commit comments

Comments
 (0)