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/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

View file

@ -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)

View file

@ -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)
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(var):
if var == SENSOR_FAN_SPEED_MODE:
_LOGGER.debug("Dispatcher update for %s", var)
self.schedule_update_ha_state()
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)

View file

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

View file

@ -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)
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
)
def _handle_update(var):
if var == self._sensor_id:
_LOGGER.debug("Dispatcher update for %s.", var)
self.schedule_update_ha_state()
# 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."""