Home Assistant Unofficial Reference 2024.12.1
utils.py
Go to the documentation of this file.
1 """ViCare helpers functions."""
2 
3 import logging
4 
5 from PyViCare.PyViCareDevice import Device as PyViCareDevice
6 from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
7 from PyViCare.PyViCareHeatingDevice import (
8  HeatingDeviceWithComponent as PyViCareHeatingDeviceComponent,
9 )
10 from PyViCare.PyViCareUtils import (
11  PyViCareInvalidDataError,
12  PyViCareNotSupportedFeatureError,
13  PyViCareRateLimitError,
14 )
15 import requests
16 
17 from homeassistant.config_entries import ConfigEntry
18 
19 from .const import CONF_HEATING_TYPE, HEATING_TYPE_TO_CREATOR_METHOD, HeatingType
20 from .types import ViCareRequiredKeysMixin
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
26  entry: ConfigEntry, device_config: PyViCareDeviceConfig
27 ) -> PyViCareDevice:
28  """Get device for device config."""
29  return getattr(
30  device_config,
31  HEATING_TYPE_TO_CREATOR_METHOD[HeatingType(entry.data[CONF_HEATING_TYPE])],
32  )()
33 
34 
35 def get_device_serial(device: PyViCareDevice) -> str | None:
36  """Get device serial for device if supported."""
37  try:
38  return device.getSerial()
39  except PyViCareNotSupportedFeatureError:
40  _LOGGER.debug("Device does not offer a 'device.serial' data point")
41  except PyViCareRateLimitError as limit_exception:
42  _LOGGER.debug("Vicare API rate limit exceeded: %s", limit_exception)
43  except PyViCareInvalidDataError as invalid_data_exception:
44  _LOGGER.debug("Invalid data from Vicare server: %s", invalid_data_exception)
45  except requests.exceptions.ConnectionError:
46  _LOGGER.debug("Unable to retrieve data from ViCare server")
47  except ValueError:
48  _LOGGER.debug("Unable to decode data from ViCare server")
49  return None
50 
51 
53  name: str,
54  entity_description: ViCareRequiredKeysMixin,
55  vicare_device,
56 ) -> bool:
57  """Check if the PyViCare device supports the requested sensor."""
58  try:
59  entity_description.value_getter(vicare_device)
60  except PyViCareNotSupportedFeatureError:
61  _LOGGER.debug("Feature not supported %s", name)
62  return False
63  except AttributeError as error:
64  _LOGGER.debug("Feature not supported %s: %s", name, error)
65  return False
66  _LOGGER.debug("Found entity %s", name)
67  return True
68 
69 
70 def get_burners(device: PyViCareDevice) -> list[PyViCareHeatingDeviceComponent]:
71  """Return the list of burners."""
72  try:
73  return device.burners
74  except PyViCareNotSupportedFeatureError:
75  _LOGGER.debug("No burners found")
76  except AttributeError as error:
77  _LOGGER.debug("No burners found: %s", error)
78  return []
79 
80 
81 def get_circuits(device: PyViCareDevice) -> list[PyViCareHeatingDeviceComponent]:
82  """Return the list of circuits."""
83  try:
84  return device.circuits
85  except PyViCareNotSupportedFeatureError:
86  _LOGGER.debug("No circuits found")
87  except AttributeError as error:
88  _LOGGER.debug("No circuits found: %s", error)
89  return []
90 
91 
92 def get_compressors(device: PyViCareDevice) -> list[PyViCareHeatingDeviceComponent]:
93  """Return the list of compressors."""
94  try:
95  return device.compressors
96  except PyViCareNotSupportedFeatureError:
97  _LOGGER.debug("No compressors found")
98  except AttributeError as error:
99  _LOGGER.debug("No compressors found: %s", error)
100  return []
str|None get_device_serial(PyViCareDevice device)
Definition: utils.py:35
list[PyViCareHeatingDeviceComponent] get_compressors(PyViCareDevice device)
Definition: utils.py:92
PyViCareDevice get_device(ConfigEntry entry, PyViCareDeviceConfig device_config)
Definition: utils.py:27
bool is_supported(str name, ViCareRequiredKeysMixin entity_description, vicare_device)
Definition: utils.py:56
list[PyViCareHeatingDeviceComponent] get_circuits(PyViCareDevice device)
Definition: utils.py:81
list[PyViCareHeatingDeviceComponent] get_burners(PyViCareDevice device)
Definition: utils.py:70