Skip to content

Commit

Permalink
#29
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed Sep 19, 2023
1 parent c0cd648 commit b9c828e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion custom_components/peaqnext/service/models/sensor_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def _update_sensor_internal(self) -> None:
self.total_consumption_in_kwh,
self.total_duration_in_seconds,
self.consumption_type,
self.custom_consumption_pattern_list
self.custom_consumption_pattern_list,
self.dt_model.get_dt_now().minute if self.update_by == UpdateBy.MINUTE else 0
)
try:
self._all_sequences = get_hours_sorted(
Expand Down
15 changes: 9 additions & 6 deletions custom_components/peaqnext/service/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@


def calculate_consumption_per_hour(
consumption: float, duration_in_seconds: int, consumption_type: ConsumptionType, custom_consumption_pattern: list
consumption: float, duration_in_seconds: int, consumption_type: ConsumptionType, custom_consumption_pattern: list, start_minute: int
) -> list[float]:
if 4 < duration_in_seconds <= 3600:
return [consumption]
# if 4 < duration_in_seconds <= 3600:
# return [consumption]
try:
segments = _get_segments(consumption_type, duration_in_seconds, custom_consumption_pattern)
except Exception as e:
Expand All @@ -34,13 +34,16 @@ def calculate_consumption_per_hour(
ret = []
try:
intret = 0
for t in range(0, duration_in_minutes):
intret += minute_consumption[t]
if (t % 60 == 0 and t > 0) or t == duration_in_minutes - 1:
j = 0
for t in range(start_minute, duration_in_minutes+start_minute):
intret += minute_consumption[j]
if (t % 60 == 0 and t > 0) or t == duration_in_minutes+start_minute - 1:
ret.append(round(intret, 1))
intret = 0
j += 1
except Exception as e:
print(e)
print(f"ret: {ret}")
return ret


Expand Down
18 changes: 17 additions & 1 deletion custom_components/peaqnext/test/test_nextsensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,20 @@ async def test_cheapest_hour_update_hourly():
for s in s.all_sequences:
print(s)

assert 1 > 2
assert 1 > 2

@pytest.mark.asyncio
async def test_price_change_per_minute():
s = NextSensor(consumption_type=ConsumptionType.Flat, name="test", hass_entity_id="sensor.test", total_duration_in_minutes=60, total_consumption_in_kwh=1, update_by=UpdateBy.MINUTE)
s.dt_model.set_date(date(2023,7,30))
s.dt_model.set_hour(0)
s.dt_model.set_minute(0)
await s.async_update_sensor([[0.1, 1],[]])
for seq in s.all_sequences:
print(seq.dt_start, seq.price)
s.dt_model.set_minute(45)
await s.async_update_sensor([[0.1, 1],[]])
print('---')
for seq in s.all_sequences:
print(seq.dt_start, seq.price)
assert 1 > 2

0 comments on commit b9c828e

Please sign in to comment.