Introduce recorder.get_statistics service
Proposed change
Currently, it's only possible for (custom_)components and the frontend to access long-term statistics. I would like to use them as part of automations, so now there's a service that allows accessing them.
I feel like I might've missed something, because this was rather straightforward and seems like a very common thing one would want to do. Thus, sorry in advance if this is a stupid/undercooked idea 😅
Type of change
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [x] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
Additional information
- This PR fixes or closes issue: N/A
- This PR is related to issue: N/A
- Link to documentation pull request: N/A
- Link to developer documentation pull request: N/A
- Link to frontend pull request: N/A
Checklist
- [x] The code change is tested and works locally.
- [ ] Local tests pass. Your PR cannot be merged unless tests pass
- [x] There is no commented out code in this PR.
- [x] I have followed the development checklist
- [x] I have followed the perfect PR recommendations
- [ ] The code has been formatted using Ruff (
ruff format homeassistant tests) - [ ] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.io
If the code communicates with devices, web services, or third-party tools:
- [ ] The manifest file has all fields filled out correctly.
Updated and included derived files by running:python3 -m script.hassfest. - [ ] New or updated dependencies have been added to
requirements_all.txt.
Updated by runningpython3 -m script.gen_requirements_all. - [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
To help with the load of incoming pull requests:
- [ ] I have reviewed two other open pull requests in this repository.
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (recorder) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of recorder can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign recorderRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:
@abmantis Are you aware of other similar services where there are already tests I could take a look at to learn how to add tests?
Okay, after looking at the recorder tests for a while, I've realized that I can't add the required tests.
Using git blame, I can see that it's basically just the core team in there and that makes a lot of sense, because you do require deep knowledge for this kind of stuff. It also helps when you're an actual python dev that has used pytest before. Or when you're actually aspiring to be that.
I don't. I just really like home assistant and believe that I'm fairly decent at stuff like UX, Product Management etc.
Hence, please consider this PR a "Batteries Included" proposal on how to improve the product that is HA. It's not one of those hit-and-run solve-my-own-issue-and-nothing-else PRs. But it's also not i'm-trying-to-become-a-ha-core-dev
If you like this, feel free to pick it up. Be it as this code or as the general Idea.
If not, just close the PR. No hard feelings I really think that there should be a way for automations to access statistics, but it's your product so your beer.
I did also consider throwing this into a custom component like my other ideas, but tbh this really should not be an augmentation to the core. Though, maybe I will do that, given that the time investment of that is 1/100 while the benefit for me personally is the same.
Thanks @bdraco !