From d6e99db38e04a80bb596e38e4e0375fa3351f1de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Arnauts?= <michael.arnauts@destiny.be> Date: Sat, 16 Nov 2019 15:05:17 +0100 Subject: [PATCH] Fix Comfoconnect errors during startup (#28802) * Add callback registrations to async_added_to_hass * Fix CODEOWNERS * Fix code formatting * Requested changes. * Don't pass unused hass and fix string formatting * Fix import order. --- CODEOWNERS | 1 + .../components/comfoconnect/__init__.py | 5 --- homeassistant/components/comfoconnect/fan.py | 37 +++++++++++-------- .../components/comfoconnect/manifest.json | 2 +- .../components/comfoconnect/sensor.py | 36 +++++++++++------- 5 files changed, 46 insertions(+), 35 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index 775fd8be5c1..ee6a8cd169c 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -61,6 +61,7 @@ homeassistant/components/cisco_webex_teams/* @fbradyirl homeassistant/components/ciscospark/* @fbradyirl homeassistant/components/cloud/* @home-assistant/cloud homeassistant/components/cloudflare/* @ludeeus +homeassistant/components/comfoconnect/* @michaelarnauts homeassistant/components/config/* @home-assistant/core homeassistant/components/configurator/* @home-assistant/core homeassistant/components/conversation/* @home-assistant/core diff --git a/homeassistant/components/comfoconnect/__init__.py b/homeassistant/components/comfoconnect/__init__.py index aef4bf1deeb..efdbf020f1a 100644 --- a/homeassistant/components/comfoconnect/__init__.py +++ b/homeassistant/components/comfoconnect/__init__.py @@ -102,7 +102,6 @@ class ComfoConnectBridge: def __init__(self, hass, bridge, name, token, friendly_name, pin): """Initialize the ComfoConnect bridge.""" - self.data = {} self.name = name self.hass = hass @@ -136,7 +135,3 @@ class ComfoConnectBridge: # Notify listeners that we have received an update dispatcher_send(self.hass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, var) - - def subscribe_sensor(self, sensor_id): - """Subscribe for the specified sensor.""" - self.comfoconnect.register_sensor(sensor_id) diff --git a/homeassistant/components/comfoconnect/fan.py b/homeassistant/components/comfoconnect/fan.py index bbb4b0176bf..34e784d61eb 100644 --- a/homeassistant/components/comfoconnect/fan.py +++ b/homeassistant/components/comfoconnect/fan.py @@ -17,7 +17,7 @@ from homeassistant.components.fan import ( SUPPORT_SET_SPEED, FanEntity, ) -from homeassistant.helpers.dispatcher import dispatcher_connect +from homeassistant.helpers.dispatcher import async_dispatcher_connect from . import DOMAIN, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, ComfoConnectBridge @@ -30,28 +30,36 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the ComfoConnect fan platform.""" ccb = hass.data[DOMAIN] - add_entities([ComfoConnectFan(hass, name=ccb.name, ccb=ccb)], True) + add_entities([ComfoConnectFan(ccb.name, ccb)], True) class ComfoConnectFan(FanEntity): """Representation of the ComfoConnect fan platform.""" - def __init__(self, hass, name, ccb: ComfoConnectBridge) -> None: + def __init__(self, name, ccb: ComfoConnectBridge) -> None: """Initialize the ComfoConnect fan.""" - self._ccb = ccb self._name = name - # Ask the bridge to keep us updated - self._ccb.comfoconnect.register_sensor(SENSOR_FAN_SPEED_MODE) - - def _handle_update(var): - if var == SENSOR_FAN_SPEED_MODE: - _LOGGER.debug("Dispatcher update for %s", var) - self.schedule_update_ha_state() + async def async_added_to_hass(self): + """Register for sensor updates.""" + await self.hass.async_add_executor_job( + self._ccb.comfoconnect.register_sensor, SENSOR_FAN_SPEED_MODE + ) + async_dispatcher_connect( + self.hass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, self._handle_update + ) + + def _handle_update(self, var): + """Handle update callbacks.""" + if var == SENSOR_FAN_SPEED_MODE: + _LOGGER.debug("Received update for %s", var) + self.schedule_update_ha_state() - # Register for dispatcher updates - dispatcher_connect(hass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, _handle_update) + @property + def should_poll(self) -> bool: + """Do not poll.""" + return False @property def name(self): @@ -71,7 +79,6 @@ class ComfoConnectFan(FanEntity): @property def speed(self): """Return the current fan mode.""" - try: speed = self._ccb.data[SENSOR_FAN_SPEED_MODE] return SPEED_MAPPING[speed] @@ -95,7 +102,7 @@ class ComfoConnectFan(FanEntity): def set_speed(self, speed: str): """Set fan speed.""" - _LOGGER.debug("Changing fan speed to %s.", speed) + _LOGGER.debug("Changing fan speed to %s", speed) if speed == SPEED_OFF: self._ccb.comfoconnect.cmd_rmi_request(CMD_FAN_MODE_AWAY) diff --git a/homeassistant/components/comfoconnect/manifest.json b/homeassistant/components/comfoconnect/manifest.json index 57daba7fdbd..091b7f7bcdd 100644 --- a/homeassistant/components/comfoconnect/manifest.json +++ b/homeassistant/components/comfoconnect/manifest.json @@ -6,5 +6,5 @@ "pycomfoconnect==0.3" ], "dependencies": [], - "codeowners": [] + "codeowners": ["@michaelarnauts"] } diff --git a/homeassistant/components/comfoconnect/sensor.py b/homeassistant/components/comfoconnect/sensor.py index 06d0506e2cf..a1f16ed9631 100644 --- a/homeassistant/components/comfoconnect/sensor.py +++ b/homeassistant/components/comfoconnect/sensor.py @@ -11,7 +11,7 @@ from pycomfoconnect import ( ) from homeassistant.const import CONF_RESOURCES, TEMP_CELSIUS -from homeassistant.helpers.dispatcher import dispatcher_connect +from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity from . import ( @@ -81,13 +81,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None): sensor_type = resource.lower() if sensor_type not in SENSOR_TYPES: - _LOGGER.warning("Sensor type: %s is not a valid sensor.", sensor_type) + _LOGGER.warning("Sensor type: %s is not a valid sensor", sensor_type) continue sensors.append( ComfoConnectSensor( - hass, - name="%s %s" % (ccb.name, SENSOR_TYPES[sensor_type][0]), + name=f"{ccb.name} {SENSOR_TYPES[sensor_type][0]}", ccb=ccb, sensor_type=sensor_type, ) @@ -99,23 +98,27 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class ComfoConnectSensor(Entity): """Representation of a ComfoConnect sensor.""" - def __init__(self, hass, name, ccb: ComfoConnectBridge, sensor_type) -> None: + def __init__(self, name, ccb: ComfoConnectBridge, sensor_type) -> None: """Initialize the ComfoConnect sensor.""" self._ccb = ccb self._sensor_type = sensor_type self._sensor_id = SENSOR_TYPES[self._sensor_type][3] self._name = name - # Register the requested sensor - self._ccb.comfoconnect.register_sensor(self._sensor_id) - - def _handle_update(var): - if var == self._sensor_id: - _LOGGER.debug("Dispatcher update for %s.", var) - self.schedule_update_ha_state() + async def async_added_to_hass(self): + """Register for sensor updates.""" + await self.hass.async_add_executor_job( + self._ccb.comfoconnect.register_sensor, self._sensor_id + ) + async_dispatcher_connect( + self.hass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, self._handle_update + ) - # Register for dispatcher updates - dispatcher_connect(hass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, _handle_update) + def _handle_update(self, var): + """Handle update callbacks.""" + if var == self._sensor_id: + _LOGGER.debug("Received update for %s", var) + self.schedule_update_ha_state() @property def state(self): @@ -125,6 +128,11 @@ class ComfoConnectSensor(Entity): except KeyError: return None + @property + def should_poll(self) -> bool: + """Do not poll.""" + return False + @property def name(self): """Return the name of the sensor.""" -- GitLab