Skip to content

Commit

Permalink
Fix watering report bug (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaskistler authored Dec 25, 2023
1 parent cacc69a commit 729eab0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
28 changes: 25 additions & 3 deletions pydrawise/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,35 @@ async def get_watering_report(
self._schema.Controller.reports.select(
self._schema.Reports.watering(
**{
"from": DateTime.to_json(start).timestamp,
"until": DateTime.to_json(end).timestamp,
"from": int(start.timestamp()),
"until": int(end.timestamp()),
}
).select(*get_selectors(self._schema, WateringReportEntry)),
),
)
result = await self._query(selector)
return deserialize(
entries = deserialize(
list[WateringReportEntry], result["controller"]["reports"]["watering"]
)
# The call to watering() can return events outside of the provided time interval.
# Filter out events that happen before or after the provided time interval.
return list(
filter(
lambda entry: entry.run_event is not None
and entry.run_event.reported_start_time is not None
and entry.run_event.reported_end_time is not None
and (
(
start.timestamp()
<= entry.run_event.reported_start_time.timestamp()
<= end.timestamp()
)
or (
start.timestamp()
<= entry.run_event.reported_end_time.timestamp()
<= end.timestamp()
)
),
entries,
)
)
9 changes: 5 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ def watering_report_json():
"standardProgram": None,
"advancedProgram": {"id": 4729362, "name": ""},
"reportedStartTime": {
"value": "Fri, 01 Dec 23 04:19:59 -0800",
"timestamp": 1701433199,
"value": "Fri, 01 Nov 23 04:19:59 -0800",
"timestamp": 1698797999,
},
"reportedEndTime": {
"value": "Fri, 01 Dec 23 04:39:59 -0800",
"timestamp": 1701434399,
"value": "Fri, 01 Nov 23 04:39:59 -0800",
"timestamp": 1698799199,
},
"reportedDuration": 1200,
"reportedStatus": {
Expand Down Expand Up @@ -550,3 +550,4 @@ async def test_get_watering_report(
query = print_ast(selector)
assert "reports" in query
assert "watering" in query
assert len(report) == 1

0 comments on commit 729eab0

Please sign in to comment.