Home Assistant Unofficial Reference 2024.12.1
types.py
Go to the documentation of this file.
1 """Types for the ViCare integration."""
2 
3 from collections.abc import Callable
4 from contextlib import suppress
5 from dataclasses import dataclass
6 import enum
7 from typing import Any
8 
9 from PyViCare.PyViCareDevice import Device as PyViCareDevice
10 from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
11 
13  PRESET_COMFORT,
14  PRESET_ECO,
15  PRESET_HOME,
16  PRESET_SLEEP,
17 )
18 
19 
20 class HeatingProgram(enum.StrEnum):
21  """ViCare preset heating programs.
22 
23  As listed in https://github.com/somm15/PyViCare/blob/63f9f7fea505fdf9a26c77c6cd0bff889abcdb05/PyViCare/PyViCareHeatingDevice.py#L606
24  """
25 
26  COMFORT = "comfort"
27  COMFORT_HEATING = "comfortHeating"
28  COMFORT_COOLING = "comfortCooling"
29  ECO = "eco"
30  NORMAL = "normal"
31  NORMAL_HEATING = "normalHeating"
32  NORMAL_COOLING = "normalCooling"
33  REDUCED = "reduced"
34  REDUCED_HEATING = "reducedHeating"
35  REDUCED_COOLING = "reducedCooling"
36  STANDBY = "standby"
37 
38  @staticmethod
39  def to_ha_preset(program: str) -> str | None:
40  """Return the mapped Home Assistant preset for the ViCare heating program."""
41 
42  try:
43  heating_program = HeatingProgram(program)
44  except ValueError:
45  # ignore unsupported / unmapped programs
46  return None
47  return VICARE_TO_HA_PRESET_HEATING.get(heating_program) if program else None
48 
49  @staticmethod
51  ha_preset: str, supported_heating_programs: list[str]
52  ) -> str | None:
53  """Return the mapped ViCare heating program for the Home Assistant preset."""
54  for program in supported_heating_programs:
55  with suppress(ValueError):
56  if (
57  VICARE_TO_HA_PRESET_HEATING.get(HeatingProgram(program))
58  == ha_preset
59  ):
60  return program
61  return None
62 
63 
64 VICARE_TO_HA_PRESET_HEATING = {
65  HeatingProgram.COMFORT: PRESET_COMFORT,
66  HeatingProgram.COMFORT_HEATING: PRESET_COMFORT,
67  HeatingProgram.ECO: PRESET_ECO,
68  HeatingProgram.NORMAL: PRESET_HOME,
69  HeatingProgram.NORMAL_HEATING: PRESET_HOME,
70  HeatingProgram.REDUCED: PRESET_SLEEP,
71  HeatingProgram.REDUCED_HEATING: PRESET_SLEEP,
72 }
73 
74 
75 @dataclass(frozen=True)
77  """Dataclass holding the device api and config."""
78 
79  config: PyViCareDeviceConfig
80  api: PyViCareDevice
81 
82 
83 @dataclass(frozen=True)
85  """Mixin for required keys."""
86 
87  value_getter: Callable[[PyViCareDevice], Any]
88 
89 
90 @dataclass(frozen=True)
92  """Mixin for required keys with setter."""
93 
94  value_setter: Callable[[PyViCareDevice], bool]
str|None from_ha_preset(str ha_preset, list[str] supported_heating_programs)
Definition: types.py:52