1 """DataUpdateCoordinator for the wallbox integration."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from datetime
import timedelta
7 from http
import HTTPStatus
9 from typing
import Any, Concatenate
12 from wallbox
import Wallbox
21 CHARGER_ENERGY_PRICE_KEY,
23 CHARGER_LOCKED_UNLOCKED_KEY,
24 CHARGER_MAX_CHARGING_CURRENT_KEY,
25 CHARGER_MAX_ICP_CURRENT_KEY,
27 CHARGER_POWER_BOOST_KEY,
28 CHARGER_STATUS_DESCRIPTION_KEY,
29 CHARGER_STATUS_ID_KEY,
36 _LOGGER = logging.getLogger(__name__)
40 CHARGER_STATUS: dict[int, ChargerStatus] = {
41 0: ChargerStatus.DISCONNECTED,
42 14: ChargerStatus.ERROR,
43 15: ChargerStatus.ERROR,
44 161: ChargerStatus.READY,
45 162: ChargerStatus.READY,
46 163: ChargerStatus.DISCONNECTED,
47 164: ChargerStatus.WAITING,
48 165: ChargerStatus.LOCKED,
49 166: ChargerStatus.UPDATING,
50 177: ChargerStatus.SCHEDULED,
51 178: ChargerStatus.PAUSED,
52 179: ChargerStatus.SCHEDULED,
53 180: ChargerStatus.WAITING_FOR_CAR,
54 181: ChargerStatus.WAITING_FOR_CAR,
55 182: ChargerStatus.PAUSED,
56 183: ChargerStatus.WAITING_IN_QUEUE_POWER_SHARING,
57 184: ChargerStatus.WAITING_IN_QUEUE_POWER_SHARING,
58 185: ChargerStatus.WAITING_IN_QUEUE_POWER_BOOST,
59 186: ChargerStatus.WAITING_IN_QUEUE_POWER_BOOST,
60 187: ChargerStatus.WAITING_MID_FAILED,
61 188: ChargerStatus.WAITING_MID_SAFETY,
62 189: ChargerStatus.WAITING_IN_QUEUE_ECO_SMART,
63 193: ChargerStatus.CHARGING,
64 194: ChargerStatus.CHARGING,
65 195: ChargerStatus.CHARGING,
66 196: ChargerStatus.DISCHARGING,
67 209: ChargerStatus.LOCKED,
68 210: ChargerStatus.LOCKED_CAR_CONNECTED,
72 def _require_authentication[_WallboxCoordinatorT: WallboxCoordinator, **_P](
73 func: Callable[Concatenate[_WallboxCoordinatorT, _P], Any],
74 ) -> Callable[Concatenate[_WallboxCoordinatorT, _P], Any]:
75 """Authenticate with decorator using Wallbox API."""
77 def require_authentication(
78 self: _WallboxCoordinatorT, *args: _P.args, **kwargs: _P.kwargs
80 """Authenticate using Wallbox API."""
83 return func(self, *args, **kwargs)
84 except requests.exceptions.HTTPError
as wallbox_connection_error:
85 if wallbox_connection_error.response.status_code == HTTPStatus.FORBIDDEN:
86 raise ConfigEntryAuthFailed
from wallbox_connection_error
87 raise ConnectionError
from wallbox_connection_error
89 return require_authentication
93 """Authenticate using Wallbox API."""
95 wallbox.authenticate()
96 except requests.exceptions.HTTPError
as wallbox_connection_error:
97 if wallbox_connection_error.response.status_code == 403:
98 raise InvalidAuth
from wallbox_connection_error
99 raise ConnectionError
from wallbox_connection_error
103 """Get new sensor data for Wallbox component."""
104 await hass.async_add_executor_job(_validate, wallbox)
108 """Wallbox Coordinator class."""
110 def __init__(self, station: str, wallbox: Wallbox, hass: HomeAssistant) ->
None:
119 update_interval=
timedelta(seconds=UPDATE_INTERVAL),
123 """Authenticate using Wallbox API."""
126 @_require_authentication
128 """Get new sensor data for Wallbox component."""
129 data: dict[str, Any] = self.
_wallbox_wallbox.getChargerStatus(self.
_station_station)
130 data[CHARGER_MAX_CHARGING_CURRENT_KEY] = data[CHARGER_DATA_KEY][
131 CHARGER_MAX_CHARGING_CURRENT_KEY
133 data[CHARGER_LOCKED_UNLOCKED_KEY] = data[CHARGER_DATA_KEY][
134 CHARGER_LOCKED_UNLOCKED_KEY
136 data[CHARGER_ENERGY_PRICE_KEY] = data[CHARGER_DATA_KEY][
137 CHARGER_ENERGY_PRICE_KEY
141 data[CHARGER_DATA_KEY].
get(CHARGER_MAX_ICP_CURRENT_KEY, 0) > 0
142 and CHARGER_POWER_BOOST_KEY
143 in data[CHARGER_DATA_KEY][CHARGER_PLAN_KEY][CHARGER_FEATURES_KEY]
145 data[CHARGER_MAX_ICP_CURRENT_KEY] = data[CHARGER_DATA_KEY][
146 CHARGER_MAX_ICP_CURRENT_KEY
149 data[CHARGER_CURRENCY_KEY] = (
150 f
"{data[CHARGER_DATA_KEY][CHARGER_CURRENCY_KEY][CODE_KEY]}/kWh"
153 data[CHARGER_STATUS_DESCRIPTION_KEY] = CHARGER_STATUS.get(
154 data[CHARGER_STATUS_ID_KEY], ChargerStatus.UNKNOWN
159 """Get new sensor data for Wallbox component."""
160 return await self.
hasshass.async_add_executor_job(self.
_get_data_get_data)
162 @_require_authentication
164 """Set maximum charging current for Wallbox."""
166 self.
_wallbox_wallbox.setMaxChargingCurrent(self.
_station_station, charging_current)
167 except requests.exceptions.HTTPError
as wallbox_connection_error:
168 if wallbox_connection_error.response.status_code == 403:
169 raise InvalidAuth
from wallbox_connection_error
173 """Set maximum charging current for Wallbox."""
174 await self.
hasshass.async_add_executor_job(
179 @_require_authentication
181 """Set maximum icp current for Wallbox."""
184 except requests.exceptions.HTTPError
as wallbox_connection_error:
185 if wallbox_connection_error.response.status_code == 403:
186 raise InvalidAuth
from wallbox_connection_error
190 """Set maximum icp current for Wallbox."""
194 @_require_authentication
196 """Set energy cost for Wallbox."""
201 """Set energy cost for Wallbox."""
205 @_require_authentication
207 """Set wallbox to locked or unlocked."""
213 except requests.exceptions.HTTPError
as wallbox_connection_error:
214 if wallbox_connection_error.response.status_code == 403:
215 raise InvalidAuth
from wallbox_connection_error
219 """Set wallbox to locked or unlocked."""
223 @_require_authentication
225 """Set wallbox to pause or resume."""
233 """Set wallbox to pause or resume."""
239 """Error to indicate there is invalid auth."""
None async_set_icp_current(self, float icp_current)
None async_set_lock_unlock(self, bool lock)
None _set_energy_cost(self, float energy_cost)
None async_set_charging_current(self, float charging_current)
None async_set_energy_cost(self, float energy_cost)
None _pause_charger(self, bool pause)
dict[str, Any] _async_update_data(self)
None _set_lock_unlock(self, bool lock)
dict[str, Any] _get_data(self)
None async_pause_charger(self, bool pause)
None _set_icp_current(self, float icp_current)
None _set_charging_current(self, float charging_current)
None __init__(self, str station, Wallbox wallbox, HomeAssistant hass)
None async_request_refresh(self)
web.Response get(self, web.Request request, str config_key)
None _validate(Wallbox wallbox)
None async_validate_input(HomeAssistant hass, Wallbox wallbox)