Skip to content

Commit

Permalink
Unhandled exception on missing sensor property "lastMeasurement" (#10)
Browse files Browse the repository at this point in the history
* bugfix crash on missing sensor property "lastMeasurement"

* formatting

* adds title variations
adds additional sensor apis

* fix case
  • Loading branch information
hubwoop committed Dec 29, 2023
1 parent 2fe5754 commit fa188f8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
12 changes: 11 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from opensensemap_api import OpenSenseMap

SENSOR_ID = "5a528c40fa02ec000fe9058a"
SENSOR_ID = "63b83dcc6795ba0007794c93"


async def main():
Expand All @@ -19,11 +19,21 @@ async def main():
print("Name:", station.name)
print("Description:", station.description)
print("Coordinates:", station.coordinates)
print("Model:", station.model)
print("Exposure:", station.exposure)

print("PM 1.0:", station.pm1_0)
print("PM 2.5:", station.pm2_5)
print("PM 10:", station.pm10)

print("Temperature:", station.temperature)
print("Humidity:", station.humidity)
print("Pressure:", station.air_pressure)
print("Wind Speed:", station.wind_speed)
print("Wind Direction:", station.wind_direction)
print("Precipitation:", station.precipitation)
print("Illuminance:", station.illuminance)
print("UV Index:", station.uv)


if __name__ == "__main__":
Expand Down
65 changes: 57 additions & 8 deletions opensensemap_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,39 @@
_INSTANCE = "https://api.opensensemap.org/boxes/{id}"

_TITLES = {
"Air pressure": (
"Air Pressure": (
"Pressure",
"Luftdruck",
"Ilmanpaine",
), # fi
),
"Humidity": (
"rel. Luftfeuchte",
"rel. Humidity",
"Ilmankosteus",
"Kosteus",
), # fi
),
"Illuminance": (
"Beleuchtungsstärke",
"Valoisuus",
"Valaistuksen voimakkuus", # fi
"Valaistuksen voimakkuus",
"Ambient Light",
),
"Temperature": (
"Temperatur",
"Lämpötila",
), # fi
),
"UV": (
"UV Index",
"UV-Intensität",
"UV-säteily",
), # fi
),
"Precipitation": (
"Rain",
"Regen",
"Niederschlag",
),
"Wind Speed": ("Windgeschwindigkeit",),
"Wind Direction": ("Windrichtung",),
}


Expand Down Expand Up @@ -78,6 +89,22 @@ def coordinates(self):
"""Return the coordinates of the station."""
return self.data["currentLocation"]["coordinates"]

@property
def exposure(self):
"""Return the exposure of the station."""
try:
return self.data["exposure"]
except KeyError:
return None

@property
def model(self):
"""Return the model of the station."""
try:
return self.data["model"]
except KeyError:
return None

@property
def pm10(self):
"""Return the particulate matter 10 value."""
Expand All @@ -88,6 +115,11 @@ def pm2_5(self):
"""Return the particulate matter 2.5 value."""
return self.get_value("PM2.5")

@property
def pm1_0(self):
"""Return the particulate matter 1.0 value."""
return self.get_value("PM1.0")

@property
def temperature(self):
"""Return the temperature of a station."""
Expand All @@ -106,7 +138,7 @@ def vcc(self):
@property
def air_pressure(self):
"""Return the current air pressure of a station."""
return self.get_value("Air pressure")
return self.get_value("Air Pressure")

@property
def illuminance(self):
Expand All @@ -123,14 +155,31 @@ def radioactivity(self):
"""Return the current radioactivity value of a station."""
return self.get_value("Radioactivity")

@property
def wind_speed(self):
"""Return the current wind speed value of a station."""
return self.get_value("Wind Speed")

@property
def wind_direction(self):
"""Return the current wind direction value of a station."""
return self.get_value("Wind Direction")

@property
def precipitation(self):
"""Return the current precipitation value of a station."""
return self.get_value("Precipitation")

def get_value(self, key):
"""Extract a value for a given key."""
for title in _TITLES.get(key, ()) + (key,):
try:
value = [
entry["lastMeasurement"]["value"]
for entry in self.data["sensors"]
if entry["title"] == title
if entry["title"].casefold() == title.casefold()
and "lastMeasurement" in entry
and "value" in entry["lastMeasurement"]
][0]
return value
except (IndexError, TypeError):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="opensensemap-api",
version="0.3.1",
version="0.3.2",
description="Python client for interacting with the openSenseMap API.",
long_description=long_description,
url="https://github.com/home-assistant-ecosystem/python-opensensemap-api",
Expand Down

0 comments on commit fa188f8

Please sign in to comment.