Add sensor platform for vicare integration (heatpump) (#34385)
* Add sensor platform for vicare (heatpump) * Formatting and fixes * Formatting and fixes 2 * Fixes and formatting 3 * Fixes and formatting 4 * Add binary_sensor and more sensors This moves some more climate attributes to sensors and adds binary_sensors * Move ActiveError back to climate component The data returned by ActiveError is more complex. It takes further investigation on how to interpret it a s a binary sensor. Therefore it is moved back as an attribute of the climate component * Update PyViCare library * PR changes * PR changes 2 Co-authored-by: Hans Oischinger <hans.oischinger@gmail.com>
This commit is contained in:
parent
0bfcb99c04
commit
0bbb56dd05
7 changed files with 424 additions and 21 deletions
123
homeassistant/components/vicare/binary_sensor.py
Normal file
123
homeassistant/components/vicare/binary_sensor.py
Normal file
|
@ -0,0 +1,123 @@
|
|||
"""Viessmann ViCare sensor device."""
|
||||
import logging
|
||||
|
||||
import requests
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASS_POWER,
|
||||
BinarySensorDevice,
|
||||
)
|
||||
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
|
||||
|
||||
from . import (
|
||||
DOMAIN as VICARE_DOMAIN,
|
||||
PYVICARE_ERROR,
|
||||
VICARE_API,
|
||||
VICARE_HEATING_TYPE,
|
||||
VICARE_NAME,
|
||||
HeatingType,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_GETTER = "getter"
|
||||
|
||||
SENSOR_CIRCULATION_PUMP_ACTIVE = "circulationpump_active"
|
||||
SENSOR_BURNER_ACTIVE = "burner_active"
|
||||
SENSOR_COMPRESSOR_ACTIVE = "compressor_active"
|
||||
|
||||
SENSOR_TYPES = {
|
||||
SENSOR_CIRCULATION_PUMP_ACTIVE: {
|
||||
CONF_NAME: "Circulation pump active",
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_POWER,
|
||||
CONF_GETTER: lambda api: api.getCirculationPumpActive(),
|
||||
},
|
||||
# gas sensors
|
||||
SENSOR_BURNER_ACTIVE: {
|
||||
CONF_NAME: "Burner active",
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_POWER,
|
||||
CONF_GETTER: lambda api: api.getBurnerActive(),
|
||||
},
|
||||
# heatpump sensors
|
||||
SENSOR_COMPRESSOR_ACTIVE: {
|
||||
CONF_NAME: "Compressor active",
|
||||
CONF_DEVICE_CLASS: DEVICE_CLASS_POWER,
|
||||
CONF_GETTER: lambda api: api.getCompressorActive(),
|
||||
},
|
||||
}
|
||||
|
||||
SENSORS_GENERIC = [SENSOR_CIRCULATION_PUMP_ACTIVE]
|
||||
|
||||
SENSORS_BY_HEATINGTYPE = {
|
||||
HeatingType.gas: [SENSOR_BURNER_ACTIVE],
|
||||
HeatingType.heatpump: [SENSOR_COMPRESSOR_ACTIVE],
|
||||
}
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Create the ViCare sensor devices."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
vicare_api = hass.data[VICARE_DOMAIN][VICARE_API]
|
||||
heating_type = hass.data[VICARE_DOMAIN][VICARE_HEATING_TYPE]
|
||||
|
||||
sensors = SENSORS_GENERIC.copy()
|
||||
|
||||
if heating_type != HeatingType.generic:
|
||||
sensors.extend(SENSORS_BY_HEATINGTYPE[heating_type])
|
||||
|
||||
add_entities(
|
||||
[
|
||||
ViCareBinarySensor(
|
||||
hass.data[VICARE_DOMAIN][VICARE_NAME], vicare_api, sensor
|
||||
)
|
||||
for sensor in sensors
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class ViCareBinarySensor(BinarySensorDevice):
|
||||
"""Representation of a ViCare sensor."""
|
||||
|
||||
def __init__(self, name, api, sensor_type):
|
||||
"""Initialize the sensor."""
|
||||
self._sensor = SENSOR_TYPES[sensor_type]
|
||||
self._name = f"{name} {self._sensor[CONF_NAME]}"
|
||||
self._api = api
|
||||
self._sensor_type = sensor_type
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
return self._state is not None and self._state != PYVICARE_ERROR
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique ID."""
|
||||
return f"{self._api.service.id}-{self._sensor_type}"
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
||||
return self._sensor[CONF_DEVICE_CLASS]
|
||||
|
||||
def update(self):
|
||||
"""Update state of sensor."""
|
||||
try:
|
||||
self._state = self._sensor[CONF_GETTER](self._api)
|
||||
except requests.exceptions.ConnectionError:
|
||||
_LOGGER.error("Unable to retrieve data from ViCare server")
|
||||
except ValueError:
|
||||
_LOGGER.error("Unable to decode data from ViCare server")
|
Loading…
Add table
Add a link
Reference in a new issue