Skip to content

Commit

Permalink
Merge pull request #48 from pawkakol1/feature/forecast-for-values-wit…
Browse files Browse the repository at this point in the history
…hout-current-value

Feature/forecast for values without current value
  • Loading branch information
pawkakol1 committed Nov 2, 2023
2 parents d1b8048 + 00bfa3d commit 3a04f83
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ If station's API supports forecast for pollution sensors (these on above list):

Pollution sensors will update forecast, and it will be able to read as attributes of the sensor:

<img src="https://github.com/pawkakol1/worlds-air-quality-index/tree/main/readme_files/forecast.png">
<img src="https://github.com/pawkakol1/worlds-air-quality-index/tree/main/readme_files/forecast.png" height="414">

If the station supports forecast of some pollution sensor, but it doesn't support actual value of this sensor, then the forecast will be omitted.
If the station supports forecast of some pollution sensor, but it doesn't support actual value of this sensor, then the sensor will be also ceated. Its current state will be set to "UNAVAILABLE", but forecast will be able to read as the sensor attributes.

There are 2 supported integration methods:

Expand Down
2 changes: 1 addition & 1 deletion custom_components/worlds_air_quality_index/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

DOMAIN = "worlds_air_quality_index"
PLATFORMS = [Platform.SENSOR]
SW_VERSION = "1.0.2"
SW_VERSION = "1.0.4"

DEFAULT_NAME = 'waqi1'
DISCOVERY_TYPE = "discovery_type"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/worlds_air_quality_index/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"issue_tracker": "https://github.com/pawkakol1/worlds-air-quality-index/issues",
"requirements": [],
"ssdp": [],
"version": "1.0.2",
"version": "1.0.4",
"zeroconf": []
}
35 changes: 24 additions & 11 deletions custom_components/worlds_air_quality_index/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
CONF_NAME,
CONF_TEMPERATURE_UNIT,
CONF_TOKEN,
STATE_UNAVAILABLE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
Expand Down Expand Up @@ -102,13 +103,19 @@ async def async_setup_entry(

if not "forecast" in scannedData['data']:
_LOGGER.warning(f"Station {name} doesn't support forecast")
scannedDataForecast = None
else:
scannedDataForecast = scannedData['data']['forecast']['daily']
scannedDataSensors = scannedData["data"]["iaqi"]

entities = []

for res in SENSORS:
if res == "aqi" or res in scannedDataSensors:
entities.append(WorldsAirQualityIndexSensor(res, requester, tempUnit))
elif scannedDataForecast is not None:
if res in scannedDataForecast:
entities.append(WorldsAirQualityIndexSensor(res, requester, tempUnit))

async_add_entities(entities, update_before_add=True)

Expand Down Expand Up @@ -175,25 +182,31 @@ def update(self) -> None:
self._data = self._requester.GetData()
self._updateLastTime = self._requester.GetUpdateLastTime()

self._attr_extra_state_attributes = {
"StationName": self._requester.GetStationName(),
"LastUpdate": self._requester.GetUpdateLastTime()
}

if self._resType == 'aqi':
if self._data["data"]["aqi"] == "-":
_LOGGER.warning("aqi value from json waqi api was undefined ('-' value)")
self._state = 0
else:
self._state = int(self._data["data"]["aqi"])
elif self._resType == 't':
if self._tempUnit == TEMP_FAHRENHEIT:
self._state = 9.0 * float(self._data["data"]["iaqi"]['t']["v"]) / 5.0 + 32.0
self._attr_extra_state_attributes['dominentpol'] = self._data["data"]["dominentpol"]

elif self._resType in self._data["data"]["iaqi"]:
if self._resType == 't':
if self._tempUnit == TEMP_FAHRENHEIT:
self._state = 9.0 * float(self._data["data"]["iaqi"]['t']["v"]) / 5.0 + 32.0
else:
self._state = float(self._data["data"]["iaqi"]['t']["v"])
else:
self._state = float(self._data["data"]["iaqi"]['t']["v"])
else:
self._state = float(self._data["data"]["iaqi"][self._resType]["v"])

self._state = float(self._data["data"]["iaqi"][self._resType]["v"])
elif "forecast" in self._data['data']:
if self._resType in self._data['data']['forecast']['daily']:
self._state = STATE_UNAVAILABLE

self._attr_extra_state_attributes = {
"StationName": self._requester.GetStationName(),
"LastUpdate": self._requester.GetUpdateLastTime()
}
if "forecast" in self._data['data']:
if self._resType in self._data['data']['forecast']['daily']:
scannedDataForecast = self._data['data']['forecast']['daily'][self._resType]
Expand Down

0 comments on commit 3a04f83

Please sign in to comment.