Skip to content

Commit 1be7582

Browse files
author
Vilppu Vuorinen
committed
Improve daily_energy_consumption w/ different client tz
Request some days from the past and some days from the future -> receive the latest day bucket.
1 parent abeb861 commit 1be7582

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

pymelcloud/client.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,19 @@ async def fetch_device_state(self, device) -> Optional[Dict[Any, Any]]:
197197
return await resp.json()
198198

199199
async def fetch_energy_report(self, device) -> Optional[Dict[Any, Any]]:
200-
"""Fetch energy report for the current day.
201-
202-
The energy report rolls over at midnight MELCloud time.
203-
"""
200+
"""Fetch energy report containing today and 1-2 days from the past."""
204201
device_id = device.device_id
205-
today_str = datetime.today().strftime("%Y-%m-%d")
206-
tomorrow_str = (datetime.today() + timedelta(days=1)).strftime("%Y-%m-%d")
202+
from_str = (datetime.today() - timedelta(days=2)).strftime("%Y-%m-%d")
203+
to_str = (datetime.today() + timedelta(days=2)).strftime("%Y-%m-%d")
204+
207205
async with self._session.post(
208206
f"{BASE_URL}/EnergyCost/Report",
209207
headers=_headers(self._token),
210208
json={
211209
"DeviceId": device_id,
212210
"UseCurrency": False,
213-
"FromDate": f"{today_str}T00:00:00",
214-
"ToDate": f"{tomorrow_str}T00:00:00"
211+
"FromDate": f"{from_str}T00:00:00",
212+
"ToDate": f"{to_str}T00:00:00"
215213
},
216214
raise_for_status=True,
217215
) as resp:

pymelcloud/device.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,27 @@ def power(self) -> Optional[bool]:
199199
def daily_energy_consumed(self) -> Optional[float]:
200200
"""Return daily energy consumption for the current day in kWh.
201201
202-
The value resets at midnight MELCloud time.
202+
The value resets at midnight MELCloud time. The logic here is a bit iffy and
203+
fragmented between Device and Client. Here's how it goes:
204+
- Client requests a 5 day report. Today, 2 days from the past and 2 days from
205+
the past.
206+
- MELCloud, with its clock potentially set to a different timezone than the
207+
client, returns a report containing data from a couple of days from the
208+
past and from the current day in MELCloud time.
209+
- Device sums up the date from the last day bucket in the report.
210+
211+
TLDR: Request some days from the past and some days from the future -> receive
212+
the latest day bucket.
203213
"""
204214
if self._energy_report is None:
205215
return None
206-
return (self._energy_report.get("TotalHeatingConsumed", 0.0)
207-
+ self._energy_report.get("TotalCoolingConsumed", 0.0)
208-
+ self._energy_report.get("TotalAutoConsumed", 0.0)
209-
+ self._energy_report.get("TotalDryConsumed", 0.0)
210-
+ self._energy_report.get("TotalFanConsumed", 0.0)
211-
+ self._energy_report.get("TotalOtherConsumed", 0.0))
216+
217+
consumption = 0
218+
219+
for mode in ['Heating', 'Cooling', 'Auto', 'Dry', 'Fan', 'Other']:
220+
consumption += self._energy_report.get(mode, [0.0])[-1]
221+
222+
return consumption
212223

213224
@property
214225
def wifi_signal(self) -> Optional[int]:

tests/test_device.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,13 @@ async def test_round_temperature():
5454
"ata_listdevice.json",
5555
"ata_get.json",
5656
{
57-
"TotalHeatingConsumed": 1.0,
58-
"TotalCoolingConsumed": 0.0,
59-
"TotalAutoConsumed": 2.0,
60-
"TotalDryConsumed": 0.0,
61-
"TotalFanConsumed": 3.0,
57+
"Heating": [0.0, 0.0, 1.0],
58+
"Cooling": [0.0, 0.1, 10.0],
59+
"Dry": [0.2, 0.0, 100.0],
60+
"Fan": [0.3, 1000.0],
6261
},
6362
)
6463

6564
await device.update()
6665

67-
assert device.daily_energy_consumed == 6.0
68-
66+
assert device.daily_energy_consumed == 1111.0

0 commit comments

Comments
 (0)