Fix missing key for ecosmart in older Wallbox models (#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 <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Hessel 2025-06-16 15:15:17 +02:00 committed by Franck Nijhof
parent a7b2f800f8
commit da97756157
No known key found for this signature in database
GPG Key ID: AB33ADACE7101952
5 changed files with 61 additions and 15 deletions

View File

@ -74,3 +74,4 @@ class EcoSmartMode(StrEnum):
OFF = "off"
ECO_MODE = "eco_mode"
FULL_SOLAR = "full_solar"
DISABLED = "disabled"

View File

@ -166,13 +166,20 @@ class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]):
)
# Set current solar charging mode
eco_smart_enabled = data[CHARGER_DATA_KEY][CHARGER_ECO_SMART_KEY][
CHARGER_ECO_SMART_STATUS_KEY
]
eco_smart_mode = data[CHARGER_DATA_KEY][CHARGER_ECO_SMART_KEY][
CHARGER_ECO_SMART_MODE_KEY
]
if eco_smart_enabled is False:
eco_smart_enabled = (
data[CHARGER_DATA_KEY]
.get(CHARGER_ECO_SMART_KEY, {})
.get(CHARGER_ECO_SMART_STATUS_KEY)
)
eco_smart_mode = (
data[CHARGER_DATA_KEY]
.get(CHARGER_ECO_SMART_KEY, {})
.get(CHARGER_ECO_SMART_MODE_KEY)
)
if eco_smart_mode is None:
data[CHARGER_ECO_SMART_KEY] = EcoSmartMode.DISABLED
elif eco_smart_enabled is False:
data[CHARGER_ECO_SMART_KEY] = EcoSmartMode.OFF
elif eco_smart_mode == 0:
data[CHARGER_ECO_SMART_KEY] = EcoSmartMode.ECO_MODE

View File

@ -63,15 +63,15 @@ async def async_setup_entry(
) -> None:
"""Create wallbox select entities in HASS."""
coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
WallboxSelect(coordinator, description)
for ent in coordinator.data
if (
(description := SELECT_TYPES.get(ent))
and description.supported_fn(coordinator)
if coordinator.data[CHARGER_ECO_SMART_KEY] != EcoSmartMode.DISABLED:
async_add_entities(
WallboxSelect(coordinator, description)
for ent in coordinator.data
if (
(description := SELECT_TYPES.get(ent))
and description.supported_fn(coordinator)
)
)
)
class WallboxSelect(WallboxEntity, SelectEntity):

View File

@ -216,6 +216,31 @@ async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None
await hass.async_block_till_done()
async def setup_integration_no_eco_mode(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class setup."""
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=HTTPStatus.OK,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response_no_power_boost,
status_code=HTTPStatus.OK,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.OK,
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def setup_integration_select(
hass: HomeAssistant, entry: MockConfigEntry, response
) -> None:

View File

@ -13,6 +13,7 @@ from . import (
authorisation_response,
setup_integration,
setup_integration_connection_error,
setup_integration_no_eco_mode,
setup_integration_read_only,
test_response,
)
@ -138,3 +139,15 @@ async def test_wallbox_refresh_failed_read_only(
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED
async def test_wallbox_setup_load_entry_no_eco_mode(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test Wallbox Unload."""
await setup_integration_no_eco_mode(hass, entry)
assert entry.state is ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED