avryhof / ambient_api

Python module for accessing the Ambient Weather API
MIT License
31 stars 18 forks source link

get_data() method appears to fail #1

Closed sabolcik closed 6 years ago

sabolcik commented 6 years ago

Hi:

I just downloaded the python code to connect to my ambient weatherstation. I am able to connect to the device and the information seems correct, but the get_data() method returns a {}. The last_data variable seems correct.

If I query get_data() multiple times the data is always null and last_data is never updated. If I call get_devices again the last_data variable is updated with a new timestamp and data.

Any thoughts on what might be happening and how to debug?

>>> from ambientapi import AmbientAPI
>>> weather = AmbientAPI()
>>> devices = weather.get_devices()
>>> dev = devices[0]
>>> dev.get_data()
{}
>>> dev.last_data
{u'eventrainin': 0, u'baromrelin': 30.07, u'maxdailygust': 8.1, u'dailyrainin': 0, u'monthlyrainin': 0.26, u'solarradiation': 0, u'dewPoint': 69.36, u'windspeedmph': 1.3, u'humidityin': 47, u'totalrainin': 0.29, u'lastRain': u'2018-08-12T12:17:00.000Z', u'windgustmph': 2.2, u'tempf': 91.9, u'hourlyrainin': 0, u'winddir': 110, u'date': u'2018-08-17T01:41:00.000Z', u'feelsLike': 97.29, u'baromabsin': 29.22, u'dateutc': 1534470060000, u'tempinf': 80.4, u'uv': 0, u'humidity': 48, u'weeklyrainin': 0.09}
>>> dev.get_data()
{}
>>> dev.last_data
{u'eventrainin': 0, u'baromrelin': 30.07, u'maxdailygust': 8.1, u'dailyrainin': 0, u'monthlyrainin': 0.26, u'solarradiation': 0, u'dewPoint': 69.36, u'windspeedmph': 1.3, u'humidityin': 47, u'totalrainin': 0.29, u'lastRain': u'2018-08-12T12:17:00.000Z', u'windgustmph': 2.2, u'tempf': 91.9, u'hourlyrainin': 0, u'winddir': 110, u'date': u'2018-08-17T01:41:00.000Z', u'feelsLike': 97.29, u'baromabsin': 29.22, u'dateutc': 1534470060000, u'tempinf': 80.4, u'uv': 0, u'humidity': 48, u'weeklyrainin': 0.09}
sabolcik commented 6 years ago

The problem with the code is tied to the definition of the current_time() function.

@staticmethod def current_time(): return lambda: int(round(time.time() * 1000))

This is used later as the default value in get_data() if no argument is passed:

def get_data(self, **kwargs):

        limit = int(kwargs.get('limit', 288))
        end_date = kwargs.get('end_date', self.current_time())

            data = dict(
                limit=limit,
                endDate=end_date
            ) 

So the data packet passed to the HTTP call is a function pointer rather than the timecode. I'm not sure why the current_time function was coded using lambda rather than simply returning the millisecond time count. I removed the lambda and the library is now working. Alternatively setting the default for the end_date parameter in get_data() will work.

avryhof commented 6 years ago

Thanks! I'll merge your changes.