async def update(self):
        """Update data."""
        if self._last_update is None or datetime.datetime.now() - self._last_update > datetime.timedelta(3600):
            try:
                with async_timeout.timeout(10):
                    resp = await self._websession.get(self._api_url, params=self._urlparams)
                if resp.status != 200:
                    _LOGGER.error('%s returned %s', self._api_url, resp.status)
                    return False
                self._data = await resp.json()
            except (asyncio.TimeoutError, aiohttp.ClientError) as err:
                _LOGGER.error('%s returned %s', self._api_url, err)
                return False
        try:
            forecast_time = datetime.datetime.now(pytz.utc) + datetime.timedelta(hours=self._forecast)

            data = None
            min_dist = 24 * 3600
            for _data in self._data['data']['time']:
                valid_from = parse_datetime(_data['from'])
                valid_to = parse_datetime(_data['to'])

                if forecast_time >= valid_to:
                    # Has already passed. Never select this.
                    continue

                average_dist = (abs((valid_to - forecast_time).total_seconds()) +
                                abs((valid_from - forecast_time).total_seconds()))
                if average_dist < min_dist:
                    min_dist = average_dist
                    data = _data
            if not data:
                return False
            self.data['aqi'] = data.get('variables', {}).get('AQI', {}).get('value')
            self.data['pm10_concentration'] = data.get('variables',
                                                       {}).get('pm10_concentration', {}).get('value')
            self.data['o3_concentration'] = data.get('variables',
                                                     {}).get('o3_concentration', {}).get('value')
            self.data['no2_concentration'] = data.get('variables',
                                                      {}).get('no2_concentration', {}).get('value')
            self.data['pm25_concentration'] = data.get('variables',
                                                       {}).get('pm25_concentration', {}).get('value')
            self.data['location'] = "{}, {}".format(self._data.get('meta', {}).get('location', {}).get('name'),
                                                    self._data.get('meta', {}).get('superlocation', {}).get('name'))
            state = data.get('variables', {}).get('AQI', {}).get('value')
            if state < 2:
                level = "low"
            elif state < 3:
                level = "medium"
            else:
                level = "high"
            self.data['level'] = level

            self.units['aqi'] = data.get('variables', {}).get('AQI', {}).get('units')
            self.units['pm10_concentration'] = data.get('variables',
                                                        {}).get('pm10_concentration', {}).get('units')
            self.units['o3_concentration'] = data.get('variables',
                                                      {}).get('o3_concentration', {}).get('units')
            self.units['no2_concentration'] = data.get('variables',
                                                       {}).get('no2_concentration', {}).get('units')
            self.units['pm25_concentration'] = data.get('variables',
                                                        {}).get('pm25_concentration', {}).get('units')
            self.units['aqi'] = data.get('variables', {}).get('AQI', {}).get('value')

        except IndexError as err:
            _LOGGER.error('%s returned %s', resp.url, err)
            return False
        return True
async def update ( self ) : if self . _last_update is None or datetime . datetime . now ( ) - self . _last_update > datetime . timedelta ( 3600 ) : try : with async_timeout . timeout ( 10 ) : resp = await self . _websession . get ( self . _api_url , params = self . _urlparams ) if resp . status != 200 : _LOGGER . error ( '%s returned %s' , self . _api_url , resp . status ) return False self . _data = await resp . json ( ) except ( asyncio . TimeoutError , aiohttp . ClientError ) as err : _LOGGER . error ( '%s returned %s' , self . _api_url , err ) return False try : forecast_time = datetime . datetime . now ( pytz . utc ) + datetime . timedelta ( hours = self . _forecast ) data = None min_dist = 24 * 3600 for _data in self . _data [ 'data' ] [ 'time' ] : valid_from = parse_datetime ( _data [ 'from' ] ) valid_to = parse_datetime ( _data [ 'to' ] ) if forecast_time >= valid_to : continue average_dist = ( abs ( ( valid_to - forecast_time ) . total_seconds ( ) ) + abs ( ( valid_from - forecast_time ) . total_seconds ( ) ) ) if average_dist < min_dist : min_dist = average_dist data = _data if not data : return False self . data [ 'aqi' ] = data . get ( 'variables' , { } ) . get ( 'AQI' , { } ) . get ( 'value' ) self . data [ 'pm10_concentration' ] = data . get ( 'variables' , { } ) . get ( 'pm10_concentration' , { } ) . get ( 'value' ) self . data [ 'o3_concentration' ] = data . get ( 'variables' , { } ) . get ( 'o3_concentration' , { } ) . get ( 'value' ) self . data [ 'no2_concentration' ] = data . get ( 'variables' , { } ) . get ( 'no2_concentration' , { } ) . get ( 'value' ) self . data [ 'pm25_concentration' ] = data . get ( 'variables' , { } ) . get ( 'pm25_concentration' , { } ) . get ( 'value' ) self . data [ 'location' ] = "{}, {}" . format ( self . _data . get ( 'meta' , { } ) . get ( 'location' , { } ) . get ( 'name' ) , self . _data . get ( 'meta' , { } ) . get ( 'superlocation' , { } ) . get ( 'name' ) ) state = data . get ( 'variables' , { } ) . get ( 'AQI' , { } ) . get ( 'value' ) if state < 2 : level = "low" elif state < 3 : level = "medium" else : level = "high" self . data [ 'level' ] = level self . units [ 'aqi' ] = data . get ( 'variables' , { } ) . get ( 'AQI' , { } ) . get ( 'units' ) self . units [ 'pm10_concentration' ] = data . get ( 'variables' , { } ) . get ( 'pm10_concentration' , { } ) . get ( 'units' ) self . units [ 'o3_concentration' ] = data . get ( 'variables' , { } ) . get ( 'o3_concentration' , { } ) . get ( 'units' ) self . units [ 'no2_concentration' ] = data . get ( 'variables' , { } ) . get ( 'no2_concentration' , { } ) . get ( 'units' ) self . units [ 'pm25_concentration' ] = data . get ( 'variables' , { } ) . get ( 'pm25_concentration' , { } ) . get ( 'units' ) self . units [ 'aqi' ] = data . get ( 'variables' , { } ) . get ( 'AQI' , { } ) . get ( 'value' ) except IndexError as err : _LOGGER . error ( '%s returned %s' , resp . url , err ) return False return True
Update data.