How to make ANY Entity-based sensor updatable only upon request (standard command_line integration as an example)
If you want to stop HA from updating state of your Entity-based sensor (anything that accepts scan_interval configuration variable) so you can update it when you need by calling homeassistant.update_entity, it's possible using monkey patching and custom integrations.
Note: you should understand that you're playing with HA's internals and your code would break if something changes in HA (class renamed, file structure changed etc). Keep it in mind when designing your setup around this functionality.
You will need to:
- install the custom integration (
manual_command_linein this example) by copying this folder into your<HA configfolder>/custom_components/ - add the custom integration's domain
manual_command_line:to yourconfiguration.yaml(as shown here) - declare your sensor as usual
That's it.
To use it with another standard (let's say sensor) integration (template sensor) you need to:
- rename the custom's integration folder to something unique in HA integrations (
my_silent_sensor) - in manifest.json set
domainto that value (my_silent_sensor) - in manifest.json change
after_dependenciesto your standard integration's domain (sensor) - in
__init__.pychangeDOMAINas per step 2 - in
__init__.pychangeimport homeassistant.components.command_line.sensortoimport homeassistant.components.template.sensor - in
configuration.yamlchangemanual_command_line:as per step 2 (my_silent_sensor:) - declare your standard sensor as usual
sensor:
- platform: template- save changes and restart Home Assistant
If you need to do it with more than one standard integration:
- repeat step 3 adding comma-separated remaining integrations' domains ("[sensor, binary_sensor]")
- add remaining
imports similar to the original one - make sure all
as xxxhave uniquexxx - use these
xxxinstead ofsensorcreate as manysensor.CommandSensor.should_poll = should_pollas necessary. Note that you'll need to change class name (CommandSensor) to a class name that your standard integration is based on.