denysdovhan / ha-yasno-outages

⚡️ Yasno electricity outages (due to war in Ukraine) integration for Home Assistant.
MIT License
88 stars 12 forks source link

Regression: wrong data displayed after switching to API #14

Closed gentoo-root closed 4 months ago

gentoo-root commented 4 months ago

The recent commit https://github.com/denysdovhan/ha-yasno-outages/commit/6809d1001b8dd3e907243f80447ff3fd8dee0aad introduced a regression: sensor.yasno_group_X_electricity has a high chance of displaying "Connected" when it's an outage. The bug is in this check: for example, if there is an outage from 0:00 till 4:00, and right now is 1:00, we'll have:

start_date = 1:00
end_date = 2:00
event_start = 0:00
event_end = 4:00
1:00 <= 0:00 <= 2:00: False
1:00 <= 4:00 <= 2:00: False

The event is filtered out, even though it clearly intersects with the interval 1:00 .. 2:00.

I prepared a quick and dirty fix in #15 to ensure proper check for intersecting intervals. I believe, though, that the code can be simplified: it seems that it's enough to check event_start <= start_date <= event_end, end_date seems to me unneeded in this function, but I might be missing some clever idea.

denysdovhan commented 4 months ago

The logic is to find only those events that are fit under timeframe. That is why we check for event_end to be in the timeframe.