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

This commit is contained in:
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 requests
import voluptuous as vol import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ( from homeassistant.const import (
CONF_NAME, CONF_MONITORED_VARIABLES, ATTR_ATTRIBUTION) CONF_NAME, CONF_MONITORED_VARIABLES, ATTR_ATTRIBUTION)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['pybbox==0.0.5-alpha'] REQUIREMENTS = ['pybbox==0.0.5-alpha']
@ -23,7 +23,7 @@ _LOGGER = logging.getLogger(__name__)
BANDWIDTH_MEGABITS_SECONDS = 'Mb/s' # type: str BANDWIDTH_MEGABITS_SECONDS = 'Mb/s' # type: str
CONF_ATTRIBUTION = "Powered by Bouygues Telecom" ATTRIBUTION = "Powered by Bouygues Telecom"
DEFAULT_NAME = 'Bbox' DEFAULT_NAME = 'Bbox'
@ -65,7 +65,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for variable in config[CONF_MONITORED_VARIABLES]: for variable in config[CONF_MONITORED_VARIABLES]:
sensors.append(BboxSensor(bbox_data, variable, name)) sensors.append(BboxSensor(bbox_data, variable, name))
add_devices(sensors) add_devices(sensors, True)
class BboxSensor(Entity): class BboxSensor(Entity):
@ -81,8 +81,6 @@ class BboxSensor(Entity):
self.bbox_data = bbox_data self.bbox_data = bbox_data
self._state = None self._state = None
self.update()
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
@ -107,7 +105,7 @@ class BboxSensor(Entity):
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
return { return {
ATTR_ATTRIBUTION: CONF_ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,
} }
def update(self): 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, dev = [BH1750Sensor(sensor, name, SENSOR_UNIT,
config.get(CONF_MULTIPLIER))] 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) i2c_address, operation_mode)
async_add_devices(dev) async_add_devices(dev, True)
class BH1750Sensor(Entity): 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 device in bloomsky.BLOOMSKY.devices.values():
for variable in sensors: for variable in sensors:
add_devices([BloomSkySensor(bloomsky.BLOOMSKY, device, variable)]) add_devices(
[BloomSkySensor(bloomsky.BLOOMSKY, device, variable)], True)
class BloomSkySensor(Entity): class BloomSkySensor(Entity):
@ -64,7 +65,7 @@ class BloomSkySensor(Entity):
self._sensor_name = sensor_name self._sensor_name = sensor_name
self._name = '{} {}'.format(device['DeviceName'], sensor_name) self._name = '{} {}'.format(device['DeviceName'], sensor_name)
self._unique_id = 'bloomsky_sensor {}'.format(self._name) self._unique_id = 'bloomsky_sensor {}'.format(self._name)
self.update() self._state = None
@property @property
def name(self): def name(self):

View File

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

View File

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

View File

@ -86,7 +86,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
except KeyError: except KeyError:
pass pass
add_devices(dev) add_devices(dev, True)
class DHTSensor(Entity): class DHTSensor(Entity):
@ -104,7 +104,6 @@ class DHTSensor(Entity):
self.humidity_offset = humidity_offset self.humidity_offset = humidity_offset
self._state = None self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self.update()
@property @property
def name(self): 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") _LOGGER.debug("Probing for access to ELIQ Online API")
api.get_data_now(channelid=channel_id) api.get_data_now(channelid=channel_id)
except OSError as error: except OSError as error:
_LOGGER.error("Could not access the ELIQ Online API. " _LOGGER.error("Could not access the ELIQ Online API: %s", error)
"Is the configuration valid? %s", error)
return False return False
add_devices([EliqSensor(api, channel_id, name)]) add_devices([EliqSensor(api, channel_id, name)], True)
class EliqSensor(Entity): class EliqSensor(Entity):
@ -65,7 +64,6 @@ class EliqSensor(Entity):
self._state = STATE_UNKNOWN self._state = STATE_UNKNOWN
self._api = api self._api = api
self._channel_id = channel_id self._channel_id = channel_id
self.update()
@property @property
def name(self): def name(self):

View File

@ -36,8 +36,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string, vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_NAME): cv.string, vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(seconds=1800)): ( vol.Optional(CONF_UPDATE_INTERVAL, default=timedelta(seconds=1800)):
vol.All(cv.time_period, cv.positive_timedelta)), 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): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Fedex platform.""" """Set up the Fedex platform."""
import fedexdeliverymanager import fedexdeliverymanager
name = config.get(CONF_NAME)
update_interval = config.get(CONF_UPDATE_INTERVAL)
try: try:
cookie = hass.config.path(COOKIE) cookie = hass.config.path(COOKIE)
session = fedexdeliverymanager.get_session( 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") _LOGGER.exception("Could not connect to Fedex Delivery Manager")
return False return False
add_devices([FedexSensor( add_devices([FedexSensor(session, name, update_interval)], True)
session, config.get(CONF_NAME), config.get(CONF_UPDATE_INTERVAL))])
class FedexSensor(Entity): class FedexSensor(Entity):
@ -68,7 +71,6 @@ class FedexSensor(Entity):
self._attributes = None self._attributes = None
self._state = None self._state = None
self.update = Throttle(interval)(self._update) self.update = Throttle(interval)(self._update)
self.update()
@property @property
def name(self): 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]) unit_of_measurement=monitored_variable[CONF_UNIT_OF_MEASUREMENT])
devices.append(new_device) devices.append(new_device)
add_devices(devices) add_devices(devices, True)
class HpIloSensor(Entity): class HpIloSensor(Entity):
@ -111,8 +111,6 @@ class HpIloSensor(Entity):
self._state = None self._state = None
self._state_attributes = None self._state_attributes = None
self.update()
_LOGGER.debug("Created HP ILO sensor %r", self) _LOGGER.debug("Created HP ILO sensor %r", self)
@property @property

View File

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

View File

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

View File

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

View File

@ -170,7 +170,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"Cannot continue setup: %s", err) "Cannot continue setup: %s", err)
return False return False
add_entities(entities) add_entities(entities, True)
class NUTSensor(Entity): class NUTSensor(Entity):
@ -182,7 +182,7 @@ class NUTSensor(Entity):
self.type = sensor_type self.type = sensor_type
self._name = "{} {}".format(name, SENSOR_TYPES[sensor_type][0]) self._name = "{} {}".format(name, SENSOR_TYPES[sensor_type][0])
self._unit = SENSOR_TYPES[sensor_type][1] self._unit = SENSOR_TYPES[sensor_type][1]
self.update() self._state = None
@property @property
def name(self): def name(self):
@ -207,7 +207,7 @@ class NUTSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the sensor attributes.""" """Return the sensor attributes."""
attr = {} attr = dict()
attr[ATTR_STATE] = self.opp_state() attr[ATTR_STATE] = self.opp_state()
return attr 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], name, SENSOR_TYPES[octo_type][3], SENSOR_TYPES[octo_type][0],
SENSOR_TYPES[octo_type][1]) SENSOR_TYPES[octo_type][1])
devices.append(new_sensor) devices.append(new_sensor)
add_devices(devices) add_devices(devices, True)
class OctoPrintSensor(Entity): class OctoPrintSensor(Entity):
@ -82,8 +82,6 @@ class OctoPrintSensor(Entity):
self.api_endpoint = endpoint self.api_endpoint = endpoint
self.api_group = group self.api_group = group
self.api_tool = tool self.api_tool = tool
# Set initial state
self.update()
_LOGGER.debug("Created OctoPrint sensor %r", self) _LOGGER.debug("Created OctoPrint sensor %r", self)
@property @property

View File

@ -37,7 +37,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
ohmid = config.get(CONF_ID) ohmid = config.get(CONF_ID)
add_devices([OhmconnectSensor(name, ohmid)]) add_devices([OhmconnectSensor(name, ohmid)], True)
class OhmconnectSensor(Entity): class OhmconnectSensor(Entity):
@ -48,7 +48,6 @@ class OhmconnectSensor(Entity):
self._name = name self._name = name
self._ohmid = ohmid self._ohmid = ohmid
self._data = {} self._data = {}
self.update()
@property @property
def name(self): def name(self):
@ -80,4 +79,4 @@ class OhmconnectSensor(Entity):
self._data[child.tag] = child.text self._data[child.tag] = child.text
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.error("No route to host/endpoint: %s", url) _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: for variable in monitored_variables:
dev.append(OpenEVSESensor(variable, charger)) dev.append(OpenEVSESensor(variable, charger))
add_devices(dev) add_devices(dev, True)
class OpenEVSESensor(Entity): class OpenEVSESensor(Entity):

View File

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