-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduced the "fetchers" concept and removed broken protocols (#10)
- Loading branch information
Showing
15 changed files
with
281 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from py_air_control_exporter import fetchers_api | ||
from py_air_control_exporter.fetchers import http_philips | ||
|
||
KNOWN_FETCHERS: dict[str, fetchers_api.FetcherCreator] = { | ||
"http": http_philips.create_fetcher, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from pyairctrl import http_client | ||
|
||
from py_air_control_exporter import fetchers_api | ||
from py_air_control_exporter.logging import LOG | ||
|
||
_FAN_SPEED_TO_INT = {"s": 0, "1": 1, "2": 2, "3": 3, "t": 4} | ||
|
||
|
||
def create_fetcher(config: fetchers_api.FetcherCreatorArgs) -> fetchers_api.Fetcher: | ||
return lambda target_host=config.target_host: get_reading(target_host) | ||
|
||
|
||
def get_reading( | ||
host: str, | ||
) -> fetchers_api.TargetReading | None: | ||
client = http_client.HTTPAirClient(host) | ||
|
||
try: | ||
status_data = client.get_status() or {} | ||
filters_data = client.get_filters() or {} | ||
|
||
return fetchers_api.TargetReading( | ||
air_quality=create_air_quality(status_data), | ||
control_info=create_control_info(status_data), | ||
filters=create_filter_info(filters_data), | ||
) | ||
except Exception as ex: | ||
LOG.error( | ||
"Could not read values from air control device %s. Error: %s", | ||
host, | ||
ex, | ||
) | ||
return None | ||
|
||
|
||
def create_air_quality(status_data: dict) -> fetchers_api.AirQuality: | ||
return fetchers_api.AirQuality(iaql=status_data["iaql"], pm25=status_data["pm25"]) | ||
|
||
|
||
def create_control_info(status_data: dict) -> fetchers_api.ControlInfo: | ||
return fetchers_api.ControlInfo( | ||
fan_speed=_FAN_SPEED_TO_INT[status_data["om"]], | ||
is_manual=status_data["mode"] == "M", | ||
is_on=status_data["pwr"] == "1", | ||
) | ||
|
||
|
||
def create_filter_info(filters_data: dict) -> dict[str, fetchers_api.Filter]: | ||
filters: dict[str, fetchers_api.Filter] = {} | ||
for key, value in filters_data.items(): | ||
if key.startswith("fltsts"): | ||
filter_id = key[6:] | ||
filters[filter_id] = fetchers_api.Filter( | ||
hours=value, | ||
filter_type=filters_data.get(f"fltt{filter_id}", ""), | ||
) | ||
|
||
return filters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from collections.abc import Callable | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass(frozen=True) | ||
class AirQuality: | ||
iaql: float # IAI allergen index | ||
pm25: float | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ControlInfo: | ||
fan_speed: float | ||
is_manual: bool | ||
is_on: bool | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Filter: | ||
hours: float # Hours remaining before replacement | ||
filter_type: str | ||
|
||
|
||
@dataclass(frozen=True) | ||
class TargetReading: | ||
air_quality: AirQuality | None | ||
control_info: ControlInfo | None | ||
filters: dict[str, Filter] | None | ||
|
||
|
||
Fetcher = Callable[[], TargetReading | None] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class FetcherCreatorArgs: | ||
target_host: str | ||
target_name: str | ||
|
||
|
||
FetcherCreator = Callable[[FetcherCreatorArgs], Fetcher] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.