From 3c10919a15dcebc00fcb79e2e86d072cdc0cb42d Mon Sep 17 00:00:00 2001 From: Giorgio Parri <1827462+giorgioparri@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:50:03 +0100 Subject: [PATCH] Add compatibility mode switch to Lektrico (#194053) --- .../components/lektrico/strings.json | 3 + homeassistant/components/lektrico/switch.py | 12 +- .../lektrico/fixtures/get_info.json | 1 + .../lektrico/snapshots/test_switch.ambr | 50 ++++++++ tests/components/lektrico/test_switch.py | 113 +++++++++++++++++- 5 files changed, 176 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/lektrico/strings.json b/homeassistant/components/lektrico/strings.json index eb3ebc4306f7..890c4ecea605 100644 --- a/homeassistant/components/lektrico/strings.json +++ b/homeassistant/components/lektrico/strings.json @@ -168,6 +168,9 @@ "authentication": { "name": "Authentication" }, + "compatibility_mode": { + "name": "Compatibility mode" + }, "force_single_phase": { "name": "Force single phase" }, diff --git a/homeassistant/components/lektrico/switch.py b/homeassistant/components/lektrico/switch.py index 3b85f360c3d3..5830d2041a18 100644 --- a/homeassistant/components/lektrico/switch.py +++ b/homeassistant/components/lektrico/switch.py @@ -56,6 +56,16 @@ SWITCHS_FOR_3_PHASE_CHARGERS: tuple[LektricoSwitchEntityDescription, ...] = ( ), ) +SWITCHS_FOR_1P7K_CHARGERS: tuple[LektricoSwitchEntityDescription, ...] = ( + LektricoSwitchEntityDescription( + key="compatibility_mode", + translation_key="compatibility_mode", + entity_category=EntityCategory.CONFIG, + value_fn=lambda data: bool(data["precise_pwm_mode"]), + set_value_fn=lambda device, data, value: device.set_compatibility_mode(value), + ), +) + async def async_setup_entry( hass: HomeAssistant, @@ -69,7 +79,7 @@ async def async_setup_entry( if coordinator.device_type == Device.TYPE_3P22K: switchs_to_be_used = SWITCHS_FOR_ALL_CHARGERS + SWITCHS_FOR_3_PHASE_CHARGERS else: - switchs_to_be_used = SWITCHS_FOR_ALL_CHARGERS + switchs_to_be_used = SWITCHS_FOR_ALL_CHARGERS + SWITCHS_FOR_1P7K_CHARGERS async_add_entities( LektricoSwitch( diff --git a/tests/components/lektrico/fixtures/get_info.json b/tests/components/lektrico/fixtures/get_info.json index 6528ec73d921..46ff93874444 100644 --- a/tests/components/lektrico/fixtures/get_info.json +++ b/tests/components/lektrico/fixtures/get_info.json @@ -11,6 +11,7 @@ "current_l1": 0.0, "fw_version": "1.44", "led_max_brightness": 20, + "precise_pwm_mode": true, "dynamic_current": 32, "user_current": 32, "lb_mode": 0, diff --git a/tests/components/lektrico/snapshots/test_switch.ambr b/tests/components/lektrico/snapshots/test_switch.ambr index 6d056b4fe02b..6b23020b3c6e 100644 --- a/tests/components/lektrico/snapshots/test_switch.ambr +++ b/tests/components/lektrico/snapshots/test_switch.ambr @@ -49,6 +49,56 @@ 'state': 'on', }) # --- +# name: test_all_entities[switch.1p7k_500006_compatibility_mode-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': None, + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.1p7k_500006_compatibility_mode', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Compatibility mode', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Compatibility mode', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'compatibility_mode', + 'unique_id': '500006_compatibility_mode', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[switch.1p7k_500006_compatibility_mode-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : '1p7k_500006 Compatibility mode', + }), + 'context': , + 'entity_id': 'switch.1p7k_500006_compatibility_mode', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_all_entities[switch.1p7k_500006_lock-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ diff --git a/tests/components/lektrico/test_switch.py b/tests/components/lektrico/test_switch.py index dc8d925dbe7b..933903701ac0 100644 --- a/tests/components/lektrico/test_switch.py +++ b/tests/components/lektrico/test_switch.py @@ -1,15 +1,46 @@ """Tests for the Lektrico switch platform.""" +from typing import Any from unittest.mock import AsyncMock, patch -from pytest_homeassistant.common import MockConfigEntry, snapshot_platform +import pytest +from pytest_homeassistant.common import ( + MockConfigEntry, + load_json_object_fixture, + snapshot_platform, +) from syrupy.assertion import SnapshotAssertion -from homeassistant.const import Platform +from homeassistant.components.lektrico.const import ( + CONF_HW_VERSION, + CONF_SERIAL_NUMBER, + DOMAIN, +) +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.const import ( + ATTR_ENTITY_ID, + CONF_HOST, + CONF_TYPE, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_OFF, + STATE_ON, + Platform, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from . import setup_integration +from .conftest import MOCKED_DEVICE_IP_ADDRESS, MOCKED_DEVICE_SERIAL_NUMBER + +COMPATIBILITY_MODE_ENTITY_ID = "switch.1p7k_500006_compatibility_mode" + + +def _device_info(**overrides: Any) -> dict[str, Any]: + """Return the device info fixture with the given overrides applied.""" + data = load_json_object_fixture("get_info.json", DOMAIN) + data.update(overrides) + return data async def test_all_entities( @@ -29,3 +60,81 @@ async def test_all_entities( await setup_integration(hass, mock_config_entry) await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +@pytest.mark.parametrize( + ("service", "target_value", "initial_state", "expected_state"), + [ + pytest.param(SERVICE_TURN_ON, True, STATE_OFF, STATE_ON, id="turn_on"), + pytest.param(SERVICE_TURN_OFF, False, STATE_ON, STATE_OFF, id="turn_off"), + ], +) +async def test_set_compatibility_mode( + hass: HomeAssistant, + mock_device: AsyncMock, + mock_config_entry: MockConfigEntry, + service: str, + target_value: bool, + initial_state: str, + expected_state: str, +) -> None: + """Test turning the compatibility mode switch on and off.""" + mock_device.device_info.return_value = _device_info( + precise_pwm_mode=not target_value + ) + + with patch.multiple( + "homeassistant.components.lektrico", + CHARGERS_PLATFORMS=[Platform.SWITCH], + LB_DEVICES_PLATFORMS=[Platform.SWITCH], + ): + await setup_integration(hass, mock_config_entry) + + assert hass.states.get(COMPATIBILITY_MODE_ENTITY_ID).state == initial_state + + mock_device.device_info.return_value = _device_info(precise_pwm_mode=target_value) + await hass.services.async_call( + SWITCH_DOMAIN, + service, + {ATTR_ENTITY_ID: COMPATIBILITY_MODE_ENTITY_ID}, + blocking=True, + ) + await hass.async_block_till_done() + + mock_device.set_compatibility_mode.assert_awaited_once_with(target_value) + assert hass.states.get(COMPATIBILITY_MODE_ENTITY_ID).state == expected_state + + +async def test_compatibility_mode_not_created_for_three_phase_charger( + hass: HomeAssistant, + mock_device: AsyncMock, +) -> None: + """Test the compatibility mode switch is created for 1P7K chargers only.""" + mock_device.device_config.return_value = { + "type": "3p22k", + "serial_number": MOCKED_DEVICE_SERIAL_NUMBER, + "board_revision": "B", + } + mock_device.device_info.return_value = _device_info( + precise_pwm_mode=True, relay_mode=3 + ) + config_entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: MOCKED_DEVICE_IP_ADDRESS, + CONF_TYPE: "3p22k", + CONF_SERIAL_NUMBER: MOCKED_DEVICE_SERIAL_NUMBER, + CONF_HW_VERSION: "B", + }, + unique_id=MOCKED_DEVICE_SERIAL_NUMBER, + ) + + with patch.multiple( + "homeassistant.components.lektrico", + CHARGERS_PLATFORMS=[Platform.SWITCH], + LB_DEVICES_PLATFORMS=[Platform.SWITCH], + ): + await setup_integration(hass, config_entry) + + assert hass.states.get("switch.3p22k_500006_compatibility_mode") is None + assert hass.states.get("switch.3p22k_500006_force_single_phase") is not None