Component update function (Throttle)

I have a following code, it is custom component (not switch, light, etc) and i want to be able get the update function to be called every 5 seconds automatically. I have found in the other components that the Throttle does the job so i set it up but i never get called. Can you help please?

import logging
import voluptuous as vol

from homeassistant.config import load_yaml_config_file
import homeassistant.helpers.config_validation as cv
from datetime import timedelta
from homeassistant.util import Throttle
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP)

DOMAIN = "mytest"

_LOGGER = logging.getLogger(__name__)

CONF_INTERFACE = "interface"

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)

TEST_SCHEMA = vol.Schema({
    vol.Required(CONF_INTERFACE): cv.string
})


def setup(hass, config):
    conf = config[DOMAIN]
    interface = conf.get(CONF_INTERFACE)

    _LOGGER.debug("Interface %s", interface)

    hass.data[DOMAIN] = myClass(hass, interface)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, hass.data[
                         DOMAIN].restore_all())
    return True


class myClass(object):

    def __init__(self, hass, interface):
        """Init the api."""
        self._hass = hass
        self._interface = interface
        self._devices = []

    def add_device(self, ip):
        _LOGGER.debug("add %s", ip)

        if ip not in self._devices:
            self._devices.append(ip)
            _LOGGER.debug("array now %s", self._devices)

            return True

        return False

    def remove_device(self, ip):
        _LOGGER.debug("remove %s", ip)

        if ip in self._devices:
            self._devices.remove(ip)
            _LOGGER.debug("array now %s", self._devices)

            return False

        return True

    @Throttle(MIN_TIME_BETWEEN_UPDATES)
    def update(self):
        """Loop"""
        _LOGGER.info("Array")

    def restore_all(self):
        _LOGGER.debug("Restore %s", self._devices)

Throttle is used to limit the number of requests. You want to se thr SCAN_INTERVAL.
I’ve used throttle to limit the number of requests to an API

Thank you very much, that explained it! for anyone finding this here is howto:

import logging
import voluptuous as vol

from homeassistant.config import load_yaml_config_file
import homeassistant.helpers.config_validation as cv
from datetime import timedelta
from homeassistant.helpers.event import track_time_interval
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP)

DOMAIN = "mytest"

_LOGGER = logging.getLogger(__name__)

CONF_INTERFACE = "interface"

SCAN_INTERVAL = timedelta(seconds=5)

TEST_SCHEMA = vol.Schema({
    vol.Required(CONF_INTERFACE): cv.string
})


def setup(hass, config):
    conf = config[DOMAIN]
    interface = conf.get(CONF_INTERFACE)

    _LOGGER.debug("Interface %s", interface)

    hass.data[DOMAIN] = myClass(hass, interface)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, hass.data[
                         DOMAIN].restore_all())

    def refresh(event_time):
        """Refresh"""
        _LOGGER.debug("Updating...")
        hass.data[DOMAIN].update()

    track_time_interval(hass, refresh, SCAN_INTERVAL)

    return True


class myClass(object):

    def __init__(self, hass, interface):
        """Init the api."""
        self._hass = hass
        self._interface = interface
        self._devices = []

    def add_device(self, ip):
        _LOGGER.debug("add %s", ip)

        if ip not in self._devices:
            self._devices.append(ip)
            _LOGGER.debug("array now %s", self._devices)

            return True

        return False

    def remove_device(self, ip):
        _LOGGER.debug("remove %s", ip)

        if ip in self._devices:
            self._devices.remove(ip)
            _LOGGER.debug("array now %s", self._devices)

            return False

        return True

    def update(self):
        """Loop"""
        _LOGGER.info("Array")

    def restore_all(self):
        _LOGGER.debug("Restore %s", self._devices)

3 Likes