Skip to content

get_entity_histories() - fixed date formatting#202

Merged
GrandMoff100 merged 10 commits intoGrandMoff100:devfrom
timohencken:fix/history_dates
Feb 28, 2025
Merged

get_entity_histories() - fixed date formatting#202
GrandMoff100 merged 10 commits intoGrandMoff100:devfrom
timohencken:fix/history_dates

Conversation

@timohencken
Copy link

As noted in https://developers.home-assistant.io/docs/api/rest#actions -> "GET /api/history/period/", timestamps should have a second resolution (no microsecond) and should be timezone-aware.

The <timestamp> (YYYY-MM-DDThh:mm:ssTZD) is optional and defaults to 1 day before the time of the request.
end_time=<timestamp> to choose the end of the period in URL encoded format (defaults to 1 day).

Also, timetamp encoding has to be done manually, as get_entity_histories() utilizes construct_params() thus preventing request's default parameter encoding

Background:
I tried using timezone-aware datetime objects and ran into issue

  File "homeassistant_api\models\entity.py", line 85, in get_history
    for history in self.group._client.get_entity_histories(
  File "homeassistant_api\rawclient.py", line 164, in get_entity_histories
    data = self.request(
           ^^^^^^^^^^^^^
  File "homeassistant_api\rawclient.py", line 110, in request
    return self.response_logic(response=resp, decode_bytes=decode_bytes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "homeassistant_api\rawclient.py", line 115, in response_logic
    return Processing(response=response, decode_bytes=decode_bytes).process()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "homeassistant_api\processing.py", line 91, in process
    raise RequestError(content)
homeassistant_api.errors.RequestError: {"message":"Invalid end_time"}

@GrandMoff100
Copy link
Owner

Thanks so much for catching that! I appreciate it. I will review it as soon as I have a moment

@timohencken
Copy link
Author

Missing imports were added, ruff and mypy are happy now.
Sorry for the inconvenience.

@GrandMoff100
Copy link
Owner

'Twasn't an inconvenience at all :)

@GrandMoff100
Copy link
Owner

Woohoo! Checks pass. I just request you run the changes and double check the tests are working and that you're satisfied with the changes. Let me know when you do that. Thank you so much for opening a PR! I hope you have a good weekend.

@timohencken
Copy link
Author

Thank you so much for your effort to make this change possible.
I think this
[k if v is None else f"{k}={quote_plus(v)}" for k, v in params.items()]
is THE correct way to go and much nicer than the dedicated encoding of timestamps I proposed.

I just gave it a try in my setup and it works wonderfully.

Looking forward to seeing this in the next release.

Have a wonderful weekend!

Copy link
Owner

@GrandMoff100 GrandMoff100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woohoo!

@GrandMoff100 GrandMoff100 merged commit ec86ac9 into GrandMoff100:dev Feb 28, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants