Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Wolf SmartSet Service integration."""
2 
3 from datetime import timedelta
4 import logging
5 
6 from httpx import RequestError
7 from wolf_comm.token_auth import InvalidAuth
8 from wolf_comm.wolf_client import FetchFailed, ParameterReadError, WolfClient
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryNotReady
14 from homeassistant.helpers import device_registry as dr
15 from homeassistant.helpers.httpx_client import get_async_client
16 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
17 
18 from .const import (
19  COORDINATOR,
20  DEVICE_GATEWAY,
21  DEVICE_ID,
22  DEVICE_NAME,
23  DOMAIN,
24  PARAMETERS,
25 )
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 PLATFORMS = [Platform.SENSOR]
30 
31 
32 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
33  """Set up Wolf SmartSet Service from a config entry."""
34 
35  username = entry.data[CONF_USERNAME]
36  password = entry.data[CONF_PASSWORD]
37  device_name = entry.data[DEVICE_NAME]
38  device_id = entry.data[DEVICE_ID]
39  gateway_id = entry.data[DEVICE_GATEWAY]
40  refetch_parameters = False
41  _LOGGER.debug(
42  "Setting up wolflink integration for device: %s (ID: %s, gateway: %s)",
43  device_name,
44  device_id,
45  gateway_id,
46  )
47 
48  wolf_client = WolfClient(
49  username,
50  password,
51  client=get_async_client(hass=hass, verify_ssl=False),
52  )
53 
54  parameters = await fetch_parameters_init(wolf_client, gateway_id, device_id)
55 
56  async def async_update_data():
57  """Update all stored entities for Wolf SmartSet."""
58  try:
59  nonlocal refetch_parameters
60  nonlocal parameters
61  if not await wolf_client.fetch_system_state_list(device_id, gateway_id):
62  refetch_parameters = True
63  raise UpdateFailed(
64  "Could not fetch values from server because device is Offline."
65  )
66  if refetch_parameters:
67  parameters = await fetch_parameters(wolf_client, gateway_id, device_id)
68  hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters
69  refetch_parameters = False
70  values = {
71  v.value_id: v.value
72  for v in await wolf_client.fetch_value(
73  gateway_id, device_id, parameters
74  )
75  }
76  return {
77  parameter.parameter_id: (
78  parameter.value_id,
79  values[parameter.value_id],
80  )
81  for parameter in parameters
82  if parameter.value_id in values
83  }
84  except RequestError as exception:
85  raise UpdateFailed(
86  f"Error communicating with API: {exception}"
87  ) from exception
88  except FetchFailed as exception:
89  raise UpdateFailed(
90  f"Could not fetch values from server due to: {exception}"
91  ) from exception
92  except ParameterReadError as exception:
93  refetch_parameters = True
94  raise UpdateFailed(
95  "Could not fetch values for parameter. Refreshing value IDs."
96  ) from exception
97  except InvalidAuth as exception:
98  raise UpdateFailed("Invalid authentication during update.") from exception
99 
100  coordinator = DataUpdateCoordinator(
101  hass,
102  _LOGGER,
103  config_entry=entry,
104  name=DOMAIN,
105  update_method=async_update_data,
106  update_interval=timedelta(seconds=60),
107  )
108 
109  await coordinator.async_refresh()
110 
111  hass.data.setdefault(DOMAIN, {})
112  hass.data[DOMAIN][entry.entry_id] = {}
113  hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters
114  hass.data[DOMAIN][entry.entry_id][COORDINATOR] = coordinator
115  hass.data[DOMAIN][entry.entry_id][DEVICE_ID] = device_id
116 
117  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
118 
119  return True
120 
121 
122 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
123  """Unload a config entry."""
124  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
125  if unload_ok:
126  hass.data[DOMAIN].pop(entry.entry_id)
127 
128  return unload_ok
129 
130 
131 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
132  """Migrate old entry."""
133  # convert unique_id to string
134  if entry.version == 1 and entry.minor_version == 1:
135  if isinstance(entry.unique_id, int):
136  hass.config_entries.async_update_entry(
137  entry, unique_id=str(entry.unique_id)
138  )
139  device_registry = dr.async_get(hass)
140  for device in dr.async_entries_for_config_entry(
141  device_registry, entry.entry_id
142  ):
143  new_identifiers = set()
144  for identifier in device.identifiers:
145  if identifier[0] == DOMAIN:
146  new_identifiers.add((DOMAIN, str(identifier[1])))
147  else:
148  new_identifiers.add(identifier)
149  device_registry.async_update_device(
150  device.id, new_identifiers=new_identifiers
151  )
152  hass.config_entries.async_update_entry(entry, minor_version=2)
153 
154  return True
155 
156 
157 async def fetch_parameters(client: WolfClient, gateway_id: int, device_id: int):
158  """Fetch all available parameters with usage of WolfClient.
159 
160  By default Reglertyp entity is removed because API will not provide value for this parameter.
161  """
162  fetched_parameters = await client.fetch_parameters(gateway_id, device_id)
163  return [param for param in fetched_parameters if param.name != "Reglertyp"]
164 
165 
166 async def fetch_parameters_init(client: WolfClient, gateway_id: int, device_id: int):
167  """Fetch all available parameters with usage of WolfClient but handles all exceptions and results in ConfigEntryNotReady."""
168  try:
169  return await fetch_parameters(client, gateway_id, device_id)
170  except (FetchFailed, RequestError) as exception:
171  raise ConfigEntryNotReady(
172  f"Error communicating with API: {exception}"
173  ) from exception
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)
Definition: httpx_client.py:41