Skip to content

Commit da97756

Browse files
hesselonlinejoostlek
authored andcommitted
Fix missing key for ecosmart in older Wallbox models (home-assistant#146847)
* fix 146839, missing key * added tests for this issue * added tests for this issue * added tests for this issue, formatting * Prevent loading select on missing key * Prevent loading select on missing key - formatting fixed * Update homeassistant/components/wallbox/coordinator.py Co-authored-by: Joost Lekkerkerker <[email protected]> --------- Co-authored-by: Joost Lekkerkerker <[email protected]>
1 parent a7b2f80 commit da97756

File tree

5 files changed

+61
-15
lines changed

5 files changed

+61
-15
lines changed

homeassistant/components/wallbox/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@ class EcoSmartMode(StrEnum):
7474
OFF = "off"
7575
ECO_MODE = "eco_mode"
7676
FULL_SOLAR = "full_solar"
77+
DISABLED = "disabled"

homeassistant/components/wallbox/coordinator.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,20 @@ def _get_data(self) -> dict[str, Any]:
166166
)
167167

168168
# Set current solar charging mode
169-
eco_smart_enabled = data[CHARGER_DATA_KEY][CHARGER_ECO_SMART_KEY][
170-
CHARGER_ECO_SMART_STATUS_KEY
171-
]
172-
eco_smart_mode = data[CHARGER_DATA_KEY][CHARGER_ECO_SMART_KEY][
173-
CHARGER_ECO_SMART_MODE_KEY
174-
]
175-
if eco_smart_enabled is False:
169+
eco_smart_enabled = (
170+
data[CHARGER_DATA_KEY]
171+
.get(CHARGER_ECO_SMART_KEY, {})
172+
.get(CHARGER_ECO_SMART_STATUS_KEY)
173+
)
174+
175+
eco_smart_mode = (
176+
data[CHARGER_DATA_KEY]
177+
.get(CHARGER_ECO_SMART_KEY, {})
178+
.get(CHARGER_ECO_SMART_MODE_KEY)
179+
)
180+
if eco_smart_mode is None:
181+
data[CHARGER_ECO_SMART_KEY] = EcoSmartMode.DISABLED
182+
elif eco_smart_enabled is False:
176183
data[CHARGER_ECO_SMART_KEY] = EcoSmartMode.OFF
177184
elif eco_smart_mode == 0:
178185
data[CHARGER_ECO_SMART_KEY] = EcoSmartMode.ECO_MODE

homeassistant/components/wallbox/select.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ async def async_setup_entry(
6363
) -> None:
6464
"""Create wallbox select entities in HASS."""
6565
coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
66-
67-
async_add_entities(
68-
WallboxSelect(coordinator, description)
69-
for ent in coordinator.data
70-
if (
71-
(description := SELECT_TYPES.get(ent))
72-
and description.supported_fn(coordinator)
66+
if coordinator.data[CHARGER_ECO_SMART_KEY] != EcoSmartMode.DISABLED:
67+
async_add_entities(
68+
WallboxSelect(coordinator, description)
69+
for ent in coordinator.data
70+
if (
71+
(description := SELECT_TYPES.get(ent))
72+
and description.supported_fn(coordinator)
73+
)
7374
)
74-
)
7575

7676

7777
class WallboxSelect(WallboxEntity, SelectEntity):

tests/components/wallbox/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,31 @@ async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None
216216
await hass.async_block_till_done()
217217

218218

219+
async def setup_integration_no_eco_mode(
220+
hass: HomeAssistant, entry: MockConfigEntry
221+
) -> None:
222+
"""Test wallbox sensor class setup."""
223+
with requests_mock.Mocker() as mock_request:
224+
mock_request.get(
225+
"https://user-api.wall-box.com/users/signin",
226+
json=authorisation_response,
227+
status_code=HTTPStatus.OK,
228+
)
229+
mock_request.get(
230+
"https://api.wall-box.com/chargers/status/12345",
231+
json=test_response_no_power_boost,
232+
status_code=HTTPStatus.OK,
233+
)
234+
mock_request.put(
235+
"https://api.wall-box.com/v2/charger/12345",
236+
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
237+
status_code=HTTPStatus.OK,
238+
)
239+
240+
await hass.config_entries.async_setup(entry.entry_id)
241+
await hass.async_block_till_done()
242+
243+
219244
async def setup_integration_select(
220245
hass: HomeAssistant, entry: MockConfigEntry, response
221246
) -> None:

tests/components/wallbox/test_init.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
authorisation_response,
1414
setup_integration,
1515
setup_integration_connection_error,
16+
setup_integration_no_eco_mode,
1617
setup_integration_read_only,
1718
test_response,
1819
)
@@ -138,3 +139,15 @@ async def test_wallbox_refresh_failed_read_only(
138139

139140
assert await hass.config_entries.async_unload(entry.entry_id)
140141
assert entry.state is ConfigEntryState.NOT_LOADED
142+
143+
144+
async def test_wallbox_setup_load_entry_no_eco_mode(
145+
hass: HomeAssistant, entry: MockConfigEntry
146+
) -> None:
147+
"""Test Wallbox Unload."""
148+
149+
await setup_integration_no_eco_mode(hass, entry)
150+
assert entry.state is ConfigEntryState.LOADED
151+
152+
assert await hass.config_entries.async_unload(entry.entry_id)
153+
assert entry.state is ConfigEntryState.NOT_LOADED

0 commit comments

Comments
 (0)