Skip to content

Commit 6cb6240

Browse files
authored
fix: reduce calls to TIMEZONE.localize (#110)
1 parent 3dd4e81 commit 6cb6240

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

custom_components/iec/coordinator.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ async def _async_update_data(
178178

179179
contracts: dict[int, Contract] = {int(c.contract_id): c for c in all_contracts if c.status == 1
180180
and int(c.contract_id) in self._contract_ids}
181-
181+
localized_today = TIMEZONE.localize(datetime.today())
182182
tariff = await self._get_kwh_tariff()
183183

184184
data = {STATICS_DICT_NAME: {
@@ -219,8 +219,8 @@ async def _async_update_data(
219219
# For some reason, there are differences between sending 2024-03-01 and sending 2024-03-07 (Today)
220220
# So instead of sending the 1st day of the month, just sending today date
221221

222-
monthly_report_req_date: datetime = TIMEZONE.localize(datetime.today().replace(hour=1, minute=0,
223-
second=0, microsecond=0))
222+
monthly_report_req_date: datetime = localized_today.replace(hour=1, minute=0,
223+
second=0, microsecond=0)
224224

225225
devices = await self._get_devices_by_contract_id(contract_id)
226226

@@ -235,7 +235,7 @@ async def _async_update_data(
235235
daily_readings[device.device_number] = remote_reading.data
236236

237237
weekly_future_consumption = None
238-
if TIMEZONE.localize(datetime.today()).day == 1:
238+
if localized_today.day == 1:
239239
# if today's the 1st of the month, "yesterday" is on a different month
240240
yesterday: datetime = monthly_report_req_date - timedelta(days=1)
241241
remote_reading = await self._get_readings(contract_id, device.device_number, device.device_code,
@@ -253,20 +253,21 @@ async def _async_update_data(
253253
daily_readings[device.device_number].sort(key=lambda x: x.date)
254254

255255
await self._verify_daily_readings_exist(daily_readings[device.device_number],
256-
TIMEZONE.localize(datetime.today()) - timedelta(days=1),
256+
localized_today - timedelta(days=1),
257257
device, contract_id)
258258

259259
today_reading_key = str(contract_id) + "-" + device.device_number
260260
today_reading = self._today_readings.get(today_reading_key)
261261

262262
if not today_reading:
263263
today_reading = await self._get_readings(contract_id, device.device_number, device.device_code,
264-
TIMEZONE.localize(datetime.today()),
264+
localized_today,
265265
ReadingResolution.DAILY)
266266
self._today_readings[today_reading_key] = today_reading
267267

268268
await self._verify_daily_readings_exist(daily_readings[device.device_number],
269-
TIMEZONE.localize(datetime.today()), device, contract_id, today_reading)
269+
localized_today, device, contract_id,
270+
today_reading)
270271

271272
# fallbacks for future consumption since IEC api is broken :/
272273
if not future_consumption[device.device_number].future_consumption:
@@ -278,7 +279,7 @@ async def _async_update_data(
278279
future_consumption[device.device_number] = (
279280
self._today_readings.get(today_reading_key).future_consumption_info)
280281
else:
281-
req_date = TIMEZONE.localize(datetime.today()) - timedelta(days=2)
282+
req_date = localized_today - timedelta(days=2)
282283
two_days_ago_reading = await self._get_readings(contract_id, device.device_number,
283284
device.device_code,
284285
req_date,
@@ -314,6 +315,7 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
314315
_LOGGER.debug(f"Updating statistics for IEC Contract {contract_id}")
315316
devices = await self._get_devices_by_contract_id(contract_id)
316317
kwh_price = await self._get_kwh_tariff()
318+
localized_today = TIMEZONE.localize(datetime.today())
317319

318320
if not devices:
319321
_LOGGER.error(f"Failed fetching devices for IEC Contract {contract_id}")
@@ -347,16 +349,15 @@ async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> No
347349
if from_date.hour == 23:
348350
from_date = from_date + timedelta(hours=2)
349351

350-
today = TIMEZONE.localize(datetime.today())
351-
if today.date() == from_date.date():
352+
if localized_today.date() == from_date.date():
352353
_LOGGER.debug("The date to fetch is today or later, replacing it with Today at 01:00:00")
353-
from_date = TIMEZONE.localize(today.replace(hour=1, minute=0, second=0, microsecond=0))
354+
from_date = localized_today.replace(hour=1, minute=0, second=0, microsecond=0)
354355

355356
_LOGGER.debug(f"Fetching consumption from {from_date.strftime('%Y-%m-%d %H:%M:%S')}")
356357
readings = await self._get_readings(contract_id, device.device_number, device.device_code,
357358
from_date,
358359
ReadingResolution.DAILY)
359-
if from_date.date() == today.date():
360+
if from_date.date() == localized_today.date():
360361
self._today_readings[str(contract_id) + "-" + device.device_number] = readings
361362

362363
if not readings or not readings.data:

0 commit comments

Comments
 (0)