Add support for Energy Socket v2 API to HomeWizard (#193403)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lex Postma
2026-11-19 16:36:55 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 249295db47
commit 0ed1ff906d
12 changed files with 427 additions and 33 deletions
+12 -6
View File
@@ -39,18 +39,24 @@ SWITCHES = [
name=None,
device_class=SwitchDeviceClass.OUTLET,
create_fn=lambda x: x.device.supports_state(),
available_fn=lambda x: x.state is not None and not x.state.switch_lock,
is_on_fn=lambda x: x.state.power_on if x.state else None,
set_fn=lambda api, active: api.state(power_on=active),
available_fn=lambda x: (
x.system is not None
and x.system.power_on is not None
and not x.system.switch_lock
),
is_on_fn=lambda x: x.system.power_on if x.system else None,
set_fn=lambda api, active: api.system(power_on=active),
),
HomeWizardSwitchEntityDescription(
key="switch_lock",
translation_key="switch_lock",
entity_category=EntityCategory.CONFIG,
create_fn=lambda x: x.device.supports_state(),
available_fn=lambda x: x.state is not None,
is_on_fn=lambda x: x.state.switch_lock if x.state else None,
set_fn=lambda api, active: api.state(switch_lock=active),
available_fn=lambda x: (
x.system is not None and x.system.switch_lock is not None
),
is_on_fn=lambda x: x.system.switch_lock if x.system else None,
set_fn=lambda api, active: api.system(switch_lock=active),
),
HomeWizardSwitchEntityDescription(
key="cloud_connection",
+15
View File
@@ -207,6 +207,21 @@ async def init_integration(
return mock_config_entry
@pytest.fixture
async def init_integration_v2(
hass: HomeAssistant,
mock_config_entry_v2: MockConfigEntry,
mock_homewizardenergy_v2: AsyncMock,
) -> MockConfigEntry:
"""Set up the HomeWizard integration using the v2 API for testing."""
mock_config_entry_v2.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry_v2.entry_id)
await hass.async_block_till_done()
return mock_config_entry_v2
@pytest.fixture
def mock_onboarding() -> Generator[MagicMock]:
"""Mock that Home Assistant is currently onboarding."""
@@ -0,0 +1,7 @@
{
"product_type": "HWE-SKT",
"product_name": "Energy Socket",
"serial": "5c2fafabcdef",
"firmware_version": "5.00",
"api_version": "2.3.1"
}
@@ -0,0 +1,11 @@
{
"energy_import_kwh": 30.511,
"energy_export_kwh": 85.951,
"power_w": 543.312,
"voltage_v": 231.539,
"current_a": 2.346,
"frequency_hz": 50.005,
"reactive_power_var": 123.456,
"apparent_power_va": 666.768,
"power_factor": 0.81688
}
@@ -0,0 +1,10 @@
{
"wifi_ssid": "My Wi-Fi",
"wifi_rssi_db": -77,
"cloud_enabled": true,
"uptime_s": 356,
"status_led_brightness_pct": 100,
"api_v1_enabled": true,
"power_on": true,
"switch_lock": false
}
@@ -900,3 +900,95 @@
}),
})
# ---
# name: test_diagnostics_v2[HWE-SKT]
dict({
'data': dict({
'batteries': None,
'device': dict({
'api_version': '2.3.1',
'firmware_version': '5.00',
'id': '**REDACTED**',
'model_name': 'Energy Socket',
'product_name': 'Energy Socket',
'product_type': 'HWE-SKT',
'serial': '**REDACTED**',
}),
'measurement': dict({
'active_liter_lpm': None,
'any_power_fail_count': None,
'apparent_power_l1_va': None,
'apparent_power_l2_va': None,
'apparent_power_l3_va': None,
'apparent_power_va': 666.768,
'average_power_15m_w': None,
'current_a': 2.346,
'current_l1_a': None,
'current_l2_a': None,
'current_l3_a': None,
'cycles': None,
'energy_export_kwh': 85.951,
'energy_export_t1_kwh': None,
'energy_export_t2_kwh': None,
'energy_export_t3_kwh': None,
'energy_export_t4_kwh': None,
'energy_import_kwh': 30.511,
'energy_import_t1_kwh': None,
'energy_import_t2_kwh': None,
'energy_import_t3_kwh': None,
'energy_import_t4_kwh': None,
'external_devices': None,
'frequency_hz': 50.005,
'long_power_fail_count': None,
'meter_model': None,
'monthly_power_peak_timestamp': None,
'monthly_power_peak_w': None,
'power_factor': 0.81688,
'power_factor_l1': None,
'power_factor_l2': None,
'power_factor_l3': None,
'power_l1_w': None,
'power_l2_w': None,
'power_l3_w': None,
'power_w': 543.312,
'protocol_version': None,
'reactive_power_l1_var': None,
'reactive_power_l2_var': None,
'reactive_power_l3_var': None,
'reactive_power_var': 123.456,
'state_of_charge_pct': None,
'tariff': None,
'timestamp': None,
'total_liter_m3': None,
'unique_id': None,
'voltage_l1_v': None,
'voltage_l2_v': None,
'voltage_l3_v': None,
'voltage_sag_l1_count': None,
'voltage_sag_l2_count': None,
'voltage_sag_l3_count': None,
'voltage_swell_l1_count': None,
'voltage_swell_l2_count': None,
'voltage_swell_l3_count': None,
'voltage_v': 231.539,
'wifi_ssid': None,
'wifi_strength': None,
}),
'state': None,
'system': dict({
'api_v1_enabled': True,
'cloud_enabled': True,
'power_on': True,
'status_led_brightness_pct': 100,
'switch_lock': False,
'uptime_s': 356,
'wifi_rssi_db': -77,
'wifi_ssid': '**REDACTED**',
'wifi_strength_pct': 46,
}),
}),
'entry': dict({
'ip_address': '**REDACTED**',
'token': '**REDACTED**',
}),
})
# ---
@@ -167,7 +167,7 @@
'via_device_id': None,
})
# ---
# name: test_switch_entities[HWE-SKT-11-switch.device-state-power_on]
# name: test_switch_entities[HWE-SKT-11-switch.device-system-power_on]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'outlet',
@@ -181,7 +181,7 @@
'state': 'on',
})
# ---
# name: test_switch_entities[HWE-SKT-11-switch.device-state-power_on].1
# name: test_switch_entities[HWE-SKT-11-switch.device-system-power_on].1
EntityRegistryEntrySnapshot({
'aliases': list([
None,
@@ -218,7 +218,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_switch_entities[HWE-SKT-11-switch.device-state-power_on].2
# name: test_switch_entities[HWE-SKT-11-switch.device-system-power_on].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entry_id': <ANY>,
@@ -336,7 +336,7 @@
'via_device_id': None,
})
# ---
# name: test_switch_entities[HWE-SKT-11-switch.device_switch_lock-state-switch_lock]
# name: test_switch_entities[HWE-SKT-11-switch.device_switch_lock-system-switch_lock]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Device Switch lock',
@@ -349,7 +349,7 @@
'state': 'off',
})
# ---
# name: test_switch_entities[HWE-SKT-11-switch.device_switch_lock-state-switch_lock].1
# name: test_switch_entities[HWE-SKT-11-switch.device_switch_lock-system-switch_lock].1
EntityRegistryEntrySnapshot({
'aliases': list([
None,
@@ -386,7 +386,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_switch_entities[HWE-SKT-11-switch.device_switch_lock-state-switch_lock].2
# name: test_switch_entities[HWE-SKT-11-switch.device_switch_lock-system-switch_lock].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entry_id': <ANY>,
@@ -420,7 +420,7 @@
'via_device_id': None,
})
# ---
# name: test_switch_entities[HWE-SKT-21-switch.device-state-power_on]
# name: test_switch_entities[HWE-SKT-21-switch.device-system-power_on]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'outlet',
@@ -434,7 +434,7 @@
'state': 'on',
})
# ---
# name: test_switch_entities[HWE-SKT-21-switch.device-state-power_on].1
# name: test_switch_entities[HWE-SKT-21-switch.device-system-power_on].1
EntityRegistryEntrySnapshot({
'aliases': list([
None,
@@ -471,7 +471,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_switch_entities[HWE-SKT-21-switch.device-state-power_on].2
# name: test_switch_entities[HWE-SKT-21-switch.device-system-power_on].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entry_id': <ANY>,
@@ -589,7 +589,7 @@
'via_device_id': None,
})
# ---
# name: test_switch_entities[HWE-SKT-21-switch.device_switch_lock-state-switch_lock]
# name: test_switch_entities[HWE-SKT-21-switch.device_switch_lock-system-switch_lock]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Device Switch lock',
@@ -602,7 +602,7 @@
'state': 'off',
})
# ---
# name: test_switch_entities[HWE-SKT-21-switch.device_switch_lock-state-switch_lock].1
# name: test_switch_entities[HWE-SKT-21-switch.device_switch_lock-system-switch_lock].1
EntityRegistryEntrySnapshot({
'aliases': list([
None,
@@ -639,7 +639,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_switch_entities[HWE-SKT-21-switch.device_switch_lock-state-switch_lock].2
# name: test_switch_entities[HWE-SKT-21-switch.device_switch_lock-system-switch_lock].2
DeviceRegistryEntrySnapshot({
'area_id': None,
'config_entry_id': <ANY>,
@@ -925,3 +925,154 @@
'via_device_id': None,
})
# ---
# name: test_switch_entities_v2[switch.device-power_on-HWE-SKT]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'outlet',
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Device',
}),
'context': <ANY>,
'entity_id': 'switch.device',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_switch_entities_v2[switch.device-power_on-HWE-SKT].1
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': None,
'device_class': None,
'device_id': <DeviceEntry identifiers=[('homewizard', '5c2fafabcdef')]>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.device',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': None,
'options': dict({
}),
'original_device_class': <SwitchDeviceClass.OUTLET: 'outlet'>,
'original_icon': None,
'original_name': None,
'platform': 'homewizard',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'HWE-BAT_5c2fafabcdef_power_on',
'unit_of_measurement': None,
})
# ---
# name: test_switch_entities_v2[switch.device_cloud_connection-cloud_enabled-HWE-SKT]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Device Cloud connection',
}),
'context': <ANY>,
'entity_id': 'switch.device_cloud_connection',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_switch_entities_v2[switch.device_cloud_connection-cloud_enabled-HWE-SKT].1
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': None,
'device_class': None,
'device_id': <DeviceEntry identifiers=[('homewizard', '5c2fafabcdef')]>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.device_cloud_connection',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Cloud connection',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Cloud connection',
'platform': 'homewizard',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'cloud_connection',
'unique_id': 'HWE-BAT_5c2fafabcdef_cloud_connection',
'unit_of_measurement': None,
})
# ---
# name: test_switch_entities_v2[switch.device_switch_lock-switch_lock-HWE-SKT]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Device Switch lock',
}),
'context': <ANY>,
'entity_id': 'switch.device_switch_lock',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_switch_entities_v2[switch.device_switch_lock-switch_lock-HWE-SKT].1
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': None,
'device_class': None,
'device_id': <DeviceEntry identifiers=[('homewizard', '5c2fafabcdef')]>,
'disabled_by': None,
'domain': 'switch',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'switch.device_switch_lock',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Switch lock',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Switch lock',
'platform': 'homewizard',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'switch_lock',
'unique_id': 'HWE-BAT_5c2fafabcdef_switch_lock',
'unit_of_measurement': None,
})
# ---
@@ -690,7 +690,7 @@ def fast_token_polling() -> Generator[None]:
@pytest.mark.usefixtures("mock_setup_entry")
@pytest.mark.parametrize(("device_fixture"), ["HWE-P1", "HWE-KWH1"])
@pytest.mark.parametrize(("device_fixture"), ["HWE-P1", "HWE-KWH1", "HWE-SKT"])
async def test_manual_flow_works_with_v2_api_support(
hass: HomeAssistant,
mock_homewizardenergy_v2: MagicMock,
@@ -809,7 +809,7 @@ async def test_manual_flow_closed_while_connecting(
@pytest.mark.usefixtures("mock_setup_entry")
@pytest.mark.parametrize(("device_fixture"), ["HWE-P1", "HWE-KWH1"])
@pytest.mark.parametrize(("device_fixture"), ["HWE-P1", "HWE-KWH1", "HWE-SKT"])
async def test_reauth_flow_updates_token(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
@@ -34,3 +34,17 @@ async def test_diagnostics(
await get_diagnostics_for_config_entry(hass, hass_client, init_integration)
== snapshot
)
@pytest.mark.parametrize("device_fixture", ["HWE-SKT"])
async def test_diagnostics_v2(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
init_integration_v2: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test diagnostics for devices using the v2 API."""
assert (
await get_diagnostics_for_config_entry(hass, hass_client, init_integration_v2)
== snapshot
)
+1 -1
View File
@@ -43,7 +43,7 @@ async def test_load_unload_v1(
assert weak_ref() is None
@pytest.mark.parametrize(("device_fixture"), ["HWE-P1", "HWE-KWH1"])
@pytest.mark.parametrize(("device_fixture"), ["HWE-P1", "HWE-KWH1", "HWE-SKT"])
async def test_load_unload_v2(
hass: HomeAssistant,
mock_config_entry_v2: MockConfigEntry,
+27 -4
View File
@@ -17,11 +17,8 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.util import dt as dt_util
pytestmark = [
pytest.mark.usefixtures("init_integration"),
]
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize("device_fixture", ["HWE-SKT-11", "HWE-SKT-21"])
async def test_number_entities(
hass: HomeAssistant,
@@ -100,9 +97,35 @@ async def test_number_entities(
)
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
"device_fixture", ["HWE-P1", "HWE-WTR", "SDM230", "SDM630", "HWE-KWH1", "HWE-KWH3"]
)
async def test_entities_not_created_for_device(hass: HomeAssistant) -> None:
"""Does not load number when device has no support for it."""
assert not hass.states.get("number.device_status_light_brightness")
@pytest.mark.usefixtures("init_integration_v2")
@pytest.mark.parametrize("device_fixture", ["HWE-SKT"])
async def test_number_entities_v2(
hass: HomeAssistant,
mock_homewizardenergy_v2: MagicMock,
) -> None:
"""Test status light brightness is controlled via the v2 system endpoint."""
assert (state := hass.states.get("number.device_status_light_brightness"))
assert state.state == "100"
await hass.services.async_call(
number.DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: state.entity_id,
ATTR_VALUE: 50,
},
blocking=True,
)
mock_homewizardenergy_v2.system.assert_called_once_with(
status_led_brightness_pct=50
)
+73 -8
View File
@@ -14,6 +14,7 @@ from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_ON,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
@@ -21,11 +22,8 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.util import dt as dt_util
pytestmark = [
pytest.mark.usefixtures("init_integration"),
]
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
("device_fixture", "entity_ids"),
[
@@ -82,14 +80,15 @@ async def test_entities_not_created_for_device(
assert not hass.states.get(entity_id)
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
("device_fixture", "entity_id", "method", "parameter"),
[
("HWE-SKT-11", "switch.device", "state", "power_on"),
("HWE-SKT-11", "switch.device_switch_lock", "state", "switch_lock"),
("HWE-SKT-11", "switch.device", "system", "power_on"),
("HWE-SKT-11", "switch.device_switch_lock", "system", "switch_lock"),
("HWE-SKT-11", "switch.device_cloud_connection", "system", "cloud_enabled"),
("HWE-SKT-21", "switch.device", "state", "power_on"),
("HWE-SKT-21", "switch.device_switch_lock", "state", "switch_lock"),
("HWE-SKT-21", "switch.device", "system", "power_on"),
("HWE-SKT-21", "switch.device_switch_lock", "system", "switch_lock"),
("HWE-SKT-21", "switch.device_cloud_connection", "system", "cloud_enabled"),
("HWE-WTR", "switch.device_cloud_connection", "system", "cloud_enabled"),
("SDM230", "switch.device_cloud_connection", "system", "cloud_enabled"),
@@ -194,6 +193,7 @@ async def test_switch_entities(
)
@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize("device_fixture", ["HWE-SKT-11", "HWE-SKT-21"])
@pytest.mark.parametrize("exception", [RequestError, UnsupportedError])
@pytest.mark.parametrize(
@@ -219,3 +219,68 @@ async def test_switch_unreachable(
assert (state := hass.states.get(entity_id))
assert state.state == STATE_UNAVAILABLE
@pytest.mark.usefixtures("init_integration_v2")
@pytest.mark.parametrize("device_fixture", ["HWE-SKT"])
@pytest.mark.parametrize(
("entity_id", "parameter"),
[
("switch.device", "power_on"),
("switch.device_switch_lock", "switch_lock"),
("switch.device_cloud_connection", "cloud_enabled"),
],
)
async def test_switch_entities_v2(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_homewizardenergy_v2: MagicMock,
snapshot: SnapshotAssertion,
entity_id: str,
parameter: str,
) -> None:
"""Test that switch entities are controlled via the v2 system endpoint."""
assert (state := hass.states.get(entity_id))
assert snapshot == state
assert (entity_entry := entity_registry.async_get(entity_id))
assert snapshot == entity_entry
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
mock_homewizardenergy_v2.system.assert_called_with(**{parameter: True})
await hass.services.async_call(
switch.DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: entity_id},
blocking=True,
)
mock_homewizardenergy_v2.system.assert_called_with(**{parameter: False})
assert len(mock_homewizardenergy_v2.system.mock_calls) == 2
assert len(mock_homewizardenergy_v2.state.mock_calls) == 0
@pytest.mark.usefixtures("init_integration_v2")
@pytest.mark.parametrize("device_fixture", ["HWE-SKT"])
async def test_power_switch_unavailable_when_switch_lock_enabled_v2(
hass: HomeAssistant,
mock_homewizardenergy_v2: MagicMock,
) -> None:
"""Test the power switch is unavailable while the switch lock is enabled."""
assert (state := hass.states.get("switch.device"))
assert state.state == STATE_ON
mock_homewizardenergy_v2.combined.return_value.system.switch_lock = True
async_fire_time_changed(hass, dt_util.utcnow() + UPDATE_INTERVAL)
await hass.async_block_till_done()
assert (state := hass.states.get("switch.device"))
assert state.state == STATE_UNAVAILABLE
assert (state := hass.states.get("switch.device_switch_lock"))
assert state.state == STATE_ON