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.
This commit is contained in:
Michaël Arnauts 2019-11-16 15:05:17 +01:00 committed by GitHub
parent 0996b717ce
commit d6e99db38e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 34 deletions

View file

@ -61,6 +61,7 @@ homeassistant/components/cisco_webex_teams/* @fbradyirl
homeassistant/components/ciscospark/* @fbradyirl homeassistant/components/ciscospark/* @fbradyirl
homeassistant/components/cloud/* @home-assistant/cloud homeassistant/components/cloud/* @home-assistant/cloud
homeassistant/components/cloudflare/* @ludeeus homeassistant/components/cloudflare/* @ludeeus
homeassistant/components/comfoconnect/* @michaelarnauts
homeassistant/components/config/* @home-assistant/core homeassistant/components/config/* @home-assistant/core
homeassistant/components/configurator/* @home-assistant/core homeassistant/components/configurator/* @home-assistant/core
homeassistant/components/conversation/* @home-assistant/core homeassistant/components/conversation/* @home-assistant/core

View file

@ -102,7 +102,6 @@ class ComfoConnectBridge:
def __init__(self, hass, bridge, name, token, friendly_name, pin): def __init__(self, hass, bridge, name, token, friendly_name, pin):
"""Initialize the ComfoConnect bridge.""" """Initialize the ComfoConnect bridge."""
self.data = {} self.data = {}
self.name = name self.name = name
self.hass = hass self.hass = hass
@ -136,7 +135,3 @@ class ComfoConnectBridge:
# Notify listeners that we have received an update # Notify listeners that we have received an update
dispatcher_send(self.hass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, var) 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)

View file

@ -17,7 +17,7 @@ from homeassistant.components.fan import (
SUPPORT_SET_SPEED, SUPPORT_SET_SPEED,
FanEntity, FanEntity,
) )
from homeassistant.helpers.dispatcher import dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from . import DOMAIN, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, ComfoConnectBridge 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.""" """Set up the ComfoConnect fan platform."""
ccb = hass.data[DOMAIN] ccb = hass.data[DOMAIN]
add_entities([ComfoConnectFan(hass, name=ccb.name, ccb=ccb)], True) add_entities([ComfoConnectFan(ccb.name, ccb)], True)
class ComfoConnectFan(FanEntity): class ComfoConnectFan(FanEntity):
"""Representation of the ComfoConnect fan platform.""" """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.""" """Initialize the ComfoConnect fan."""
self._ccb = ccb self._ccb = ccb
self._name = name self._name = name
# Ask the bridge to keep us updated async def async_added_to_hass(self):
self._ccb.comfoconnect.register_sensor(SENSOR_FAN_SPEED_MODE) """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(var): def _handle_update(self, var):
if var == SENSOR_FAN_SPEED_MODE: """Handle update callbacks."""
_LOGGER.debug("Dispatcher update for %s", var) if var == SENSOR_FAN_SPEED_MODE:
self.schedule_update_ha_state() _LOGGER.debug("Received update for %s", var)
self.schedule_update_ha_state()
# Register for dispatcher updates @property
dispatcher_connect(hass, SIGNAL_COMFOCONNECT_UPDATE_RECEIVED, _handle_update) def should_poll(self) -> bool:
"""Do not poll."""
return False
@property @property
def name(self): def name(self):
@ -71,7 +79,6 @@ class ComfoConnectFan(FanEntity):
@property @property
def speed(self): def speed(self):
"""Return the current fan mode.""" """Return the current fan mode."""
try: try:
speed = self._ccb.data[SENSOR_FAN_SPEED_MODE] speed = self._ccb.data[SENSOR_FAN_SPEED_MODE]
return SPEED_MAPPING[speed] return SPEED_MAPPING[speed]
@ -95,7 +102,7 @@ class ComfoConnectFan(FanEntity):
def set_speed(self, speed: str): def set_speed(self, speed: str):
"""Set fan speed.""" """Set fan speed."""
_LOGGER.debug("Changing fan speed to %s.", speed) _LOGGER.debug("Changing fan speed to %s", speed)
if speed == SPEED_OFF: if speed == SPEED_OFF:
self._ccb.comfoconnect.cmd_rmi_request(CMD_FAN_MODE_AWAY) self._ccb.comfoconnect.cmd_rmi_request(CMD_FAN_MODE_AWAY)

View file

@ -6,5 +6,5 @@
"pycomfoconnect==0.3" "pycomfoconnect==0.3"
], ],
"dependencies": [], "dependencies": [],
"codeowners": [] "codeowners": ["@michaelarnauts"]
} }

View file

@ -11,7 +11,7 @@ from pycomfoconnect import (
) )
from homeassistant.const import CONF_RESOURCES, TEMP_CELSIUS 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 homeassistant.helpers.entity import Entity
from . import ( from . import (
@ -81,13 +81,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
sensor_type = resource.lower() sensor_type = resource.lower()
if sensor_type not in SENSOR_TYPES: 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 continue
sensors.append( sensors.append(
ComfoConnectSensor( ComfoConnectSensor(
hass, name=f"{ccb.name} {SENSOR_TYPES[sensor_type][0]}",
name="%s %s" % (ccb.name, SENSOR_TYPES[sensor_type][0]),
ccb=ccb, ccb=ccb,
sensor_type=sensor_type, sensor_type=sensor_type,
) )
@ -99,23 +98,27 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class ComfoConnectSensor(Entity): class ComfoConnectSensor(Entity):
"""Representation of a ComfoConnect sensor.""" """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.""" """Initialize the ComfoConnect sensor."""
self._ccb = ccb self._ccb = ccb
self._sensor_type = sensor_type self._sensor_type = sensor_type
self._sensor_id = SENSOR_TYPES[self._sensor_type][3] self._sensor_id = SENSOR_TYPES[self._sensor_type][3]
self._name = name self._name = name
# Register the requested sensor async def async_added_to_hass(self):
self._ccb.comfoconnect.register_sensor(self._sensor_id) """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
)
def _handle_update(var): def _handle_update(self, var):
if var == self._sensor_id: """Handle update callbacks."""
_LOGGER.debug("Dispatcher update for %s.", var) if var == self._sensor_id:
self.schedule_update_ha_state() _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 @property
def state(self): def state(self):
@ -125,6 +128,11 @@ class ComfoConnectSensor(Entity):
except KeyError: except KeyError:
return None return None
@property
def should_poll(self) -> bool:
"""Do not poll."""
return False
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""