bachya / aioambient

🌤 A clean, async-friendly library for interacting with the Ambient Weather API
MIT License
32 stars 10 forks source link

end_date examples in README fail because "2019-01-16" is a string not a datetime #78

Closed mikepqr closed 3 years ago

mikepqr commented 3 years ago

The README example has things like this

        await client.api.get_device_details(
            "<DEVICE MAC ADDRESS>", end_date="2019-01-16"
        )

but end_date is typed here as a datetime (with an isoformat method). I assume this is a minor README bug, i.e. these should be end_date=datetime.datetime(2019, 01, 16).

I would submit a PR for this, but I'm actually unclear on the intended behavior of the end_date parameter. The comment above the end_date example is Get all stored readings from a device (starting at a datetime). I'm struggling to reconcile "starting at" with "end_date", and either of these with the actual behaviour I see.

I'm verifying that behavior with this script:

import asyncio
import os
from datetime import datetime, timezone

from aioambient import Client

async def main() -> None:
    client = Client(<api>, <application>)
    data = await client.api.get_device_details(<mac>)
    print("Now (local): ", datetime.now())
    print("Now (UTC):   ", datetime.now(tz=timezone.utc))
    print("Oldest data: ", data[-1]["date"])
    print("Newest data: ", data[0]["date"])

asyncio.run(main())
  1. Without end_date (i.e. as above), I get this:

    Now (local):  2020-12-27 22:21:41.241295
    Now (UTC):    2020-12-28 06:21:41.241362+00:00
    Oldest data:  2020-12-27T06:20:00.000Z
    Newest data:  2020-12-27T23:55:00.000Z

    i.e. I appear to get a set of data that starts approximately 24 hours ago, and ends at the most recent UTC midnight. I would expect to get the last 24 hours of data.

  2. With end_date set to the current UTC time, i.e. end_date=datetime.now(tz=timezone.utc). I get the same thing, i.e. a slice of data that is less than 24 hours, that runs from 24 hours ago until the most recent UTC midnight.

  3. With end_date set to the current UTC date, i.e. end_date=datetime(2020, 12, 28) at the time of writing, I get 24 hours of data ending at 2020-12-27T23:55:00.000Z.

  4. With end_date set to a date in the past, e.g. end_date=datetime(2020, 12, 01), I get 24 hours of data ending at 2020-12-01T00:00:00.000Z.

Either there is a bug in my understanding of what end_date is supposed to do, or there are some actual bug(s) here. Most importantly, it seems like there is no way to get any data after the most recent UTC midnight (other than the most recent timestamp you get from get_devices()). Am I missing something?

mikepqr commented 3 years ago

Looking more carefully at ambientweather.net, the fact that I can't get any data after midnight UTC out of aioambient seems likely to be a bug in the upstream API. This is a plot of the "last 24 hours". It ends abruptly at 4pm PST, i.e. midnight UTC. I took this screenshot at 22:41 PST on Dec 27, i.e. 06:41 UTC on Dec 28.

Screen Shot 2020-12-27 at 22 41 42

mikepqr commented 3 years ago

OK presumably 99% of this issue is due to the fact that I happen to be trying aioambient during an upstream API outage:

Screen Shot 2020-12-28 at 20 35 02

So please ignore all of this except the bit about the README being wrong about the type of end_date :-)

bachya commented 3 years ago

Thanks, @mikepqr! You're right: those methods are supposed to take date objects. I'll fix.