|
| 1 | +"""Climate platform for AirPatrol integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from homeassistant.components.climate import ( |
| 8 | + FAN_AUTO, |
| 9 | + FAN_HIGH, |
| 10 | + FAN_LOW, |
| 11 | + SWING_OFF, |
| 12 | + SWING_ON, |
| 13 | + ClimateEntity, |
| 14 | + ClimateEntityFeature, |
| 15 | + HVACMode, |
| 16 | +) |
| 17 | +from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature |
| 18 | +from homeassistant.core import HomeAssistant |
| 19 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 20 | + |
| 21 | +from . import AirPatrolConfigEntry |
| 22 | +from .coordinator import AirPatrolDataUpdateCoordinator |
| 23 | +from .entity import AirPatrolEntity |
| 24 | + |
| 25 | +PARALLEL_UPDATES = 0 |
| 26 | + |
| 27 | +AP_TO_HA_HVAC_MODES = { |
| 28 | + "heat": HVACMode.HEAT, |
| 29 | + "cool": HVACMode.COOL, |
| 30 | + "off": HVACMode.OFF, |
| 31 | +} |
| 32 | +HA_TO_AP_HVAC_MODES = {value: key for key, value in AP_TO_HA_HVAC_MODES.items()} |
| 33 | + |
| 34 | +AP_TO_HA_FAN_MODES = { |
| 35 | + "min": FAN_LOW, |
| 36 | + "max": FAN_HIGH, |
| 37 | + "auto": FAN_AUTO, |
| 38 | +} |
| 39 | +HA_TO_AP_FAN_MODES = {value: key for key, value in AP_TO_HA_FAN_MODES.items()} |
| 40 | + |
| 41 | +AP_TO_HA_SWING_MODES = { |
| 42 | + "on": SWING_ON, |
| 43 | + "off": SWING_OFF, |
| 44 | +} |
| 45 | +HA_TO_AP_SWING_MODES = {value: key for key, value in AP_TO_HA_SWING_MODES.items()} |
| 46 | + |
| 47 | + |
| 48 | +async def async_setup_entry( |
| 49 | + hass: HomeAssistant, |
| 50 | + config_entry: AirPatrolConfigEntry, |
| 51 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 52 | +) -> None: |
| 53 | + """Set up AirPatrol climate entities.""" |
| 54 | + coordinator = config_entry.runtime_data |
| 55 | + units = coordinator.data |
| 56 | + |
| 57 | + async_add_entities( |
| 58 | + AirPatrolClimate(coordinator, unit_id) |
| 59 | + for unit_id, unit in units.items() |
| 60 | + if "climate" in unit |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +class AirPatrolClimate(AirPatrolEntity, ClimateEntity): |
| 65 | + """AirPatrol climate entity.""" |
| 66 | + |
| 67 | + _attr_name = None |
| 68 | + _attr_temperature_unit = UnitOfTemperature.CELSIUS |
| 69 | + _attr_supported_features = ( |
| 70 | + ClimateEntityFeature.TARGET_TEMPERATURE |
| 71 | + | ClimateEntityFeature.FAN_MODE |
| 72 | + | ClimateEntityFeature.SWING_MODE |
| 73 | + | ClimateEntityFeature.TURN_OFF |
| 74 | + | ClimateEntityFeature.TURN_ON |
| 75 | + ) |
| 76 | + _attr_hvac_modes = [HVACMode.HEAT, HVACMode.COOL, HVACMode.OFF] |
| 77 | + _attr_fan_modes = [FAN_LOW, FAN_HIGH, FAN_AUTO] |
| 78 | + _attr_swing_modes = [SWING_ON, SWING_OFF] |
| 79 | + _attr_min_temp = 16.0 |
| 80 | + _attr_max_temp = 30.0 |
| 81 | + |
| 82 | + def __init__( |
| 83 | + self, |
| 84 | + coordinator: AirPatrolDataUpdateCoordinator, |
| 85 | + unit_id: str, |
| 86 | + ) -> None: |
| 87 | + """Initialize the climate entity.""" |
| 88 | + super().__init__(coordinator, unit_id) |
| 89 | + self._attr_unique_id = f"{coordinator.config_entry.unique_id}-{unit_id}" |
| 90 | + |
| 91 | + @property |
| 92 | + def climate_data(self) -> dict[str, Any]: |
| 93 | + """Return the climate data.""" |
| 94 | + return self.device_data.get("climate") or {} |
| 95 | + |
| 96 | + @property |
| 97 | + def params(self) -> dict[str, Any]: |
| 98 | + """Return the current parameters for the climate entity.""" |
| 99 | + return self.climate_data.get("ParametersData") or {} |
| 100 | + |
| 101 | + @property |
| 102 | + def available(self) -> bool: |
| 103 | + """Return if entity is available.""" |
| 104 | + return super().available and bool(self.climate_data) |
| 105 | + |
| 106 | + @property |
| 107 | + def current_humidity(self) -> float | None: |
| 108 | + """Return the current humidity.""" |
| 109 | + if humidity := self.climate_data.get("RoomHumidity"): |
| 110 | + return float(humidity) |
| 111 | + return None |
| 112 | + |
| 113 | + @property |
| 114 | + def current_temperature(self) -> float | None: |
| 115 | + """Return the current temperature.""" |
| 116 | + if temp := self.climate_data.get("RoomTemp"): |
| 117 | + return float(temp) |
| 118 | + return None |
| 119 | + |
| 120 | + @property |
| 121 | + def target_temperature(self) -> float | None: |
| 122 | + """Return the target temperature.""" |
| 123 | + if temp := self.params.get("PumpTemp"): |
| 124 | + return float(temp) |
| 125 | + return None |
| 126 | + |
| 127 | + @property |
| 128 | + def hvac_mode(self) -> HVACMode | None: |
| 129 | + """Return the current HVAC mode.""" |
| 130 | + pump_power = self.params.get("PumpPower") |
| 131 | + pump_mode = self.params.get("PumpMode") |
| 132 | + |
| 133 | + if pump_power and pump_power == "on" and pump_mode: |
| 134 | + return AP_TO_HA_HVAC_MODES.get(pump_mode) |
| 135 | + return HVACMode.OFF |
| 136 | + |
| 137 | + @property |
| 138 | + def fan_mode(self) -> str | None: |
| 139 | + """Return the current fan mode.""" |
| 140 | + fan_speed = self.params.get("FanSpeed") |
| 141 | + if fan_speed: |
| 142 | + return AP_TO_HA_FAN_MODES.get(fan_speed) |
| 143 | + return None |
| 144 | + |
| 145 | + @property |
| 146 | + def swing_mode(self) -> str | None: |
| 147 | + """Return the current swing mode.""" |
| 148 | + swing = self.params.get("Swing") |
| 149 | + if swing: |
| 150 | + return AP_TO_HA_SWING_MODES.get(swing) |
| 151 | + return None |
| 152 | + |
| 153 | + async def async_set_temperature(self, **kwargs: Any) -> None: |
| 154 | + """Set new target temperature.""" |
| 155 | + params = self.params.copy() |
| 156 | + |
| 157 | + if ATTR_TEMPERATURE in kwargs: |
| 158 | + temp = kwargs[ATTR_TEMPERATURE] |
| 159 | + params["PumpTemp"] = f"{temp:.3f}" |
| 160 | + |
| 161 | + await self._async_set_params(params) |
| 162 | + |
| 163 | + async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: |
| 164 | + """Set new target hvac mode.""" |
| 165 | + params = self.params.copy() |
| 166 | + |
| 167 | + if hvac_mode == HVACMode.OFF: |
| 168 | + params["PumpPower"] = "off" |
| 169 | + else: |
| 170 | + params["PumpPower"] = "on" |
| 171 | + params["PumpMode"] = HA_TO_AP_HVAC_MODES.get(hvac_mode) |
| 172 | + |
| 173 | + await self._async_set_params(params) |
| 174 | + |
| 175 | + async def async_set_fan_mode(self, fan_mode: str) -> None: |
| 176 | + """Set new target fan mode.""" |
| 177 | + params = self.params.copy() |
| 178 | + params["FanSpeed"] = HA_TO_AP_FAN_MODES.get(fan_mode) |
| 179 | + |
| 180 | + await self._async_set_params(params) |
| 181 | + |
| 182 | + async def async_set_swing_mode(self, swing_mode: str) -> None: |
| 183 | + """Set new target swing mode.""" |
| 184 | + params = self.params.copy() |
| 185 | + params["Swing"] = HA_TO_AP_SWING_MODES.get(swing_mode) |
| 186 | + |
| 187 | + await self._async_set_params(params) |
| 188 | + |
| 189 | + async def async_turn_on(self) -> None: |
| 190 | + """Turn the entity on.""" |
| 191 | + params = self.params.copy() |
| 192 | + if mode := AP_TO_HA_HVAC_MODES.get(params["PumpMode"]): |
| 193 | + await self.async_set_hvac_mode(mode) |
| 194 | + |
| 195 | + async def async_turn_off(self) -> None: |
| 196 | + """Turn the entity off.""" |
| 197 | + await self.async_set_hvac_mode(HVACMode.OFF) |
| 198 | + |
| 199 | + async def _async_set_params(self, params: dict[str, Any]) -> None: |
| 200 | + """Set the unit to dry mode.""" |
| 201 | + new_climate_data = self.climate_data.copy() |
| 202 | + new_climate_data["ParametersData"] = params |
| 203 | + |
| 204 | + await self.coordinator.api.set_unit_climate_data( |
| 205 | + self._unit_id, new_climate_data |
| 206 | + ) |
| 207 | + |
| 208 | + await self.coordinator.async_request_refresh() |
0 commit comments