Hi everybody,
I’m starting developing my first custom sensor for hass but I’m having trouble in reading data and populating the state value.
I cannot find any record inside the log, so I think the update function of the component is never called, could you please help me with that? I would like to share it with the community when it is ready
from datetime import timedelta import logging import requests import random import voluptuous as vol from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ( CONF_NAME, CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME, CONF_SCAN_INTERVAL) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.util import Throttle _LOGGER = logging.getLogger(__name__) DEFAULT_NAME = 'Asus DownloadMaster' DEFAULT_HOST = '192.168.1.1' DEFAULT_PORT = 8081 ICON = 'mdi:clipboard-arrow-down' BASE_URL = 'http://{0}:{1}{2}' SCAN_INTERVAL = timedelta(minutes=5) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_PASSWORD): cv.string, vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string, vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period }) def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the sensor platform.""" add_devices([AsusDonwloadMaster(config.get(CONF_NAME), config.get(CONF_USERNAME), config.get(CONF_PASSWORD), config.get(CONF_HOST), config.get(CONF_PORT))]) class AsusDonwloadMaster(Entity): """Representation of a Sensor.""" def __init__(self, name, user, password, host, port): """Initialize the sensor.""" self._state = None self._name = name self._user = user self._password = password self._host = host self._port = port self._unit_of_measurement = 'Kpbs' @property def name(self): """Return the name of the sensor.""" return self._name @property def state(self): """Return the state of the sensor.""" return self._state @property def icon(self): """Icon to use in the frontend, if any.""" return ICON @property def unit_of_measurement(self): """Return the unit of measurement.""" return self._unit_of_measurement @Throttle(SCAN_INTERVAL) def update(self): """Fetch new state data for the sensor. This is the only method that should fetch new data for Home Assistant. """ with requests.Session() as c: payload = {'login_username': self._user, 'login_passwd': self._password, 'directurl': None,'foilautofill': self._user, 'flag': None} try: t = str(random.uniform(0, 1)) # GET access cookie _LOGGER.info("Connecting to %s", BASE_URL.format(self._host,self._port,'/check.asp')) c.get(BASE_URL.format(self._host,self._port,'/check.asp'), params=payload) # Request download status status = c.get(BASE_URL.format(self._host,self._port,'/downloadmaster/dm_print_status.cgi?action_mode=All&t=' + t)) # SET state if request is successfull if(status.status_code == 200): out = status.text[2:-2].split('","') self._state = out[7][:-5] + "," + out[8][:-5] # Logout c.get(BASE_URL.format(self._host,self._port,'/downloadmaster/Logout.asp?t=' + t)) except Exception as e: _LOGGER.error('Request error %s', e)