/ha-introspection

Simple custom component to do python introspection in HA

Primary LanguagePythonApache License 2.0Apache-2.0

Home Assistant Introspection

GitHub Release GitHub Activity License

pre-commit Black

hacs Project Maintenance

Community Forum

Description

Adds a service for Home Assistant to do dynamic introspection with python expressions.

WARNING: This is a very powerful low-level tool for inspecting (and possibly changing) the internal environment in the running Home Assistant system. There are no safeguards, and this should be used only if you fully understand the consequences of running "eval()" with whatever arbitrary python code you submit, inside of the running Home Assistant process.

It's entirely likely HA core will never support such low-level direct access as what's provided in this simple integration. You can use HACS to install this integration manually as explained below.

Services

Do Introspection

Evaluate a piece of python code in the context of the running Home Assistant process.

expression is a string with they python code you wish to eval statement is an optional string with python code you want exec'd first, before the eval (good for declaring functions).

service: ha_introspection.do_introspection
data:
  statement: "def head(s): return s[20:]"
  expression: head(dir())

The results of the eval'd expression are stashed in state(s), for lack of a better way for a service to return results. (Please le me know if there is a more straightforward approach.) Since every state value is limited to 255 bytes, a long result could overflow, so multiples states may be used. You'll find the count of states populated instrospection.len, indicating the maximum "n" for the values stored in instrospection.state_(n). If the result is less than 256 bytes, there's just introspection.state_1.

Because every call to do_introspection might change the values of one or many states, you are advised to exclude them from recorder, with this in your configuration.yaml:

recorder:
  exclude:
    entity_globs:
      - introspection.*

The following template can be used to concatenate all of the states to get the result of the do_introspection eval:

{% for i in range(1+states.introspection.len.state|int) %}{{  states.introspection['result_' ~ i]['state'] }}{% endfor %}

An example shell script which invokes the do_introspection service and then fetches the results via the template is available in the project, as "introspect.sh".

{% if not installed %}

Installation

For now, you'll need to add a "custom repository," https://github.com/homeautomaton/ha-introspection in the HACS menu at the top, then the integration will show up for "download."

{% endif %}

Contributions are welcome!

TBD

Credits

Inspiration and some formatting/logic taken from [@amosyuen](user_profile: https://github.com/amosyuen)'s ha-registry