1 """The Wolf SmartSet Service integration."""
3 from datetime
import timedelta
6 from httpx
import RequestError
7 from wolf_comm.token_auth
import InvalidAuth
8 from wolf_comm.wolf_client
import FetchFailed, ParameterReadError, WolfClient
27 _LOGGER = logging.getLogger(__name__)
29 PLATFORMS = [Platform.SENSOR]
33 """Set up Wolf SmartSet Service from a config entry."""
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
42 "Setting up wolflink integration for device: %s (ID: %s, gateway: %s)",
48 wolf_client = WolfClient(
56 async
def async_update_data():
57 """Update all stored entities for Wolf SmartSet."""
59 nonlocal refetch_parameters
61 if not await wolf_client.fetch_system_state_list(device_id, gateway_id):
62 refetch_parameters =
True
64 "Could not fetch values from server because device is Offline."
66 if refetch_parameters:
68 hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters
69 refetch_parameters =
False
72 for v
in await wolf_client.fetch_value(
73 gateway_id, device_id, parameters
77 parameter.parameter_id: (
79 values[parameter.value_id],
81 for parameter
in parameters
82 if parameter.value_id
in values
84 except RequestError
as exception:
86 f
"Error communicating with API: {exception}"
88 except FetchFailed
as exception:
90 f
"Could not fetch values from server due to: {exception}"
92 except ParameterReadError
as exception:
93 refetch_parameters =
True
95 "Could not fetch values for parameter. Refreshing value IDs."
97 except InvalidAuth
as exception:
98 raise UpdateFailed(
"Invalid authentication during update.")
from exception
105 update_method=async_update_data,
109 await coordinator.async_refresh()
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
117 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
123 """Unload a config entry."""
124 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
126 hass.data[DOMAIN].pop(entry.entry_id)
132 """Migrate old entry."""
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)
139 device_registry = dr.async_get(hass)
140 for device
in dr.async_entries_for_config_entry(
141 device_registry, entry.entry_id
143 new_identifiers = set()
144 for identifier
in device.identifiers:
145 if identifier[0] == DOMAIN:
146 new_identifiers.add((DOMAIN,
str(identifier[1])))
148 new_identifiers.add(identifier)
149 device_registry.async_update_device(
150 device.id, new_identifiers=new_identifiers
152 hass.config_entries.async_update_entry(entry, minor_version=2)
158 """Fetch all available parameters with usage of WolfClient.
160 By default Reglertyp entity is removed because API will not provide value for this parameter.
162 fetched_parameters = await client.fetch_parameters(gateway_id, device_id)
163 return [param
for param
in fetched_parameters
if param.name !=
"Reglertyp"]
167 """Fetch all available parameters with usage of WolfClient but handles all exceptions and results in ConfigEntryNotReady."""
170 except (FetchFailed, RequestError)
as exception:
172 f
"Error communicating with API: {exception}"
def fetch_parameters(WolfClient client, int gateway_id, int device_id)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
def fetch_parameters_init(WolfClient client, int gateway_id, int device_id)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)