Skip to content

Commit fa188f8

Browse files
authored
Unhandled exception on missing sensor property "lastMeasurement" (#10)
* bugfix crash on missing sensor property "lastMeasurement" * formatting * adds title variations adds additional sensor apis * fix case
1 parent 2fe5754 commit fa188f8

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

example.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from opensensemap_api import OpenSenseMap
77

8-
SENSOR_ID = "5a528c40fa02ec000fe9058a"
8+
SENSOR_ID = "63b83dcc6795ba0007794c93"
99

1010

1111
async def main():
@@ -19,11 +19,21 @@ async def main():
1919
print("Name:", station.name)
2020
print("Description:", station.description)
2121
print("Coordinates:", station.coordinates)
22+
print("Model:", station.model)
23+
print("Exposure:", station.exposure)
2224

25+
print("PM 1.0:", station.pm1_0)
2326
print("PM 2.5:", station.pm2_5)
2427
print("PM 10:", station.pm10)
2528

2629
print("Temperature:", station.temperature)
30+
print("Humidity:", station.humidity)
31+
print("Pressure:", station.air_pressure)
32+
print("Wind Speed:", station.wind_speed)
33+
print("Wind Direction:", station.wind_direction)
34+
print("Precipitation:", station.precipitation)
35+
print("Illuminance:", station.illuminance)
36+
print("UV Index:", station.uv)
2737

2838

2939
if __name__ == "__main__":

opensensemap_api/__init__.py

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,39 @@
1212
_INSTANCE = "https://api.opensensemap.org/boxes/{id}"
1313

1414
_TITLES = {
15-
"Air pressure": (
15+
"Air Pressure": (
16+
"Pressure",
1617
"Luftdruck",
1718
"Ilmanpaine",
18-
), # fi
19+
),
1920
"Humidity": (
2021
"rel. Luftfeuchte",
22+
"rel. Humidity",
2123
"Ilmankosteus",
2224
"Kosteus",
23-
), # fi
25+
),
2426
"Illuminance": (
2527
"Beleuchtungsstärke",
2628
"Valoisuus",
27-
"Valaistuksen voimakkuus", # fi
29+
"Valaistuksen voimakkuus",
30+
"Ambient Light",
2831
),
2932
"Temperature": (
3033
"Temperatur",
3134
"Lämpötila",
32-
), # fi
35+
),
3336
"UV": (
37+
"UV Index",
3438
"UV-Intensität",
3539
"UV-säteily",
36-
), # fi
40+
),
41+
"Precipitation": (
42+
"Rain",
43+
"Regen",
44+
"Niederschlag",
45+
),
46+
"Wind Speed": ("Windgeschwindigkeit",),
47+
"Wind Direction": ("Windrichtung",),
3748
}
3849

3950

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

92+
@property
93+
def exposure(self):
94+
"""Return the exposure of the station."""
95+
try:
96+
return self.data["exposure"]
97+
except KeyError:
98+
return None
99+
100+
@property
101+
def model(self):
102+
"""Return the model of the station."""
103+
try:
104+
return self.data["model"]
105+
except KeyError:
106+
return None
107+
81108
@property
82109
def pm10(self):
83110
"""Return the particulate matter 10 value."""
@@ -88,6 +115,11 @@ def pm2_5(self):
88115
"""Return the particulate matter 2.5 value."""
89116
return self.get_value("PM2.5")
90117

118+
@property
119+
def pm1_0(self):
120+
"""Return the particulate matter 1.0 value."""
121+
return self.get_value("PM1.0")
122+
91123
@property
92124
def temperature(self):
93125
"""Return the temperature of a station."""
@@ -106,7 +138,7 @@ def vcc(self):
106138
@property
107139
def air_pressure(self):
108140
"""Return the current air pressure of a station."""
109-
return self.get_value("Air pressure")
141+
return self.get_value("Air Pressure")
110142

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

158+
@property
159+
def wind_speed(self):
160+
"""Return the current wind speed value of a station."""
161+
return self.get_value("Wind Speed")
162+
163+
@property
164+
def wind_direction(self):
165+
"""Return the current wind direction value of a station."""
166+
return self.get_value("Wind Direction")
167+
168+
@property
169+
def precipitation(self):
170+
"""Return the current precipitation value of a station."""
171+
return self.get_value("Precipitation")
172+
126173
def get_value(self, key):
127174
"""Extract a value for a given key."""
128175
for title in _TITLES.get(key, ()) + (key,):
129176
try:
130177
value = [
131178
entry["lastMeasurement"]["value"]
132179
for entry in self.data["sensors"]
133-
if entry["title"] == title
180+
if entry["title"].casefold() == title.casefold()
181+
and "lastMeasurement" in entry
182+
and "value" in entry["lastMeasurement"]
134183
][0]
135184
return value
136185
except (IndexError, TypeError):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name="opensensemap-api",
15-
version="0.3.1",
15+
version="0.3.2",
1616
description="Python client for interacting with the openSenseMap API.",
1717
long_description=long_description,
1818
url="https://github.com/home-assistant-ecosystem/python-opensensemap-api",

0 commit comments

Comments
 (0)