core/homeassistant/components/homematicip_cloud/sensor.py

403 lines
13 KiB
Python
Raw Normal View History

"""Support for HomematicIP Cloud sensors."""
import logging
from homematicip.aio.device import (
2019-07-31 19:25:30 +00:00
AsyncBrandSwitchMeasuring,
AsyncFullFlushSwitchMeasuring,
AsyncHeatingThermostat,
AsyncHeatingThermostatCompact,
AsyncLightSensor,
AsyncMotionDetectorIndoor,
AsyncMotionDetectorOutdoor,
AsyncMotionDetectorPushButton,
AsyncPassageDetector,
2019-07-31 19:25:30 +00:00
AsyncPlugableSwitchMeasuring,
AsyncPresenceDetectorIndoor,
AsyncTemperatureHumiditySensorDisplay,
AsyncTemperatureHumiditySensorOutdoor,
2019-07-31 19:25:30 +00:00
AsyncTemperatureHumiditySensorWithoutDisplay,
AsyncWeatherSensor,
AsyncWeatherSensorPlus,
AsyncWeatherSensorPro,
)
from homematicip.base.enums import ValveState
2019-04-25 22:13:07 +00:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_POWER,
DEVICE_CLASS_TEMPERATURE,
POWER_WATT,
TEMP_CELSIUS,
)
2019-04-25 22:13:07 +00:00
from homeassistant.core import HomeAssistant
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
from .device import ATTR_IS_GROUP, ATTR_MODEL_TYPE
from .hap import HomematicipHAP
_LOGGER = logging.getLogger(__name__)
ATTR_LEFT_COUNTER = "left_counter"
ATTR_RIGHT_COUNTER = "right_counter"
2019-07-31 19:25:30 +00:00
ATTR_TEMPERATURE_OFFSET = "temperature_offset"
ATTR_WIND_DIRECTION = "wind_direction"
ATTR_WIND_DIRECTION_VARIATION = "wind_direction_variation_in_degree"
2019-07-31 19:25:30 +00:00
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
2018-08-21 19:25:16 +00:00
"""Set up the HomematicIP Cloud sensors devices."""
Add HomematicIP Cloud Config Flow and Entries loading (#14861) * Add HomematicIP Cloud to config flow * Inititial trial for config_flow * Integrations text files * Load and write config_flow and init homematicip_cloud * Split into dedicated files * Ceanup of text messages * Working config_flow * Move imports inside a function * Enable laoding even no accesspoints are defined * Revert unnecassary changes in CONFIG_SCHEMA * Better error handling * fix flask8 * Migration to async for token generation * A few fixes * Simplify config_flow * Bump version to 9.6 with renamed package * Requirements file * First fixes after review * Implement async_step_import * Cleanup for Config Flow * First tests for homematicip_cloud setup * Remove config_flow tests * Really remove all things * Fix comment * Update picture * Add support for async_setup_entry to switch and climate platform * Update path of the config_flow picture * Refactoring for better tesability * Further tests implemented * Move 3th party lib inside function * Fix lint * Update requirments_test_all.txt file * UPdate of requirments_test_all.txt did not work * Furder cleanup in websocket connection * Remove a test for the hap * Revert "Remove a test for the hap" This reverts commit 968d58cba108e0f371022c7ab540374aa2ab13f4. * First tests implemented for config_flow * Fix lint * Rework of client registration process * Implemented tests for config_flow 100% coverage * Cleanup * Cleanup comments and code * Try to fix import problem * Add homematicip to the test env requirements
2018-07-06 21:05:34 +00:00
pass
2019-07-31 19:25:30 +00:00
async def async_setup_entry(
hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities
) -> None:
2018-08-21 19:25:16 +00:00
"""Set up the HomematicIP Cloud sensors from a config entry."""
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
devices = [HomematicipAccesspointStatus(hap)]
for device in hap.home.devices:
2019-07-31 19:25:30 +00:00
if isinstance(device, (AsyncHeatingThermostat, AsyncHeatingThermostatCompact)):
devices.append(HomematicipHeatingThermostat(hap, device))
devices.append(HomematicipTemperatureSensor(hap, device))
2019-07-31 19:25:30 +00:00
if isinstance(
device,
(
AsyncTemperatureHumiditySensorDisplay,
AsyncTemperatureHumiditySensorWithoutDisplay,
AsyncTemperatureHumiditySensorOutdoor,
AsyncWeatherSensor,
AsyncWeatherSensorPlus,
AsyncWeatherSensorPro,
),
):
devices.append(HomematicipTemperatureSensor(hap, device))
devices.append(HomematicipHumiditySensor(hap, device))
2019-07-31 19:25:30 +00:00
if isinstance(
device,
(
AsyncLightSensor,
AsyncMotionDetectorIndoor,
AsyncMotionDetectorOutdoor,
AsyncMotionDetectorPushButton,
AsyncPresenceDetectorIndoor,
AsyncWeatherSensor,
AsyncWeatherSensorPlus,
AsyncWeatherSensorPro,
),
):
devices.append(HomematicipIlluminanceSensor(hap, device))
2019-07-31 19:25:30 +00:00
if isinstance(
device,
(
AsyncPlugableSwitchMeasuring,
AsyncBrandSwitchMeasuring,
AsyncFullFlushSwitchMeasuring,
),
):
devices.append(HomematicipPowerSensor(hap, device))
2019-07-31 19:25:30 +00:00
if isinstance(
device, (AsyncWeatherSensor, AsyncWeatherSensorPlus, AsyncWeatherSensorPro)
):
devices.append(HomematicipWindspeedSensor(hap, device))
2019-07-31 19:25:30 +00:00
if isinstance(device, (AsyncWeatherSensorPlus, AsyncWeatherSensorPro)):
devices.append(HomematicipTodayRainSensor(hap, device))
if isinstance(device, AsyncPassageDetector):
devices.append(HomematicipPassageDetectorDeltaCounter(hap, device))
if devices:
async_add_entities(devices)
class HomematicipAccesspointStatus(HomematicipGenericDevice):
2018-08-21 19:25:16 +00:00
"""Representation of an HomeMaticIP Cloud access point."""
def __init__(self, hap: HomematicipHAP) -> None:
"""Initialize access point device."""
super().__init__(hap, hap.home)
@property
def device_info(self):
"""Return device specific attributes."""
# Adds a sensor to the existing HAP device
return {
2019-07-31 19:25:30 +00:00
"identifiers": {
# Serial numbers of Homematic IP device
(HMIPC_DOMAIN, self._device.id)
}
}
@property
2019-04-25 22:13:07 +00:00
def icon(self) -> str:
"""Return the icon of the access point device."""
2019-07-31 19:25:30 +00:00
return "mdi:access-point-network"
@property
2019-04-25 22:13:07 +00:00
def state(self) -> float:
"""Return the state of the access point."""
return self._home.dutyCycle
@property
2019-04-25 22:13:07 +00:00
def available(self) -> bool:
"""Device available."""
return self._home.connected
@property
2019-04-25 22:13:07 +00:00
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
2019-07-31 19:25:30 +00:00
return "%"
@property
def device_state_attributes(self):
"""Return the state attributes of the access point."""
state_attr = super().device_state_attributes
state_attr[ATTR_MODEL_TYPE] = "HmIP-HAP"
state_attr[ATTR_IS_GROUP] = False
return state_attr
class HomematicipHeatingThermostat(HomematicipGenericDevice):
2019-08-02 21:20:07 +00:00
"""Representation of a HomematicIP heating thermostat device."""
def __init__(self, hap: HomematicipHAP, device) -> None:
"""Initialize heating thermostat device."""
super().__init__(hap, device, "Heating")
@property
2019-04-25 22:13:07 +00:00
def icon(self) -> str:
"""Return the icon."""
if super().icon:
return super().icon
if self._device.valveState != ValveState.ADAPTION_DONE:
2019-07-31 19:25:30 +00:00
return "mdi:alert"
return "mdi:radiator"
@property
2019-04-25 22:13:07 +00:00
def state(self) -> int:
"""Return the state of the radiator valve."""
if self._device.valveState != ValveState.ADAPTION_DONE:
return self._device.valveState
2019-07-31 19:25:30 +00:00
return round(self._device.valvePosition * 100)
@property
2019-04-25 22:13:07 +00:00
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
2019-07-31 19:25:30 +00:00
return "%"
class HomematicipHumiditySensor(HomematicipGenericDevice):
2019-08-02 21:20:07 +00:00
"""Representation of a HomematicIP Cloud humidity device."""
def __init__(self, hap: HomematicipHAP, device) -> None:
"""Initialize the thermometer device."""
super().__init__(hap, device, "Humidity")
@property
2019-04-25 22:13:07 +00:00
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_HUMIDITY
@property
2019-04-25 22:13:07 +00:00
def state(self) -> int:
"""Return the state."""
return self._device.humidity
@property
2019-04-25 22:13:07 +00:00
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
2019-07-31 19:25:30 +00:00
return "%"
class HomematicipTemperatureSensor(HomematicipGenericDevice):
2018-08-21 19:25:16 +00:00
"""Representation of a HomematicIP Cloud thermometer device."""
def __init__(self, hap: HomematicipHAP, device) -> None:
"""Initialize the thermometer device."""
super().__init__(hap, device, "Temperature")
@property
2019-04-25 22:13:07 +00:00
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_TEMPERATURE
@property
2019-04-25 22:13:07 +00:00
def state(self) -> float:
"""Return the state."""
2019-07-31 19:25:30 +00:00
if hasattr(self._device, "valveActualTemperature"):
return self._device.valveActualTemperature
return self._device.actualTemperature
@property
2019-04-25 22:13:07 +00:00
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
return TEMP_CELSIUS
@property
def device_state_attributes(self):
"""Return the state attributes of the windspeed sensor."""
state_attr = super().device_state_attributes
temperature_offset = getattr(self._device, "temperatureOffset", None)
if temperature_offset:
state_attr[ATTR_TEMPERATURE_OFFSET] = temperature_offset
return state_attr
class HomematicipIlluminanceSensor(HomematicipGenericDevice):
2019-08-02 21:20:07 +00:00
"""Representation of a HomematicIP Illuminance device."""
def __init__(self, hap: HomematicipHAP, device) -> None:
"""Initialize the device."""
super().__init__(hap, device, "Illuminance")
@property
2019-04-25 22:13:07 +00:00
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_ILLUMINANCE
@property
2019-04-25 22:13:07 +00:00
def state(self) -> float:
"""Return the state."""
2019-07-31 19:25:30 +00:00
if hasattr(self._device, "averageIllumination"):
return self._device.averageIllumination
return self._device.illumination
@property
2019-04-25 22:13:07 +00:00
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
2019-07-31 19:25:30 +00:00
return "lx"
class HomematicipPowerSensor(HomematicipGenericDevice):
2019-08-02 21:20:07 +00:00
"""Representation of a HomematicIP power measuring device."""
def __init__(self, hap: HomematicipHAP, device) -> None:
"""Initialize the device."""
super().__init__(hap, device, "Power")
@property
2019-04-25 22:13:07 +00:00
def device_class(self) -> str:
"""Return the device class of the sensor."""
return DEVICE_CLASS_POWER
@property
2019-04-25 22:13:07 +00:00
def state(self) -> float:
2019-08-02 21:20:07 +00:00
"""Representation of the HomematicIP power comsumption value."""
return self._device.currentPowerConsumption
@property
2019-04-25 22:13:07 +00:00
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
return POWER_WATT
class HomematicipWindspeedSensor(HomematicipGenericDevice):
2019-08-02 21:20:07 +00:00
"""Representation of a HomematicIP wind speed sensor."""
def __init__(self, hap: HomematicipHAP, device) -> None:
"""Initialize the device."""
super().__init__(hap, device, "Windspeed")
@property
2019-04-25 22:13:07 +00:00
def state(self) -> float:
2019-08-02 21:20:07 +00:00
"""Representation of the HomematicIP wind speed value."""
return self._device.windSpeed
@property
2019-04-25 22:13:07 +00:00
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
2019-07-31 19:25:30 +00:00
return "km/h"
@property
def device_state_attributes(self):
"""Return the state attributes of the wind speed sensor."""
state_attr = super().device_state_attributes
wind_direction = getattr(self._device, "windDirection", None)
if wind_direction is not None:
state_attr[ATTR_WIND_DIRECTION] = _get_wind_direction(wind_direction)
wind_direction_variation = getattr(self._device, "windDirectionVariation", None)
if wind_direction_variation:
state_attr[ATTR_WIND_DIRECTION_VARIATION] = wind_direction_variation
return state_attr
class HomematicipTodayRainSensor(HomematicipGenericDevice):
2019-08-02 21:20:07 +00:00
"""Representation of a HomematicIP rain counter of a day sensor."""
def __init__(self, hap: HomematicipHAP, device) -> None:
"""Initialize the device."""
super().__init__(hap, device, "Today Rain")
@property
2019-04-25 22:13:07 +00:00
def state(self) -> float:
2019-08-02 21:20:07 +00:00
"""Representation of the HomematicIP todays rain value."""
return round(self._device.todayRainCounter, 2)
@property
2019-04-25 22:13:07 +00:00
def unit_of_measurement(self) -> str:
"""Return the unit this state is expressed in."""
2019-07-31 19:25:30 +00:00
return "mm"
class HomematicipPassageDetectorDeltaCounter(HomematicipGenericDevice):
"""Representation of a HomematicIP passage detector delta counter."""
@property
def state(self) -> int:
"""Representation of the HomematicIP passage detector delta counter value."""
return self._device.leftRightCounterDelta
@property
def device_state_attributes(self):
"""Return the state attributes of the delta counter."""
state_attr = super().device_state_attributes
state_attr[ATTR_LEFT_COUNTER] = self._device.leftCounter
state_attr[ATTR_RIGHT_COUNTER] = self._device.rightCounter
return state_attr
2019-04-25 22:13:07 +00:00
def _get_wind_direction(wind_direction_degree: float) -> str:
"""Convert wind direction degree to named direction."""
if 11.25 <= wind_direction_degree < 33.75:
2019-07-31 19:25:30 +00:00
return "NNE"
if 33.75 <= wind_direction_degree < 56.25:
2019-07-31 19:25:30 +00:00
return "NE"
if 56.25 <= wind_direction_degree < 78.75:
2019-07-31 19:25:30 +00:00
return "ENE"
if 78.75 <= wind_direction_degree < 101.25:
2019-07-31 19:25:30 +00:00
return "E"
if 101.25 <= wind_direction_degree < 123.75:
2019-07-31 19:25:30 +00:00
return "ESE"
if 123.75 <= wind_direction_degree < 146.25:
2019-07-31 19:25:30 +00:00
return "SE"
if 146.25 <= wind_direction_degree < 168.75:
2019-07-31 19:25:30 +00:00
return "SSE"
if 168.75 <= wind_direction_degree < 191.25:
2019-07-31 19:25:30 +00:00
return "S"
if 191.25 <= wind_direction_degree < 213.75:
2019-07-31 19:25:30 +00:00
return "SSW"
if 213.75 <= wind_direction_degree < 236.25:
2019-07-31 19:25:30 +00:00
return "SW"
if 236.25 <= wind_direction_degree < 258.75:
2019-07-31 19:25:30 +00:00
return "WSW"
if 258.75 <= wind_direction_degree < 281.25:
2019-07-31 19:25:30 +00:00
return "W"
if 281.25 <= wind_direction_degree < 303.75:
2019-07-31 19:25:30 +00:00
return "WNW"
if 303.75 <= wind_direction_degree < 326.25:
2019-07-31 19:25:30 +00:00
return "NW"
if 326.25 <= wind_direction_degree < 348.75:
2019-07-31 19:25:30 +00:00
return "NNW"
return "N"