diff --git a/aioflo/api.py b/aioflo/api.py index cb59246..524be2d 100644 --- a/aioflo/api.py +++ b/aioflo/api.py @@ -91,8 +91,8 @@ async def _request(self, method: str, url: str, **kwargs) -> dict: try: async with session.request(method, url, **kwargs) as resp: - data: dict = await resp.json(content_type=None) resp.raise_for_status() + data: dict = await resp.json(content_type=None) return data except ClientError as err: raise RequestError(f"There was an error while requesting {url}") from err diff --git a/aioflo/water.py b/aioflo/water.py index 9ded6fe..8386f92 100644 --- a/aioflo/water.py +++ b/aioflo/water.py @@ -1,6 +1,6 @@ """Define /water endpoints.""" from datetime import datetime -from typing import Awaitable, Callable +from typing import Awaitable, Callable, Optional from .const import API_V2_BASE from .util import raise_on_invalid_argument @@ -24,6 +24,7 @@ async def get_consumption_info( start: datetime, end: datetime, interval: str = INTERVAL_HOURLY, + device_mac_address: Optional[str] = None, ) -> dict: """Return user account data. @@ -33,19 +34,25 @@ async def get_consumption_info( :type start: ``datetime.datetime`` :param end: The end datetime of the range to examine :type end: ``datetime.datetime`` + :param device_mac_address: Limit results to a single device at the location + :type device_mac_address: ``Optional[str]`` :rtype: ``dict`` """ raise_on_invalid_argument(interval, INTERVALS) + params = { + "endDate": end.isoformat(), + "interval": interval, + "locationId": location_id, + "startDate": start.isoformat(), + } + if device_mac_address: + params["macAddress"] = device_mac_address.replace(":", "") + return await self._request( "get", f"{API_V2_BASE}/water/consumption", - params={ - "endDate": end.isoformat(), - "interval": interval, - "locationId": location_id, - "startDate": start.isoformat(), - }, + params=params, ) async def get_metrics( diff --git a/pyproject.toml b/pyproject.toml index 4d5f007..3bcf543 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ use_parentheses = true [tool.poetry] name = "aioflo" -version = "2021.11.0" +version = "2021.11.1" description = "A Python3, async-friendly library for Flo by Moen Smart Water Detectors" readme = "README.md" authors = ["Aaron Bach "] diff --git a/tests/test_water.py b/tests/test_water.py index 4c90cb2..14ebd3a 100644 --- a/tests/test_water.py +++ b/tests/test_water.py @@ -34,6 +34,14 @@ async def test_get_consumption_info(aresponses, auth_success_response): text=load_fixture("water_consumption_info_response.json"), status=200 ), ) + aresponses.add( + "api-gw.meetflo.com", + "/api/v2/water/consumption", + "get", + aresponses.Response( + text=load_fixture("water_consumption_info_response.json"), status=200 + ), + ) start = datetime(2020, 1, 16, 0, 0) end = datetime(2020, 1, 16, 23, 59, 59, 999000) @@ -45,6 +53,11 @@ async def test_get_consumption_info(aresponses, auth_success_response): ) assert len(consumption_info["items"]) == 5 + consumption_info = await api.water.get_consumption_info( + TEST_LOCATION_ID, start, end, device_mac_address=TEST_MAC_ADDRESS + ) + assert len(consumption_info["items"]) == 5 + # Test various cases of using invalid parameter values in set_mode_sleep: with pytest.raises(RequestError): await api.water.get_consumption_info(