Skip to content

Commit 12f7550

Browse files
committed
Update
1 parent 40afe3b commit 12f7550

File tree

2 files changed

+9
-23
lines changed

2 files changed

+9
-23
lines changed

example.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ async def main():
1616
"""Sample code to retrieve the data."""
1717
async with aiohttp.ClientSession() as session:
1818
data = Luftdaten(SENSOR_ID, loop, session)
19+
await data.get_data()
1920

20-
valid = await data.validate_sensor(SENSOR_ID)
21-
22-
if not valid:
21+
if not await data.validate_sensor():
2322
print("Station is not available:", data.sensor_id)
2423
return
2524

26-
await data.get_data()
27-
2825
if data.values and data.meta:
2926
# Print the sensor values
3027
print("Sensor values:", data.values)

luftdaten/__init__.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, sensor_id, loop, session):
2323
self._loop = loop
2424
self._session = session
2525
self.sensor_id = sensor_id
26+
self.data = None
2627
self.values = {
2728
'humidity': None,
2829
'P1': None,
@@ -42,19 +43,19 @@ async def get_data(self):
4243

4344
_LOGGER.debug(
4445
"Response from luftdaten.info: %s", response.status)
45-
data = await response.json()
46-
_LOGGER.debug(data)
46+
self.data = await response.json()
47+
_LOGGER.debug(self.data)
4748
except (asyncio.TimeoutError, aiohttp.ClientError):
4849
_LOGGER.error("Can not load data from luftdaten.info")
4950
raise exceptions.LuftdatenConnectionError()
5051

51-
if not data:
52+
if not self.data:
5253
self.values = self.meta = None
5354
return
5455

5556
try:
5657
sensor_data = sorted(
57-
data, key=lambda timestamp: timestamp['timestamp'],
58+
self.data, key=lambda timestamp: timestamp['timestamp'],
5859
reverse=True)[0]
5960

6061
for entry in sensor_data['sensordatavalues']:
@@ -69,18 +70,6 @@ async def get_data(self):
6970
except (TypeError, IndexError):
7071
raise exceptions.LuftdatenError()
7172

72-
async def validate_sensor(self, sensor_id):
73+
async def validate_sensor(self):
7374
"""Return True if the sensor ID is valid."""
74-
try:
75-
with async_timeout.timeout(5, loop=self._loop):
76-
response = await self._session.get(
77-
'{}/{}/'.format(self.url, sensor_id))
78-
79-
_LOGGER.debug(
80-
"Response from luftdaten.info: %s", response.status)
81-
data = await response.json()
82-
except (asyncio.TimeoutError, aiohttp.ClientError):
83-
_LOGGER.error("Can not load data from luftdaten.info")
84-
raise exceptions.LuftdatenConnectionError()
85-
86-
return True if data else False
75+
return True if self.data else False

0 commit comments

Comments
 (0)