Home Assistant Unofficial Reference 2024.12.1
data.py
Go to the documentation of this file.
1 """The radiotherm component data."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any
7 
8 import radiotherm
9 from radiotherm.thermostat import CommonThermostat
10 
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import device_registry as dr
13 
14 from .const import TIMEOUT
15 
16 
17 @dataclass
19  """An update from a radiotherm device."""
20 
21  tstat: dict[str, Any]
22  humidity: int | None
23 
24 
25 @dataclass
27  """An data needed to init the integration."""
28 
29  tstat: CommonThermostat
30  host: str
31  name: str
32  mac: str
33  model: str | None
34  fw_version: str | None
35  api_version: int | None
36 
37 
38 def _get_init_data(host: str) -> RadioThermInitData:
39  tstat = radiotherm.get_thermostat(host)
40  tstat.timeout = TIMEOUT
41  name: str = tstat.name["raw"]
42  sys: dict[str, Any] = tstat.sys["raw"]
43  mac: str = dr.format_mac(sys["uuid"])
44  model: str = tstat.model.get("raw")
45  return RadioThermInitData(
46  tstat, host, name, mac, model, sys.get("fw_version"), sys.get("api_version")
47  )
48 
49 
50 async def async_get_init_data(hass: HomeAssistant, host: str) -> RadioThermInitData:
51  """Get the RadioInitData."""
52  return await hass.async_add_executor_job(_get_init_data, host)
53 
54 
55 def _get_data(device: CommonThermostat) -> RadioThermUpdate:
56  # Request the current state from the thermostat.
57  # Radio thermostats are very slow, and sometimes don't respond
58  # very quickly. So we need to keep the number of calls to them
59  # to a bare minimum or we'll hit the Home Assistant 10 sec warning. We
60  # have to make one call to /tstat to get temps but we'll try and
61  # keep the other calls to a minimum. Even with this, these
62  # thermostats tend to time out sometimes when they're actively
63  # heating or cooling.
64  tstat: dict[str, Any] = device.tstat["raw"]
65  humidity: int | None = None
66  if isinstance(device, radiotherm.thermostat.CT80):
67  humidity = device.humidity["raw"]
68  return RadioThermUpdate(tstat, humidity)
69 
70 
71 async def async_get_data(
72  hass: HomeAssistant, device: CommonThermostat
73 ) -> RadioThermUpdate:
74  """Fetch the data from the thermostat."""
75  return await hass.async_add_executor_job(_get_data, device)
RadioThermInitData _get_init_data(str host)
Definition: data.py:38
RadioThermInitData async_get_init_data(HomeAssistant hass, str host)
Definition: data.py:50
RadioThermUpdate _get_data(CommonThermostat device)
Definition: data.py:55
RadioThermUpdate async_get_data(HomeAssistant hass, CommonThermostat device)
Definition: data.py:73