1414_LOGGER = logging .getLogger (__name__ )
1515_RESOURCE = 'https://api.luftdaten.info/v1'
1616
17- VOLUME_MICROGRAMS_PER_CUBIC_METER = 'µg/m3'
18-
19- SENSOR_TEMPERATURE = 'temperature'
20- SENSOR_HUMIDITY = 'humidity'
21- SENSOR_PM10 = 'P1'
22- SENSOR_PM2_5 = 'P2'
23-
24- SENSOR_TYPES = {
25- SENSOR_TEMPERATURE : ['Temperature' , '°C' ],
26- SENSOR_HUMIDITY : ['Humidity' , '%' ],
27- SENSOR_PM10 : ['PM10' , VOLUME_MICROGRAMS_PER_CUBIC_METER ],
28- SENSOR_PM2_5 : ['PM2.5' , VOLUME_MICROGRAMS_PER_CUBIC_METER ]
29- }
30-
3117
3218class Luftdaten (object ):
33- """A class for handling connections from Luftdaten.info ."""
19+ """A class for handling the data retrieval ."""
3420
3521 def __init__ (self , sensor_id , loop , session ):
3622 """Initialize the connection."""
@@ -47,23 +33,22 @@ def __init__(self, sensor_id, loop, session):
4733 }
4834 self .meta = {}
4935
50- @ asyncio . coroutine
51- def async_get_data ( self ):
36+ async def async_get_data ( self ):
37+ """Retrieve the data."""
5238 url = '{}/{}/{}/' .format (_RESOURCE , 'sensor' , self .sensor_id )
5339
5440 try :
5541 with async_timeout .timeout (5 , loop = self ._loop ):
56- response = yield from self ._session .get (url )
42+ response = await self ._session .get (url )
5743
5844 _LOGGER .debug (
5945 "Response from luftdaten.info: %s" , response .status )
60- data = yield from response .json ()
46+ data = await response .json ()
6147 _LOGGER .debug (data )
6248 except (asyncio .TimeoutError , aiohttp .ClientError ):
6349 _LOGGER .error ("Can not load data from luftdaten.info" )
6450 raise exceptions .LuftdatenConnectionError ()
6551
66- print (data )
6752 try :
6853 sensor_data = sorted (
6954 data , key = lambda timestamp : timestamp ['timestamp' ],
@@ -75,7 +60,8 @@ def async_get_data(self):
7560 self .values [measurement ] = float (entry ['value' ])
7661
7762 self .meta ['sensor_id' ] = self .sensor_id
78- self .meta ['longitude' ] = float (sensor_data ['location' ]['longitude' ])
63+ self .meta ['longitude' ] = float (
64+ sensor_data ['location' ]['longitude' ])
7965 self .meta ['latitude' ] = float (sensor_data ['location' ]['latitude' ])
8066 except (TypeError , IndexError ):
8167 raise exceptions .LuftdatenError ()
0 commit comments