I tried to understand if this is possible.
I have a components I wrote that pools via ip a device.
I would like to change the time out for the device answer with a variable in the config.
The issue is that the scan_interval should be higher than the all the time outs.
In other words if I have 5 ip calls and the time out is 1.5 sec the scan_interval must not be below 7.5 sec (let’s say round up to 8s) or it will create a mess
So I would like to set the scan_interval from within my components in the init phase after I read the time out
but I was not able to find a way to do it
can you help me?
You can set a SCAN_INTERVAL constant in your sensor and binary sensor file but not sure you can set this to a config value.
Alternatively, you would need to create your own update routine. This can be done with either a data update coordinator or by using the async_added_to_hass function.
The advantage of the data update coordinator is that it will only query your device once and tell all entities to update using a stored variable of your fetched data. At the moment, each entity is seperately updating from your device causing more requests than needed. However, this would need a bit more coding than the async_added_to_hass method.
The async_added_to_hass function would just create an interval timer to call your update function.
Need to make sure you also add the async_will_remove_from_hass() function to cancel the update timer when the integration is removed.
Example of interval timer to add to your common.py
@property
def should_poll():
return false
async def _async_update():
# async function for interval timer to call your sync function
await self._hass.async_add_executor_job(self.update)
async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
# Manually set an update interval so we can use interval from config
self.async_on_remove(
async_track_time_interval(self._hass, self._async_update,
timedelta(seconds=self._config[CONF_TIMEOUT])))