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)