Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass data as dataclass object #10

Merged
merged 4 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from aiohttp import ClientError, ClientSession
from gios import ApiError, InvalidSensorsData, Gios, NoStationError

GIOS_STATION_ID = 117
GIOS_STATION_ID = 568

logging.basicConfig(level=logging.DEBUG)

Expand Down
18 changes: 12 additions & 6 deletions gios/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Dict, List, Optional, cast

from aiohttp import ClientSession
from dacite import from_dict

from .const import (
ATTR_AQI,
Expand All @@ -20,6 +21,7 @@
URL_STATION,
URL_STATIONS,
)
from .model import GiosSensors

_LOGGER = logging.getLogger(__name__)

Expand All @@ -37,7 +39,7 @@ def __init__(self, station_id: int, session: ClientSession) -> None:

self.session = session

async def async_update(self) -> Dict[str, Any]: # pylint:disable=too-many-branches
async def async_update(self) -> GiosSensors: # pylint:disable=too-many-branches
"""Update GIOS data."""
data: Dict[str, Dict[str, Any]] = {}
invalid_sensors: List[str] = []
Expand Down Expand Up @@ -99,12 +101,16 @@ async def async_update(self) -> Dict[str, Any]: # pylint:disable=too-many-branc
sensor_data[ATTR_INDEX] = indexes[index_level]["indexLevelName"].lower()

with suppress(IndexError, KeyError, TypeError):
data[ATTR_AQI.lower()] = {ATTR_NAME: ATTR_AQI}
data[ATTR_AQI.lower()][ATTR_VALUE] = indexes["stIndexLevel"][
"indexLevelName"
].lower()
if indexes["stIndexLevel"]["indexLevelName"]:
data[ATTR_AQI.lower()] = {ATTR_NAME: ATTR_AQI}
data[ATTR_AQI.lower()][ATTR_VALUE] = indexes["stIndexLevel"][
"indexLevelName"
].lower()

return data
if data.get("pm2.5"):
data["pm25"] = data.pop("pm2.5")

return from_dict(data_class=GiosSensors, data=data)

async def _get_stations(self) -> List[Dict[str, Any]]:
"""Retreive list of measuring stations."""
Expand Down
27 changes: 27 additions & 0 deletions gios/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Type definitions for GIOS."""
from dataclasses import dataclass
from typing import Optional, Union


@dataclass
class Sensor:
"""Data class for sensor."""

name: str
id: Optional[int] # pylint: disable=invalid-name
index: Optional[str] = None
value: Optional[Union[float, str]] = None


@dataclass
class GiosSensors: # pylint: disable=too-many-instance-attributes
"""Data class for polutants."""

aqi: Optional[Sensor]
c6h6: Optional[Sensor]
co: Optional[Sensor] # pylint: disable=invalid-name
no2: Optional[Sensor]
o3: Optional[Sensor] # pylint: disable=invalid-name
pm10: Optional[Sensor]
pm25: Optional[Sensor]
so2: Optional[Sensor]
Empty file added gios/py.typed
Empty file.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
aiohttp
aiohttp
dacite
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="gios",
version="1.0.2",
version="2.0.0",
author="Maciej Bieniek",
description="Python wrapper for getting air quality data from GIOŚ servers.",
long_description=long_description,
Expand All @@ -16,6 +16,7 @@
url="https://github.com/bieniu/gios",
license="Apache-2.0 License",
packages=["gios"],
package_data={"gios": ["py.typed"]},
python_requires=">=3.6",
install_requires=list(val.strip() for val in open("requirements.txt")),
classifiers=[
Expand Down
93 changes: 45 additions & 48 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,21 @@ async def test_valid_data_first_value(): # pylint:disable=too-many-statements
assert gios.station_id == VALID_STATION_ID
assert gios.latitude == VALID_LATITUDE
assert gios.longitude == VALID_LONGITUDE
assert len(data) == 8
assert data["so2"]["value"] == 11.6502
assert data["so2"]["index"] == "very good"
assert data["c6h6"]["value"] == 2.57148
assert data["c6h6"]["index"] == "very good"
assert data["co"]["value"] == 786.702
assert data["co"]["index"] == "very good"
assert data["no2"]["value"] == 59.9545
assert data["no2"]["index"] == "very good"
assert data["o3"]["value"] == 8.63111
assert data["o3"]["index"] == "good"
assert data["pm2.5"]["value"] == 59.9428
assert data["pm2.5"]["index"] == "very good"
assert data["pm10"]["value"] == 123.879
assert data["pm10"]["index"] == "very good"
assert data["aqi"]["value"] == "good"
assert data.so2.value == 11.6502
assert data.so2.index == "very good"
assert data.c6h6.value == 2.57148
assert data.c6h6.index == "very good"
assert data.co.value == 786.702
assert data.co.index == "very good"
assert data.no2.value == 59.9545
assert data.no2.index == "very good"
assert data.o3.value == 8.63111
assert data.o3.index == "good"
assert data.pm25.value == 59.9428
assert data.pm25.index == "very good"
assert data.pm10.value == 123.879
assert data.pm10.index == "very good"
assert data.aqi.value == "good"


@pytest.mark.asyncio
Expand Down Expand Up @@ -214,22 +213,21 @@ async def test_valid_data_second_value(): # pylint:disable=too-many-statements
assert gios.station_id == VALID_STATION_ID
assert gios.latitude == VALID_LATITUDE
assert gios.longitude == VALID_LONGITUDE
assert len(data) == 8
assert data["so2"]["value"] == 11.501
assert data["so2"]["index"] == "very good"
assert data["c6h6"]["value"] == 3.24432
assert data["c6h6"]["index"] == "very good"
assert data["co"]["value"] == 1041.74
assert data["co"]["index"] == "very good"
assert data["no2"]["value"] == 52.6198
assert data["no2"]["index"] == "very good"
assert data["o3"]["value"] == 4.93778
assert data["o3"]["index"] == "good"
assert data["pm2.5"]["value"] == 72.0243
assert data["pm2.5"]["index"] == "very good"
assert data["pm10"]["value"] == 115.559
assert data["pm10"]["index"] == "very good"
assert data["aqi"]["value"] == "good"
assert data.so2.value == 11.501
assert data.so2.index == "very good"
assert data.c6h6.value == 3.24432
assert data.c6h6.index == "very good"
assert data.co.value == 1041.74
assert data.co.index == "very good"
assert data.no2.value == 52.6198
assert data.no2.index == "very good"
assert data.o3.value == 4.93778
assert data.o3.index == "good"
assert data.pm25.value == 72.0243
assert data.pm25.index == "very good"
assert data.pm10.value == 115.559
assert data.pm10.index == "very good"
assert data.aqi.value == "good"


@pytest.mark.asyncio
Expand Down Expand Up @@ -307,22 +305,21 @@ async def test_no_indexes_data(): # pylint: disable=too-many-statements
assert gios.station_id == VALID_STATION_ID
assert gios.latitude == VALID_LATITUDE
assert gios.longitude == VALID_LONGITUDE
assert len(data) == 8
assert data["so2"]["value"] == 11.6502
assert data["so2"].get("index") is None
assert data["c6h6"]["value"] == 2.57148
assert data["c6h6"].get("index") is None
assert data["co"]["value"] == 786.702
assert data["co"].get("index") is None
assert data["no2"]["value"] == 59.9545
assert data["no2"].get("index") is None
assert data["o3"]["value"] == 8.63111
assert data["o3"].get("index") is None
assert data["pm2.5"]["value"] == 59.9428
assert data["pm2.5"].get("index") is None
assert data["pm10"]["value"] == 123.879
assert data["pm10"].get("index") is None
assert data["aqi"].get("value") is None
assert data.so2.value == 11.6502
assert data.so2.index is None
assert data.c6h6.value == 2.57148
assert data.c6h6.index is None
assert data.co.value == 786.702
assert data.co.index is None
assert data.no2.value == 59.9545
assert data.no2.index is None
assert data.o3.value == 8.63111
assert data.o3.index is None
assert data.pm25.value == 59.9428
assert data.pm25.index is None
assert data.pm10.value == 123.879
assert data.pm10.index is None
assert data.aqi is None


@pytest.mark.asyncio
Expand Down