Change scan interval from within component

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?

M

How to do this depends on how you have written your integration. Post a link to your code and i will have a look for you.

thank you a lot, here everything

msavazzi/homeassistant-jvc-dila (github.com)

I would like to change the

time.sleep(0.6)

in the common.py

with a parameter to be put in config.yaml (i.e. tcp_timeout = 0.5)
and then calculate the correct scan_interval to be higher then the time :slight_smile:

so I can have in the config.yaml

binary_sensor:
  - platform: jvc_dila
    host: 192.168.1.1
    port: 20554
    tcp_timeout: 0.3
    retries:5

Apologies for the delay in replying.

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.

I’ll post you and example of this later today.

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])))

You can probably also use Throttle as a decorator on the update function to control the refresh rate.

Would still recommend a DataUpdateCoordinator but likely throttling is easier to implement at first.