Add compatibility mode switch to Lektrico (#194053)

This commit is contained in:
Giorgio Parri
2026-11-24 18:50:03 +00:00
committed by GitHub
parent eab6a483a8
commit 3c10919a15
5 changed files with 176 additions and 3 deletions
@@ -168,6 +168,9 @@
"authentication": {
"name": "Authentication"
},
"compatibility_mode": {
"name": "Compatibility mode"
},
"force_single_phase": {
"name": "Force single phase"
},
+11 -1
View File
@@ -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(
@@ -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,
@@ -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': <ANY>,
'config_subentry_id': None,
'device_class': None,
'device_id': <DeviceEntry identifiers=[('lektrico', '500006')]>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.1p7k_500006_compatibility_mode',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'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({
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: '1p7k_500006 Compatibility mode',
}),
'context': <ANY>,
'entity_id': 'switch.1p7k_500006_compatibility_mode',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_all_entities[switch.1p7k_500006_lock-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
+111 -2
View File
@@ -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