Skip to content

Commit

Permalink
Sort pH readings to get the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
tdragon committed Nov 26, 2022
1 parent 3a0b79c commit d2e3a3d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
10 changes: 8 additions & 2 deletions custom_components/reef_pi/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import asyncio
import httpx
import json
from datetime import datetime

REEFPI_DATETIME_FORMAT = "%b-%d-%H:%M, %Y"

class ReefApi:
def __init__(self, host, verify=False, timeout_sec=15):
Expand Down Expand Up @@ -89,11 +92,14 @@ async def phprobes(self):
return await self._get("phprobes")

async def ph(self, id):

get_time = lambda x: datetime.strptime(x['time'], REEFPI_DATETIME_FORMAT) if 'time' in x.keys() else datetime.datetime(0,0,0)

readings = await self._get(f"phprobes/{id}/readings")
if readings and 'current' in readings.keys() and len(readings['current']):
return readings['current'][-1]
return sorted(readings['current'], key=get_time)[-1]
if readings and 'historical' in readings.keys() and len(readings['historical']):
return readings['historical'][-1]
return sorted(readings['historical'], key=get_time)[-1]
return {'value': None}

async def pumps(self):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/reef_pi/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"domain": "reef_pi",
"version": "0.3.4",
"version": "0.3.5",
"name": "Reef PI Integration",
"config_flow": true,
"documentation": "https://github.com/tdragon/reef-pi-hass-custom",
Expand Down
1 change: 1 addition & 0 deletions custom_components/reef_pi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def __init__(self, id, name, coordinator):
_attr_has_entity_name = True
_attr_icon = "mdi:ph"
_attr_native_unit_of_measurement = DEGREE
_attr_state_class = SensorStateClass.MEASUREMENT

@property
def device_info(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/payloads/ph_readings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8638,7 +8638,7 @@
"value": 6.859060402684564,
"up": 0,
"down": 0,
"time": "Nov-24-14:06, 2022"
"time": "Nov-24-19:40, 2022"
}
],
"historical": [
Expand Down
11 changes: 6 additions & 5 deletions tests/ph_sensor_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ async def async_api_mock_instance():

with open(os.path.join(PAYLOAD_DIR, "ph_readings.json"), "rt") as payload:
ph_readings = json.loads(payload.read())
base_number = len(ph_readings['current']) - 3

mock.get(f'{async_api_mock.REEF_MOCK_URL}/api/phprobes/6/readings').mock(side_effect=lambda request, route:
httpx.Response(200, json={
'current': ph_readings['current'][0:route.call_count+1],
'current': ph_readings['current'][0:route.call_count+base_number],
'historical': ph_readings['historical']}))
yield mock

Expand Down Expand Up @@ -59,11 +60,11 @@ async def test_ph(hass, async_api_mock_instance):
assert state
assert state.state == '6.8389'
assert state.name == 'Reef PI pH'
assert state.attributes["time"].isoformat() == "2022-11-23T16:18:00"
assert await waitFor(lambda: hass.states.get("sensor.reef_pi_ph").state, '6.849', 4)
assert state.attributes["time"].isoformat() == "2022-11-24T19:39:00"
await waitFor(lambda: hass.states.get("sensor.reef_pi_ph").state, '6.849', 10)
state = hass.states.get("sensor.reef_pi_ph")
assert state.state == '6.849'
assert state.attributes["time"].isoformat() == "2022-11-23T16:21:00"
assert state.state == '6.8591'
assert state.attributes["time"].isoformat() == "2022-11-24T19:40:00"

async def test_ph_without_current(hass, async_api_mock_instance):
entry = MockConfigEntry(domain=DOMAIN, data={
Expand Down

0 comments on commit d2e3a3d

Please sign in to comment.