ha-whodid
Get userid from context objects, even if the context itself does not have a userid provided.
How it works
After creating the WhoDid instance, all events will be monitored.
For every event, the context id is stored together with its user id and parent id (in a bounded cache, oldest entries are dropped first).
Automations and scripts create a new context whose parent id points to the context that triggered them. getUserId follows this parent chain back to its root, so a user is found even through several automations/scripts in a row.
If the root has neither a user nor a parent, the change came from a device or integration and None is returned right away.
getOrigin(context) returns the full picture: kind ("user", "system" for device/integration, or "unknown" if the chain could not be followed), user_id, and chain, the automations/scripts that were passed through.
All integrations using this package will share a single instance.
This is done to save memory.
Example code
from hawhodid import WhoDid
async def handle_servicecall(call: ServiceCall):
whodidInstance = WhoDid(hass=call.hass)
userid = await whodidInstance.getUserId(context=call.context)
if userid == None:
return
origin = await whodidInstance.getOrigin(context=call.context)
# Origin(kind="user", user_id="...", chain=["automation.b", "script.s"])
This best to put WhoDid(hass=hass) into the setup or async_setup function of your integration to ensure that all events from the beginning are accounted for.
Work in progress
This is a bare bones implementation, that i made for my own integrations.
I only made this to prevent code duplication and reduce memory usage.
If you need something for you or any bugs, just create an issue or provide a solution with a PR.
Both are appreciated.
Releasing
- Bump
versioninpyproject.tomland merge tomain. - Either run the Tag main workflow (Actions tab, run on
main), which tagsmainasv<version>and publishes it, or create a branch/tag namedv<version>(e.g.v0.0.2) yourself.
The Publish to PyPI workflow runs only for those two cases, never on pushes or merges to main. It fails if the ref name does not match the pyproject.toml version. It uses the PYPI_PASSWORD secret (a PyPI API token).
Metadata
Release files for hawhodid 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hawhodid-0.0.2.tar.gz | 4.8 kB | Without provenance | Details for hawhodid-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hawhodid-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for hawhodid-0.0.2-py3-none-any.whl |
Total release size: 9.8 kB
Release files / hawhodid-0.0.2.tar.gz
| Download URL | hawhodid-0.0.2.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9891954dede0387413a2616f5c9af831b25fcc1736e7205956e88e839189b6f8
|
|
BLAKE2b-256 checksum How to use checksums |
ac61a6a144632b113f47937897c8fdac8f17bb797ac8827739ca78bd8e24b60c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / hawhodid-0.0.2-py3-none-any.whl
| Download URL | hawhodid-0.0.2-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1ea2ae8733e91f2d5fdcd47304b9820481e82ebf62718e5377b68047883ea322
|
|
BLAKE2b-256 checksum How to use checksums |
e710ef98f1369a3844a965074ebfcf95e95745b033b9a3bd35d1d73a38c907c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for hawhodid 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.