core/homeassistant/components/somfy/climate.py

178 lines
5.7 KiB
Python
Raw Normal View History

"""Support for Somfy Thermostat."""
2021-03-18 13:31:38 +00:00
from __future__ import annotations
from pymfy.api.devices.category import Category
from pymfy.api.devices.thermostat import (
DurationType,
HvacState,
RegulationState,
TargetMode,
Thermostat,
)
2022-04-26 06:44:13 +00:00
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
PRESET_AWAY,
PRESET_HOME,
PRESET_SLEEP,
2022-04-26 06:44:13 +00:00
ClimateEntityFeature,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import COORDINATOR, DOMAIN
from .entity import SomfyEntity
SUPPORTED_CATEGORIES = {Category.HVAC.value}
PRESET_FROST_GUARD = "Frost Guard"
PRESET_GEOFENCING = "Geofencing"
PRESET_MANUAL = "Manual"
PRESETS_MAPPING = {
TargetMode.AT_HOME: PRESET_HOME,
TargetMode.AWAY: PRESET_AWAY,
TargetMode.SLEEP: PRESET_SLEEP,
TargetMode.MANUAL: PRESET_MANUAL,
TargetMode.GEOFENCING: PRESET_GEOFENCING,
TargetMode.FROST_PROTECTION: PRESET_FROST_GUARD,
}
REVERSE_PRESET_MAPPING = {v: k for k, v in PRESETS_MAPPING.items()}
2022-04-26 06:44:13 +00:00
HVAC_MODES_MAPPING = {HvacState.COOL: HVACMode.COOL, HvacState.HEAT: HVACMode.HEAT}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Somfy climate platform."""
domain_data = hass.data[DOMAIN]
coordinator = domain_data[COORDINATOR]
climates = [
SomfyClimate(coordinator, device_id)
for device_id, device in coordinator.data.items()
if SUPPORTED_CATEGORIES & set(device.categories)
]
async_add_entities(climates)
class SomfyClimate(SomfyEntity, ClimateEntity):
"""Representation of a Somfy thermostat device."""
_attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
)
def __init__(self, coordinator, device_id):
"""Initialize the Somfy device."""
super().__init__(coordinator, device_id)
self._climate = None
self._create_device()
def _create_device(self):
"""Update the device with the latest data."""
self._climate = Thermostat(self.device, self.coordinator.client)
@property
def temperature_unit(self):
"""Return the unit of measurement used by the platform."""
return TEMP_CELSIUS
@property
def current_temperature(self):
"""Return the current temperature."""
return self._climate.get_ambient_temperature()
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._climate.get_target_temperature()
def set_temperature(self, **kwargs) -> None:
"""Set new target temperature."""
2021-10-20 15:47:46 +00:00
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
return
self._climate.set_target(TargetMode.MANUAL, temperature, DurationType.NEXT_MODE)
@property
def max_temp(self) -> float:
"""Return the maximum temperature."""
return 26.0
@property
def min_temp(self) -> float:
"""Return the minimum temperature."""
return 15.0
@property
def current_humidity(self):
"""Return the current humidity."""
return self._climate.get_humidity()
@property
2022-04-26 06:44:13 +00:00
def hvac_mode(self) -> HVACMode:
"""Return hvac operation ie. heat, cool mode."""
if self._climate.get_regulation_state() == RegulationState.TIMETABLE:
2022-04-26 06:44:13 +00:00
return HVACMode.AUTO
return HVAC_MODES_MAPPING[self._climate.get_hvac_state()]
@property
2022-04-26 06:44:13 +00:00
def hvac_modes(self) -> list[HVACMode]:
"""Return the list of available hvac operation modes.
HEAT and COOL mode are exclusive. End user has to enable a mode manually within the Somfy application.
So only one mode can be displayed. Auto mode is a scheduler.
"""
hvac_state = HVAC_MODES_MAPPING[self._climate.get_hvac_state()]
2022-04-26 06:44:13 +00:00
return [HVACMode.AUTO, hvac_state]
2022-04-26 06:44:13 +00:00
def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode."""
2022-04-26 06:44:13 +00:00
if hvac_mode == HVACMode.AUTO:
self._climate.cancel_target()
else:
self._climate.set_target(
TargetMode.MANUAL, self.target_temperature, DurationType.FURTHER_NOTICE
)
@property
2021-03-18 13:31:38 +00:00
def preset_mode(self) -> str | None:
"""Return the current preset mode."""
mode = self._climate.get_target_mode()
return PRESETS_MAPPING.get(mode)
@property
2021-03-18 13:31:38 +00:00
def preset_modes(self) -> list[str] | None:
"""Return a list of available preset modes."""
return list(PRESETS_MAPPING.values())
def set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
if self.preset_mode == preset_mode:
return
if preset_mode == PRESET_HOME:
temperature = self._climate.get_at_home_temperature()
elif preset_mode == PRESET_AWAY:
temperature = self._climate.get_away_temperature()
elif preset_mode == PRESET_SLEEP:
temperature = self._climate.get_night_temperature()
elif preset_mode == PRESET_FROST_GUARD:
temperature = self._climate.get_frost_protection_temperature()
2021-07-29 23:20:03 +00:00
elif preset_mode in (PRESET_MANUAL, PRESET_GEOFENCING):
temperature = self.target_temperature
else:
raise ValueError(f"Preset mode not supported: {preset_mode}")
self._climate.set_target(
REVERSE_PRESET_MAPPING[preset_mode], temperature, DurationType.NEXT_MODE
)