-
Notifications
You must be signed in to change notification settings - Fork 55
Logbook Functionality #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Also, this changes the way #61 works. So that PR is not needed if this one is merged. However, I'm leaving that open for now in case this PR needs more work since that change is useful. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Here are a few minor comments...
I'd like it if the text used to indicate what is doing the work in the logbook was clearer. The logbook is weird, though, so it comes from two places, as illustrated here: As a result, it makes sense to keep the entity_id and the name the same (but the entity_id would have "pyscript." at the beginning of it, which the logbook does not display). I think maybe what should be displayed is something like "pyscript function test_script in file/app mytest". This should give the user a pretty clear indication of where to look for what caused this if they are debugging. Additionally, the function in logbook.py is called WHEN the logbook is displayed in the browser. Because of this, changes made to logbook.py will appear to be retroactive. However, logbook.py also has to be written in a way that no piece of data from the event is REQUIRED, this way if the event we fire ever changes, or if someone should fire a "pyscript_running" event manually, it doesn't result in an exception (which doesn't break HASS, but it does make that logbook entry not show up at all). |
There might be a better way to do this instead of using
Function
like I have. However, it works.A few things to note.
The event fired before the function runs can be named anything we want. I chose "pyscript_running" and then realized Home Assistant uses "automation_triggered". This can be renamed by changing it in both
trigger.py
andlogbook.py
.While I have confirmed that
state.set()
is sending the correct context to the best of my knowledge, it seems that states set in this manner don't show up in the log book at all. The state still "sets" as expected, but there is no logbook entry. Therefore, all of the "context" code can be removed fromstate.set()
if desired.I've left the original context as part of the trigger function kwargs. This way, pyscript users can still look at
user_id
to determine who did something. Unfortunately, determining if pyscript did something would mean climbing the context tree, which is tedious.I've also left context as an optional kwarg to
service.call
andevent.fire
. If a users passes a context, it'll use that. If they don't and we have one from thepyscript_running
event we fired, it'll use that. If they want to disable the logbook functionality for some reason, they can passcontext=Context()
.The logbook attributes actions to entity_ids. And entity ids can only have one "." between the domain and name. So I'm sending, essentially,
__name__
, with "." converted to "_". This means the entity_ids are a bit ugly (ex.pyscript.file_myfile_myfunction
). This is set intrigger.py
and can be changed to anything we want as long as it fits the rules for entity ids.I've tested events, service_calls, time triggers, and state.set with this code:
It results in this logbook: