From 13df925795aaee4ba50e97c49279e8c223ddb49c Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 5 Jun 2017 17:35:26 +0200 Subject: [PATCH] Do not call update() in constructor (#7912) * Do not call update() in constructor * Do not call update() in constructor * Remove unused import --- homeassistant/components/sensor/cpuspeed.py | 7 ++++--- homeassistant/components/sensor/cups.py | 7 ++----- homeassistant/components/sensor/currencylayer.py | 11 ++++------- homeassistant/components/sensor/deutsche_bahn.py | 12 +++++------- homeassistant/components/sensor/fixer.py | 9 +++------ homeassistant/components/sensor/linux_battery.py | 3 +-- homeassistant/components/sensor/moon.py | 1 - homeassistant/components/sensor/scrape.py | 3 +-- .../components/sensor/swiss_public_transport.py | 15 ++++++++------- homeassistant/components/sensor/time_date.py | 1 - homeassistant/components/sensor/twitch.py | 3 +-- homeassistant/components/sensor/worldclock.py | 1 - 12 files changed, 29 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/sensor/cpuspeed.py b/homeassistant/components/sensor/cpuspeed.py index e26d12469d8..9c5ea10e54c 100644 --- a/homeassistant/components/sensor/cpuspeed.py +++ b/homeassistant/components/sensor/cpuspeed.py @@ -8,9 +8,9 @@ import logging import voluptuous as vol +import homeassistant.helpers.config_validation as cv from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import CONF_NAME -import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity REQUIREMENTS = ['py-cpuinfo==3.2.0'] @@ -22,6 +22,7 @@ ATTR_HZ = 'GHz Advertised' ATTR_ARCH = 'arch' DEFAULT_NAME = 'CPU speed' + ICON = 'mdi:pulse' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ @@ -34,7 +35,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the CPU speed sensor.""" name = config.get(CONF_NAME) - add_devices([CpuSpeedSensor(name)]) + add_devices([CpuSpeedSensor(name)], True) class CpuSpeedSensor(Entity): @@ -44,8 +45,8 @@ class CpuSpeedSensor(Entity): """Initialize the sensor.""" self._name = name self._state = None + self.info = None self._unit_of_measurement = 'GHz' - self.update() @property def name(self): diff --git a/homeassistant/components/sensor/cups.py b/homeassistant/components/sensor/cups.py index 1ad26e85261..f4d826c250d 100644 --- a/homeassistant/components/sensor/cups.py +++ b/homeassistant/components/sensor/cups.py @@ -13,7 +13,6 @@ import homeassistant.helpers.config_validation as cv from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.helpers.entity import Entity -from homeassistant.util import Throttle REQUIREMENTS = ['pycups==1.9.73'] @@ -36,7 +35,7 @@ DEFAULT_PORT = 631 ICON = 'mdi:printer' -MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60) +SCAN_INTERVAL = timedelta(minutes=1) PRINTER_STATES = { 3: 'idle', @@ -72,7 +71,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): _LOGGER.error("Printer is not present: %s", printer) continue - add_devices(dev) + add_devices(dev, True) class CupsSensor(Entity): @@ -83,7 +82,6 @@ class CupsSensor(Entity): self.data = data self._name = printer self._printer = None - self.update() @property def name(self): @@ -140,7 +138,6 @@ class CupsData(object): self._port = port self.printers = None - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Get the latest data from CUPS.""" from cups import Connection diff --git a/homeassistant/components/sensor/currencylayer.py b/homeassistant/components/sensor/currencylayer.py index f605dda3099..2d4e43f69be 100644 --- a/homeassistant/components/sensor/currencylayer.py +++ b/homeassistant/components/sensor/currencylayer.py @@ -10,12 +10,11 @@ import logging import requests import voluptuous as vol +import homeassistant.helpers.config_validation as cv from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ( CONF_API_KEY, CONF_NAME, CONF_BASE, CONF_QUOTE, ATTR_ATTRIBUTION) from homeassistant.helpers.entity import Entity -from homeassistant.util import Throttle -import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) _RESOURCE = 'http://apilayer.net/api/live' @@ -27,7 +26,7 @@ DEFAULT_NAME = 'CurrencyLayer Sensor' ICON = 'mdi:currency' -MIN_TIME_BETWEEN_UPDATES = timedelta(hours=2) +SCAN_INTERVAL = timedelta(hours=2) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_API_KEY): cv.string, @@ -56,8 +55,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if 'error' in response.json(): return False else: - add_devices(sensors) - rest.update() + add_devices(sensors, True) class CurrencylayerSensor(Entity): @@ -68,7 +66,7 @@ class CurrencylayerSensor(Entity): self.rest = rest self._quote = quote self._base = base - self.update() + self._state = None @property def name(self): @@ -110,7 +108,6 @@ class CurrencylayerData(object): self._parameters = parameters self.data = None - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Get the latest data from Currencylayer.""" try: diff --git a/homeassistant/components/sensor/deutsche_bahn.py b/homeassistant/components/sensor/deutsche_bahn.py index 0f6e3b267ca..04c9ba45c78 100644 --- a/homeassistant/components/sensor/deutsche_bahn.py +++ b/homeassistant/components/sensor/deutsche_bahn.py @@ -9,11 +9,10 @@ from datetime import timedelta import voluptuous as vol -from homeassistant.components.sensor import PLATFORM_SCHEMA import homeassistant.helpers.config_validation as cv -from homeassistant.util import Throttle -from homeassistant.helpers.entity import Entity import homeassistant.util.dt as dt_util +from homeassistant.components.sensor import PLATFORM_SCHEMA +from homeassistant.helpers.entity import Entity REQUIREMENTS = ['schiene==0.18'] @@ -24,7 +23,7 @@ CONF_START = 'from' ICON = 'mdi:train' -MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120) +SCAN_INTERVAL = timedelta(minutes=2) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_DESTINATION): cv.string, @@ -37,7 +36,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): start = config.get(CONF_START) destination = config.get(CONF_DESTINATION) - add_devices([DeutscheBahnSensor(start, destination)]) + add_devices([DeutscheBahnSensor(start, destination)], True) class DeutscheBahnSensor(Entity): @@ -47,7 +46,7 @@ class DeutscheBahnSensor(Entity): """Initialize the sensor.""" self._name = '{} to {}'.format(start, goal) self.data = SchieneData(start, goal) - self.update() + self._state = None @property def name(self): @@ -92,7 +91,6 @@ class SchieneData(object): self.schiene = schiene.Schiene() self.connections = [{}] - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Update the connection data.""" self.connections = self.schiene.connections( diff --git a/homeassistant/components/sensor/fixer.py b/homeassistant/components/sensor/fixer.py index 87fafe2c27c..6e994395b3b 100644 --- a/homeassistant/components/sensor/fixer.py +++ b/homeassistant/components/sensor/fixer.py @@ -9,11 +9,10 @@ from datetime import timedelta import voluptuous as vol +import homeassistant.helpers.config_validation as cv from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import (CONF_NAME, ATTR_ATTRIBUTION, CONF_BASE) from homeassistant.helpers.entity import Entity -from homeassistant.util import Throttle -import homeassistant.helpers.config_validation as cv REQUIREMENTS = ['fixerio==0.1.1'] @@ -31,7 +30,7 @@ DEFAULT_NAME = 'Exchange rate' ICON = 'mdi:currency' -MIN_TIME_BETWEEN_UPDATES = timedelta(days=1) +SCAN_INTERVAL = timedelta(days=1) PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_TARGET): cv.string, @@ -55,7 +54,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return False data = ExchangeData(base, target) - add_devices([ExchangeRateSensor(data, name, target)]) + add_devices([ExchangeRateSensor(data, name, target)], True) class ExchangeRateSensor(Entity): @@ -67,7 +66,6 @@ class ExchangeRateSensor(Entity): self._target = target self._name = name self._state = None - self.update() @property def name(self): @@ -120,7 +118,6 @@ class ExchangeData(object): base=self.base_currency, symbols=[self.target_currency], secure=True) - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Get the latest data from Fixer.io.""" self.rate = self.exchange.latest() diff --git a/homeassistant/components/sensor/linux_battery.py b/homeassistant/components/sensor/linux_battery.py index 18e2250ee13..645917fec47 100644 --- a/homeassistant/components/sensor/linux_battery.py +++ b/homeassistant/components/sensor/linux_battery.py @@ -60,7 +60,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): _LOGGER.error("No battery found") return False - add_devices([LinuxBatterySensor(name, battery_id)]) + add_devices([LinuxBatterySensor(name, battery_id)], True) class LinuxBatterySensor(Entity): @@ -75,7 +75,6 @@ class LinuxBatterySensor(Entity): self._battery_stat = None self._battery_id = battery_id - 1 self._unit_of_measurement = '%' - self.update() @property def name(self): diff --git a/homeassistant/components/sensor/moon.py b/homeassistant/components/sensor/moon.py index ca79e5241c4..9d844292e18 100644 --- a/homeassistant/components/sensor/moon.py +++ b/homeassistant/components/sensor/moon.py @@ -32,7 +32,6 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): name = config.get(CONF_NAME) async_add_devices([MoonSensor(name)], True) - return True class MoonSensor(Entity): diff --git a/homeassistant/components/sensor/scrape.py b/homeassistant/components/sensor/scrape.py index fe50b567319..60f601fa5ab 100644 --- a/homeassistant/components/sensor/scrape.py +++ b/homeassistant/components/sensor/scrape.py @@ -57,7 +57,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): add_devices([ ScrapeSensor(hass, rest, name, select, value_template, unit) - ]) + ], True) class ScrapeSensor(Entity): @@ -71,7 +71,6 @@ class ScrapeSensor(Entity): self._select = select self._value_template = value_template self._unit_of_measurement = unit - self.update() @property def name(self): diff --git a/homeassistant/components/sensor/swiss_public_transport.py b/homeassistant/components/sensor/swiss_public_transport.py index 3ca8c4cb8e1..aa0be36b075 100644 --- a/homeassistant/components/sensor/swiss_public_transport.py +++ b/homeassistant/components/sensor/swiss_public_transport.py @@ -10,12 +10,11 @@ from datetime import timedelta import requests import voluptuous as vol +import homeassistant.helpers.config_validation as cv +import homeassistant.util.dt as dt_util from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import CONF_NAME, ATTR_ATTRIBUTION -import homeassistant.util.dt as dt_util from homeassistant.helpers.entity import Entity -from homeassistant.util import Throttle -import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) _RESOURCE = 'http://transport.opendata.ch/v1/' @@ -31,9 +30,11 @@ CONF_DESTINATION = 'to' CONF_START = 'from' DEFAULT_NAME = 'Next Departure' + ICON = 'mdi:bus' -MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60) +SCAN_INTERVAL = timedelta(minutes=1) + TIME_STR_FORMAT = "%H:%M" PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ @@ -62,7 +63,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return False data = PublicTransportData(journey) - add_devices([SwissPublicTransportSensor(data, journey, name)]) + add_devices([SwissPublicTransportSensor(data, journey, name)], True) class SwissPublicTransportSensor(Entity): @@ -72,9 +73,10 @@ class SwissPublicTransportSensor(Entity): """Initialize the sensor.""" self.data = data self._name = name + self._state = None + self._times = None self._from = journey[2] self._to = journey[3] - self.update() @property def name(self): @@ -124,7 +126,6 @@ class PublicTransportData(object): self.destination = journey[1] self.times = {} - @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Get the latest data from opendata.ch.""" response = requests.get( diff --git a/homeassistant/components/sensor/time_date.py b/homeassistant/components/sensor/time_date.py index 484be92ec47..c1c4d62dbcb 100644 --- a/homeassistant/components/sensor/time_date.py +++ b/homeassistant/components/sensor/time_date.py @@ -52,7 +52,6 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): devices.append(device) async_add_devices(devices, True) - return True class TimeDateSensor(Entity): diff --git a/homeassistant/components/sensor/twitch.py b/homeassistant/components/sensor/twitch.py index 6d075049b74..b3e227aea72 100644 --- a/homeassistant/components/sensor/twitch.py +++ b/homeassistant/components/sensor/twitch.py @@ -36,7 +36,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the Twitch platform.""" channels = config.get(CONF_CHANNELS, []) - add_devices([TwitchSensor(channel) for channel in channels]) + add_devices([TwitchSensor(channel) for channel in channels], True) class TwitchSensor(Entity): @@ -49,7 +49,6 @@ class TwitchSensor(Entity): self._preview = None self._game = None self._title = None - self.update() @property def should_poll(self): diff --git a/homeassistant/components/sensor/worldclock.py b/homeassistant/components/sensor/worldclock.py index 7f1e6429ba5..839b5776b3c 100644 --- a/homeassistant/components/sensor/worldclock.py +++ b/homeassistant/components/sensor/worldclock.py @@ -36,7 +36,6 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): time_zone = dt_util.get_time_zone(config.get(CONF_TIME_ZONE)) async_add_devices([WorldClockSensor(time_zone, name)], True) - return True class WorldClockSensor(Entity):