DarwinsBuddy / WienerNetzeSmartmeter

A home-assistant integration supporting WienerNetze Smartmeters as sensors
121 stars 12 forks source link

Problem with 29.02. #220

Closed J0s3f closed 3 months ago

J0s3f commented 4 months ago

Something in the date-math is broken:

homeassistant | Traceback (most recent call last): homeassistant | File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 573, in _async_add_entity homeassistant | await entity.async_device_update(warning=False) homeassistant | File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 936, in async_device_update homeassistant | await self.async_update() homeassistant | File "/config/custom_components/wnsm/statistics_sensor.py", line 123, in async_update homeassistant | await self._import_bewegungsdaten(smartmeter) homeassistant | File "/config/custom_components/wnsm/statistics_sensor.py", line 187, in _import_bewegungsdaten homeassistant | recording = await self.get_bewegungsdaten(smartmeter) homeassistant | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ homeassistant | File "/config/custom_components/wnsm/base_sensor.py", line 154, in get_bewegungsdaten homeassistant | response = await self.hass.async_add_executor_job( homeassistant | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ homeassistant | File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run homeassistant | result = self.fn(*self.args, **self.kwargs) homeassistant | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ homeassistant | File "/config/custom_components/wnsm/api/client.py", line 479, in bewegungsdaten homeassistant | date_from = date_until.replace(year=date_until.year - 3) homeassistant | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ homeassistant | ValueError: day is out of range for month

reox commented 4 months ago

haha wow, okay - that is a funny bug.

To reproduce:

import datetime
datetime.datetime(2024, 2, 29).replace(year=2021)

Looks like that instead of replace one should better use from dateutil.relativedelta import relativedelta to calculate such things.

DarwinsBuddy commented 3 months ago

neat. Thanks @reox I didn't know of the existence of relativetimedelta. :)