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

@ -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."""