This repository was archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclimate.py
More file actions
131 lines (105 loc) · 4.32 KB
/
climate.py
File metadata and controls
131 lines (105 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Climate entities for MySkoda."""
from asyncio import sleep
import logging
from homeassistant.components.climate import (
ClimateEntity,
ClimateEntityDescription,
ClimateEntityFeature,
HVACAction,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DATA_COODINATOR, DOMAIN
from .entity import MySkodaDataEntity
from .myskoda import Vehicle
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
coordinator = hass.data[DOMAIN][config.entry_id][DATA_COODINATOR]
vehicles = coordinator.data.get("vehicles")
entities = [MySkodaClimate(coordinator, vehicle) for vehicle in vehicles]
async_add_entities(entities, update_before_add=True)
class MySkodaClimate(MySkodaDataEntity, ClimateEntity):
"""Climate control for MySkoda vehicles."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
ClimateEntityDescription(
key="climate",
name=f"{vehicle.info.title} Air Conditioning",
icon="mdi:air-conditioner",
),
)
ClimateEntity.__init__(self)
self._attr_temperature_unit = UnitOfTemperature.CELSIUS
self._attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TURN_ON
| ClimateEntityFeature.TURN_OFF
)
self._attr_unique_id = f"{self.vehicle.info.vin}_climate"
@property
def hvac_modes(self) -> list[HVACMode]: # noqa: D102
return [HVACMode.AUTO, HVACMode.OFF]
@property
def hvac_mode(self) -> HVACMode | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
if self.vehicle.air_conditioning.air_conditioning_on:
return HVACMode.AUTO
return HVACMode.OFF
@property
def hvac_action(self) -> HVACAction | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
if self.vehicle.air_conditioning.state == "HEATING":
return HVACAction.HEATING
if self.vehicle.air_conditioning.state == "COOLING":
return HVACAction.COOLING
return HVACAction.OFF
@property
def target_temperature(self) -> None | float: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.air_conditioning.target_temperature_celsius
async def async_set_hvac_mode(self, hvac_mode: HVACMode): # noqa: D102
if hvac_mode == HVACMode.AUTO:
await self.coordinator.hub.start_air_conditioning(
self.vehicle.info.vin,
self.vehicle.air_conditioning.target_temperature_celsius,
)
else:
await self.coordinator.hub.stop_air_conditioning(self.vehicle.info.vin)
for _ in range(10):
await sleep(15)
if self.hvac_mode == hvac_mode:
break
await self.coordinator.async_refresh()
_LOGGER.info("HVAC mode set to %s.", hvac_mode)
async def async_turn_on(self): # noqa: D102
await self.async_set_hvac_mode(HVACMode.AUTO)
async def async_turn_off(self): # noqa: D102
await self.async_set_hvac_mode(HVACMode.OFF)
async def async_set_temperature(self, **kwargs): # noqa: D102
temp = kwargs[ATTR_TEMPERATURE]
await self.coordinator.hub.set_target_temperature(self.vehicle.info.vin, temp)
for _ in range(10):
await sleep(15)
if self.target_temperature == temp:
break
await self.coordinator.async_refresh()
_LOGGER.info("AC disabled.")