Skip to content

Commit

Permalink
Fix Flo returning stale data (home-assistant#60491)
Browse files Browse the repository at this point in the history
* Fix Flo returning stale data

* update tests

* update coverage
  • Loading branch information
dmulcahey authored Nov 30, 2021
1 parent 9f26850 commit 2f24fc0
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 11 deletions.
10 changes: 9 additions & 1 deletion homeassistant/components/flo/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ async def _async_update_data(self):
try:
async with timeout(10):
await asyncio.gather(
*[self._update_device(), self._update_consumption_data()]
*[
self.send_presence_ping(),
self._update_device(),
self._update_consumption_data(),
]
)
except (RequestError) as error:
raise UpdateFailed(error) from error
Expand Down Expand Up @@ -188,6 +192,10 @@ def battery_level(self) -> float:
"""Return the battery level for battery-powered device, e.g. leak detectors."""
return self._device_information["battery"]["level"]

async def send_presence_ping(self):
"""Send Flo a presence ping."""
await self.api_client.presence.ping()

async def async_set_mode_home(self):
"""Set the Flo location to home mode."""
await self.api_client.location.set_mode_home(self._flo_location_id)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/flo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Flo",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/flo",
"requirements": ["aioflo==0.4.1"],
"requirements": ["aioflo==2021.11.0"],
"codeowners": ["@dmulcahey"],
"iot_class": "cloud_polling"
}
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ aioemonitor==1.0.5
aioesphomeapi==10.3.0

# homeassistant.components.flo
aioflo==0.4.1
aioflo==2021.11.0

# homeassistant.components.yi
aioftp==0.12.0
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ aioemonitor==1.0.5
aioesphomeapi==10.3.0

# homeassistant.components.flo
aioflo==0.4.1
aioflo==2021.11.0

# homeassistant.components.guardian
aioguardian==2021.11.0
Expand Down
7 changes: 7 additions & 0 deletions tests/components/flo/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def aioclient_mock_fixture(aioclient_mock):
headers={"Content-Type": CONTENT_TYPE_JSON},
status=HTTPStatus.OK,
)
# Mocks the presence ping response for flo.
aioclient_mock.post(
"https://api-gw.meetflo.com/api/v2/presence/me",
text=load_fixture("flo/ping_response.json"),
headers={"Content-Type": CONTENT_TYPE_JSON},
status=HTTPStatus.OK,
)
# Mocks the devices for flo.
aioclient_mock.get(
"https://api-gw.meetflo.com/api/v2/devices/98765",
Expand Down
8 changes: 8 additions & 0 deletions tests/components/flo/fixtures/ping_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ipAddress": "11.111.111.111",
"userId": "12345abcde",
"action": "report",
"type": "user",
"appName": "legacy",
"userData": { "account": { "type": "personal" } }
}
16 changes: 15 additions & 1 deletion tests/components/flo/test_device.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Define tests for device-related endpoints."""
from datetime import timedelta
from unittest.mock import patch

from aioflo.errors import RequestError
import pytest

from homeassistant.components.flo.const import DOMAIN as FLO_DOMAIN
from homeassistant.components.flo.device import FloDeviceDataUpdateCoordinator
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers.update_coordinator import UpdateFailed
from homeassistant.setup import async_setup_component
from homeassistant.util import dt

Expand Down Expand Up @@ -67,10 +72,19 @@ async def test_device(hass, config_entry, aioclient_mock_fixture, aioclient_mock
assert detector.model == "puck_v1"
assert detector.manufacturer == "Flo by Moen"
assert detector.device_name == "Kitchen Sink"
assert detector.serial_number == "111111111112"

call_count = aioclient_mock.call_count

async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=90))
await hass.async_block_till_done()

assert aioclient_mock.call_count == call_count + 4
assert aioclient_mock.call_count == call_count + 6

# test error sending device ping
with patch(
"homeassistant.components.flo.device.FloDeviceDataUpdateCoordinator.send_presence_ping",
side_effect=RequestError,
):
with pytest.raises(UpdateFailed):
await valve._async_update_data()
2 changes: 1 addition & 1 deletion tests/components/flo/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ async def test_manual_update_entity(
{ATTR_ENTITY_ID: ["sensor.current_system_mode"]},
blocking=True,
)
assert aioclient_mock.call_count == call_count + 2
assert aioclient_mock.call_count == call_count + 3
10 changes: 5 additions & 5 deletions tests/components/flo/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def test_services(hass, config_entry, aioclient_mock_fixture, aioclient_mo
await hass.async_block_till_done()

assert len(hass.data[FLO_DOMAIN][config_entry.entry_id]["devices"]) == 2
assert aioclient_mock.call_count == 6
assert aioclient_mock.call_count == 8

await hass.services.async_call(
FLO_DOMAIN,
Expand All @@ -35,7 +35,7 @@ async def test_services(hass, config_entry, aioclient_mock_fixture, aioclient_mo
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 7
assert aioclient_mock.call_count == 9

await hass.services.async_call(
FLO_DOMAIN,
Expand All @@ -44,7 +44,7 @@ async def test_services(hass, config_entry, aioclient_mock_fixture, aioclient_mo
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 8
assert aioclient_mock.call_count == 10

await hass.services.async_call(
FLO_DOMAIN,
Expand All @@ -53,7 +53,7 @@ async def test_services(hass, config_entry, aioclient_mock_fixture, aioclient_mo
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 9
assert aioclient_mock.call_count == 11

await hass.services.async_call(
FLO_DOMAIN,
Expand All @@ -66,4 +66,4 @@ async def test_services(hass, config_entry, aioclient_mock_fixture, aioclient_mo
blocking=True,
)
await hass.async_block_till_done()
assert aioclient_mock.call_count == 10
assert aioclient_mock.call_count == 12

0 comments on commit 2f24fc0

Please sign in to comment.