Skip to content

Commit

Permalink
Release 0.3.1: clip_negative_values and get_latest_data
Browse files Browse the repository at this point in the history
get_latest_data implements a convenient public call to fetch from
either "data" or "average" route, as well as allowing to conveniently
toggle negative value clipping (default) and dropping of uncertainties
(default)
  • Loading branch information
Sibgatulin committed Oct 25, 2023
1 parent b4e4c8a commit b8a0d0a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aioairq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__email__ = "daniel.lehmann@corant.de"
__url__ = "https://www.air-q.com"
__license__ = "Apache License 2.0"
__version__ = "0.3.0"
__version__ = "0.3.1"
__all__ = ["AirQ", "DeviceInfo", "InvalidAuth", "InvalidInput", "InvalidAirQResponse"]

from aioairq.core import AirQ, DeviceInfo
Expand Down
31 changes: 28 additions & 3 deletions aioairq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,35 @@ def drop_uncertainties_from_data(data: dict) -> dict:
# `if v else None` is a precaution for the case of v being an empty list
# (which ought not to happen really...)
return {
k: (v[0] if v else None) if isinstance(v, list) else v
k: (v[0] if v else None) if isinstance(v, (list, tuple)) else v
for k, v in data.items()
}

@staticmethod
def clip_negative_values(data: dict) -> dict:
def clip(value):
if isinstance(value, list):
return [max(0, value[0]), value[1]]
elif isinstance(value, (float, int)):
return max(0, value)
else:
return value

return {k: clip(v) for k, v in data.items()}

async def get_latest_data(
self,
return_average=True,
clip_negative_values=True,
return_uncertainties=False,
):
data = await self.get("average" if return_average else "data")
if clip_negative_values:
data = self.clip_negative_values(data)
if not return_uncertainties:
data = self.drop_uncertainties_from_data(data)
return data

async def get(self, subject: str) -> dict:
"""Return the given subject from the air-Q device"""
if subject not in self._supported_routes:
Expand All @@ -133,8 +158,8 @@ async def get(self, subject: str) -> dict:
encoded_message = json.loads(html)["content"]
except (json.JSONDecodeError, KeyError):
raise InvalidAirQResponse(
"AirQ.get() is currently limited to a set of requests, "
f"returning a dict with a key 'content' (namely {self._supported_routes}). "
"AirQ.get() is currently limited to a set of requests, returning "
f"a dict with a key 'content' (namely {self._supported_routes}). "
f"AirQ.get({subject}) returned {html}"
)

Expand Down

0 comments on commit b8a0d0a

Please sign in to comment.