Do not call update() in constructor (#8859)

pull/8857/head
Fabian Affolter 2017-08-06 19:21:55 +02:00 committed by Paulus Schoutsen
parent ac9c1235bb
commit d8ca04a4bc
19 changed files with 43 additions and 57 deletions

View File

@ -10,12 +10,12 @@ from datetime import timedelta
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_NAME, CONF_MONITORED_VARIABLES, ATTR_ATTRIBUTION)
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['pybbox==0.0.5-alpha']
@ -23,7 +23,7 @@ _LOGGER = logging.getLogger(__name__)
BANDWIDTH_MEGABITS_SECONDS = 'Mb/s' # type: str
CONF_ATTRIBUTION = "Powered by Bouygues Telecom"
ATTRIBUTION = "Powered by Bouygues Telecom"
DEFAULT_NAME = 'Bbox'
@ -65,7 +65,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for variable in config[CONF_MONITORED_VARIABLES]:
sensors.append(BboxSensor(bbox_data, variable, name))
add_devices(sensors)
add_devices(sensors, True)
class BboxSensor(Entity):
@ -81,8 +81,6 @@ class BboxSensor(Entity):
self.bbox_data = bbox_data
self._state = None
self.update()
@property
def name(self):
"""Return the name of the sensor."""
@ -107,7 +105,7 @@ class BboxSensor(Entity):
def device_state_attributes(self):
"""Return the state attributes."""
return {
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
ATTR_ATTRIBUTION: ATTRIBUTION,
}
def update(self):

View File

@ -92,10 +92,10 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
dev = [BH1750Sensor(sensor, name, SENSOR_UNIT,
config.get(CONF_MULTIPLIER))]
_LOGGER.info("Setup of BH1750 light sensor at %s in mode %s is complete.",
_LOGGER.info("Setup of BH1750 light sensor at %s in mode %s is complete",
i2c_address, operation_mode)
async_add_devices(dev)
async_add_devices(dev, True)
class BH1750Sensor(Entity):

View File

@ -51,7 +51,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for device in bloomsky.BLOOMSKY.devices.values():
for variable in sensors:
add_devices([BloomSkySensor(bloomsky.BLOOMSKY, device, variable)])
add_devices(
[BloomSkySensor(bloomsky.BLOOMSKY, device, variable)], True)
class BloomSkySensor(Entity):
@ -64,7 +65,7 @@ class BloomSkySensor(Entity):
self._sensor_name = sensor_name
self._name = '{} {}'.format(device['DeviceName'], sensor_name)
self._unique_id = 'bloomsky_sensor {}'.format(self._name)
self.update()
self._state = None
@property
def name(self):

View File

@ -50,20 +50,19 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Broadlink device sensors."""
host = config.get(CONF_HOST)
mac = config.get(CONF_MAC).encode().replace(b':', b'')
mac_addr = binascii.unhexlify(mac)
broadlink_data = BroadlinkData(
config.get(CONF_UPDATE_INTERVAL),
config.get(CONF_HOST),
mac_addr, config.get(CONF_TIMEOUT))
name = config.get(CONF_NAME)
timeout = config.get(CONF_TIMEOUT)
update_interval = config.get(CONF_UPDATE_INTERVAL)
broadlink_data = BroadlinkData(update_interval, host, mac_addr, timeout)
dev = []
for variable in config[CONF_MONITORED_CONDITIONS]:
dev.append(BroadlinkSensor(
config.get(CONF_NAME),
broadlink_data,
variable))
add_devices(dev)
dev.append(BroadlinkSensor(name, broadlink_data, variable))
add_devices(dev, True)
class BroadlinkSensor(Entity):
@ -76,7 +75,6 @@ class BroadlinkSensor(Entity):
self._type = sensor_type
self._broadlink_data = broadlink_data
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self.update()
@property
def name(self):

View File

@ -54,7 +54,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
variable[CONF_SENSOR_TYPE], variable[CONF_OFFSET],
variable.get(CONF_NAME)))
add_devices(dev)
add_devices(dev, True)
class ComedHourlyPricingSensor(Entity):

View File

@ -24,7 +24,7 @@ SENSOR_TYPES = {}
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the ComfoConnect fan platform."""
"""Set up the ComfoConnect fan platform."""
from pycomfoconnect import (
SENSOR_TEMPERATURE_EXTRACT, SENSOR_HUMIDITY_EXTRACT,
SENSOR_TEMPERATURE_OUTDOOR, SENSOR_HUMIDITY_OUTDOOR,
@ -92,8 +92,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices(sensors, True)
return
class ComfoConnectSensor(Entity):
"""Representation of a ComfoConnect sensor."""

View File

@ -86,7 +86,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
except KeyError:
pass
add_devices(dev)
add_devices(dev, True)
class DHTSensor(Entity):
@ -104,7 +104,6 @@ class DHTSensor(Entity):
self.humidity_offset = humidity_offset
self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self.update()
@property
def name(self):

View File

@ -49,11 +49,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.debug("Probing for access to ELIQ Online API")
api.get_data_now(channelid=channel_id)
except OSError as error:
_LOGGER.error("Could not access the ELIQ Online API. "
"Is the configuration valid? %s", error)
_LOGGER.error("Could not access the ELIQ Online API: %s", error)
return False
add_devices([EliqSensor(api, channel_id, name)])
add_devices([EliqSensor(api, channel_id, name)], True)
class EliqSensor(Entity):
@ -65,7 +64,6 @@ class EliqSensor(Entity):
self._state = STATE_UNKNOWN
self._api = api
self._channel_id = channel_id
self.update()
@property
def name(self):

View File

@ -36,8 +36,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(seconds=1800)): (
vol.All(cv.time_period, cv.positive_timedelta)),
vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(seconds=1800)):
vol.All(cv.time_period, cv.positive_timedelta),
})
@ -45,6 +45,10 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Fedex platform."""
import fedexdeliverymanager
name = config.get(CONF_NAME)
update_interval = config.get(CONF_UPDATE_INTERVAL)
try:
cookie = hass.config.path(COOKIE)
session = fedexdeliverymanager.get_session(
@ -54,8 +58,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.exception("Could not connect to Fedex Delivery Manager")
return False
add_devices([FedexSensor(
session, config.get(CONF_NAME), config.get(CONF_UPDATE_INTERVAL))])
add_devices([FedexSensor(session, name, update_interval)], True)
class FedexSensor(Entity):
@ -68,7 +71,6 @@ class FedexSensor(Entity):
self._attributes = None
self._state = None
self.update = Throttle(interval)(self._update)
self.update()
@property
def name(self):

View File

@ -89,7 +89,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
unit_of_measurement=monitored_variable[CONF_UNIT_OF_MEASUREMENT])
devices.append(new_device)
add_devices(devices)
add_devices(devices, True)
class HpIloSensor(Entity):
@ -111,8 +111,6 @@ class HpIloSensor(Entity):
self._state = None
self._state_attributes = None
self.update()
_LOGGER.debug("Created HP ILO sensor %r", self)
@property

View File

@ -38,7 +38,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
config.get(CONF_PORT))
if sensor.connection:
add_devices([sensor])
add_devices([sensor], True)
else:
return False
@ -55,7 +55,6 @@ class ImapSensor(Entity):
self._port = port
self._unread_count = 0
self.connection = self._login()
self.update()
def _login(self):
"""Login and return an IMAP connection."""

View File

@ -53,7 +53,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
config.get(CONF_SENDERS), value_template)
if sensor.connected:
add_devices([sensor])
add_devices([sensor], True)
else:
return False

View File

@ -27,7 +27,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for sensor_type in ('level', 'state'):
dev.append(IOSSensor(sensor_type, device_name, device))
add_devices(dev)
add_devices(dev, True)
class IOSSensor(Entity):
@ -41,7 +41,6 @@ class IOSSensor(Entity):
self.type = sensor_type
self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self.update()
@property
def name(self):

View File

@ -95,7 +95,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
except lnetatmo.NoDevice:
return None
add_devices(dev)
add_devices(dev, True)
class NetAtmoSensor(Entity):
@ -115,7 +115,6 @@ class NetAtmoSensor(Entity):
self.module_id = module_id[1]
self._unique_id = "Netatmo Sensor {0} - {1} ({2})".format(
self._name, module_id, self.type)
self.update()
@property
def name(self):

View File

@ -170,7 +170,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"Cannot continue setup: %s", err)
return False
add_entities(entities)
add_entities(entities, True)
class NUTSensor(Entity):
@ -182,7 +182,7 @@ class NUTSensor(Entity):
self.type = sensor_type
self._name = "{} {}".format(name, SENSOR_TYPES[sensor_type][0])
self._unit = SENSOR_TYPES[sensor_type][1]
self.update()
self._state = None
@property
def name(self):
@ -207,7 +207,7 @@ class NUTSensor(Entity):
@property
def device_state_attributes(self):
"""Return the sensor attributes."""
attr = {}
attr = dict()
attr[ATTR_STATE] = self.opp_state()
return attr

View File

@ -60,7 +60,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
name, SENSOR_TYPES[octo_type][3], SENSOR_TYPES[octo_type][0],
SENSOR_TYPES[octo_type][1])
devices.append(new_sensor)
add_devices(devices)
add_devices(devices, True)
class OctoPrintSensor(Entity):
@ -82,8 +82,6 @@ class OctoPrintSensor(Entity):
self.api_endpoint = endpoint
self.api_group = group
self.api_tool = tool
# Set initial state
self.update()
_LOGGER.debug("Created OctoPrint sensor %r", self)
@property

View File

@ -37,7 +37,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
name = config.get(CONF_NAME)
ohmid = config.get(CONF_ID)
add_devices([OhmconnectSensor(name, ohmid)])
add_devices([OhmconnectSensor(name, ohmid)], True)
class OhmconnectSensor(Entity):
@ -48,7 +48,6 @@ class OhmconnectSensor(Entity):
self._name = name
self._ohmid = ohmid
self._data = {}
self.update()
@property
def name(self):
@ -80,4 +79,4 @@ class OhmconnectSensor(Entity):
self._data[child.tag] = child.text
except requests.exceptions.ConnectionError:
_LOGGER.error("No route to host/endpoint: %s", url)
self.data = {}
self._data = {}

View File

@ -49,7 +49,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for variable in monitored_variables:
dev.append(OpenEVSESensor(variable, charger))
add_devices(dev)
add_devices(dev, True)
class OpenEVSESensor(Entity):

View File

@ -55,7 +55,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
return False
rest.update()
add_devices([OpenexchangeratesSensor(rest, name, quote)])
add_devices([OpenexchangeratesSensor(rest, name, quote)], True)
class OpenexchangeratesSensor(Entity):
@ -66,7 +66,7 @@ class OpenexchangeratesSensor(Entity):
self.rest = rest
self._name = name
self._quote = quote
self.update()
self._state = None
@property
def name(self):