Skip to content

Commit

Permalink
fixes #4, fixes #5, version 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Groch committed Jun 23, 2024
1 parent c310ea3 commit d235044
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
14 changes: 7 additions & 7 deletions custom_components/esa_nask/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from datetime import timedelta

from homeassistant.const import (
TEMP_CELSIUS,
PRESSURE_HPA,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
PERCENTAGE,
Platform
Platform,
UnitOfTemperature,
UnitOfPressure
)
from homeassistant.components.sensor import (
SensorDeviceClass,
Expand All @@ -16,15 +16,15 @@

DOMAIN = "esa_nask"
PLATFORMS = [Platform.SENSOR]
SW_VERSION = "1.0.2"
SW_VERSION = "1.0.3"

SCAN_INTERVAL = timedelta(minutes=10)

SENSORS = {
'pm10': ['PM10', CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, 'mdi:molecule', SensorDeviceClass.PM10, SensorStateClass.MEASUREMENT],
'pm25': ['PM2.5', CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, 'mdi:molecule', SensorDeviceClass.PM25, SensorStateClass.MEASUREMENT],
'humidity': ['Humidity', PERCENTAGE, 'mdi:water-percent', SensorDeviceClass.HUMIDITY, SensorStateClass.MEASUREMENT],
'pressure': ['Pressure', PRESSURE_HPA, 'mdi:gauge', SensorDeviceClass.PRESSURE, SensorStateClass.MEASUREMENT],
'temperature': ['Temperature', TEMP_CELSIUS, 'mdi:thermometer', SensorDeviceClass.TEMPERATURE, SensorStateClass.MEASUREMENT],
'dewPoint': ['Dew Point', TEMP_CELSIUS, 'mdi:thermometer-lines', SensorDeviceClass.TEMPERATURE, SensorStateClass.MEASUREMENT]
'pressure': ['Pressure', UnitOfPressure.HPA, 'mdi:gauge', SensorDeviceClass.PRESSURE, SensorStateClass.MEASUREMENT],
'temperature': ['Temperature', UnitOfTemperature.CELSIUS, 'mdi:thermometer', SensorDeviceClass.TEMPERATURE, SensorStateClass.MEASUREMENT],
'dewPoint': ['Dew Point', UnitOfTemperature.CELSIUS, 'mdi:thermometer-lines', SensorDeviceClass.TEMPERATURE, SensorStateClass.MEASUREMENT]
}
2 changes: 1 addition & 1 deletion custom_components/esa_nask/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/Mr-Groch/HA-ESA-NASK-Air-Quality/issues",
"requirements": ["beautifulsoup4==4.11.1"],
"version": "1.0.2"
"version": "1.0.3"
}
43 changes: 28 additions & 15 deletions custom_components/esa_nask/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
) -> bool:
"""Set up the ESA NASK sensor entry."""

_LOGGER.debug("config ID:")
Expand All @@ -49,6 +49,9 @@ async def async_setup_entry(
await hass.async_add_executor_job(scrapper.update)

scannedData = scrapper.GetData()
if not scannedData or not "sensors" in scannedData:
return False

scannedData = scannedData["sensors"][0]["lastMeasurement"]

entities = []
Expand All @@ -58,6 +61,8 @@ async def async_setup_entry(
entities.append(EsaNaskSensor(res, scrapper))

async_add_entities(entities, update_before_add=True)

return True



Expand Down Expand Up @@ -115,7 +120,7 @@ def state_class(self) -> SensorStateClass | str | None:
def icon(self) -> str | None:
return SENSORS[self._resType][2]

def update(self) -> None:
def update(self) -> bool:
#Fetch new state data for the sensor.
#This is the only method that should fetch new data for Home Assistant.

Expand All @@ -124,16 +129,24 @@ def update(self) -> None:
self._data = self._scrapper.GetData()
self._updateLastTime = self._scrapper.GetUpdateLastTime()

if self._resType == 'pm25' or self._resType == 'pm10':
self._state = int(self._data["sensors"][0]["lastMeasurement"][self._resType]["value"])
self._AQI = self._data["sensors"][0]["lastMeasurement"][self._resType]["icon"]
else:
self._state = round((self._data["sensors"][0]["lastMeasurement"][self._resType]), 2)

self._attr_extra_state_attributes = {
"Station Name": self._scrapper.GetStationFriendlyName(),
"Station ID": self._scrapper.GetStationId(),
"Last Update": self._scrapper.GetUpdateLastTime()
}
if self._AQI:
self._attr_extra_state_attributes["AQI"] = self._AQI
if self._data and "sensors" in self._data:
try:
if self._resType == 'pm25' or self._resType == 'pm10':
self._state = int(self._data["sensors"][0]["lastMeasurement"][self._resType]["value"])
self._AQI = self._data["sensors"][0]["lastMeasurement"][self._resType]["icon"]
else:
self._state = round((self._data["sensors"][0]["lastMeasurement"][self._resType]), 2)
except TypeError as exc:
return False

self._attr_extra_state_attributes = {
"Station Name": self._scrapper.GetStationFriendlyName(),
"Station ID": self._scrapper.GetStationId(),
"Last Update": self._scrapper.GetUpdateLastTime()
}
if self._AQI:
self._attr_extra_state_attributes["AQI"] = self._AQI

return True

return False

0 comments on commit d235044

Please sign in to comment.