Fix Flo returning stale data (#60491)

* Fix Flo returning stale data

* update tests

* update coverage
This commit is contained in:
David F. Mulcahey 2021-11-29 20:00:39 -05:00 committed by GitHub
parent 9f26850a19
commit 2f24fc0fd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 48 additions and 11 deletions

View File

@ -42,7 +42,11 @@ class FloDeviceDataUpdateCoordinator(DataUpdateCoordinator):
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
@ -188,6 +192,10 @@ class FloDeviceDataUpdateCoordinator(DataUpdateCoordinator):
"""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)

View File

@ -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"
}

View File

@ -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

View File

@ -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

View File

@ -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",

View File

@ -0,0 +1,8 @@
{
"ipAddress": "11.111.111.111",
"userId": "12345abcde",
"action": "report",
"type": "user",
"appName": "legacy",
"userData": { "account": { "type": "personal" } }
}

View File

@ -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
@ -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()

View File

@ -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

View File

@ -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,
@ -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,
@ -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,
@ -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,
@ -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