mirror of
https://github.com/home-assistant/core.git
synced 2026-07-04 23:51:32 +02:00
Add switch platform to Indevolt integration (#163522)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -7,7 +7,7 @@ from homeassistant.core import HomeAssistant
|
||||
|
||||
from .coordinator import IndevoltConfigEntry, IndevoltCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR]
|
||||
PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR, Platform.SWITCH]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: IndevoltConfigEntry) -> bool:
|
||||
|
||||
@@ -96,6 +96,8 @@ SENSOR_KEYS = {
|
||||
"19176",
|
||||
"19177",
|
||||
"680",
|
||||
"2618",
|
||||
"7171",
|
||||
"11011",
|
||||
"11009",
|
||||
"11010",
|
||||
|
||||
@@ -255,6 +255,17 @@
|
||||
"total_ac_output_energy": {
|
||||
"name": "Total AC output energy"
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"bypass": {
|
||||
"name": "Bypass socket"
|
||||
},
|
||||
"grid_charging": {
|
||||
"name": "Allow grid charging"
|
||||
},
|
||||
"light": {
|
||||
"name": "LED indicator"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
"""Switch platform for Indevolt integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Final
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
SwitchDeviceClass,
|
||||
SwitchEntity,
|
||||
SwitchEntityDescription,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import IndevoltConfigEntry
|
||||
from .coordinator import IndevoltCoordinator
|
||||
from .entity import IndevoltEntity
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class IndevoltSwitchEntityDescription(SwitchEntityDescription):
|
||||
"""Custom entity description class for Indevolt switch entities."""
|
||||
|
||||
read_key: str
|
||||
write_key: str
|
||||
read_on_value: int = 1
|
||||
read_off_value: int = 0
|
||||
generation: list[int] = field(default_factory=lambda: [1, 2])
|
||||
|
||||
|
||||
SWITCHES: Final = (
|
||||
IndevoltSwitchEntityDescription(
|
||||
key="grid_charging",
|
||||
translation_key="grid_charging",
|
||||
generation=[2],
|
||||
read_key="2618",
|
||||
write_key="1143",
|
||||
read_on_value=1001,
|
||||
read_off_value=1000,
|
||||
device_class=SwitchDeviceClass.SWITCH,
|
||||
),
|
||||
IndevoltSwitchEntityDescription(
|
||||
key="light",
|
||||
translation_key="light",
|
||||
generation=[2],
|
||||
read_key="7171",
|
||||
write_key="7265",
|
||||
device_class=SwitchDeviceClass.SWITCH,
|
||||
),
|
||||
IndevoltSwitchEntityDescription(
|
||||
key="bypass",
|
||||
translation_key="bypass",
|
||||
generation=[2],
|
||||
read_key="680",
|
||||
write_key="7266",
|
||||
device_class=SwitchDeviceClass.SWITCH,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: IndevoltConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the switch platform for Indevolt."""
|
||||
coordinator = entry.runtime_data
|
||||
device_gen = coordinator.generation
|
||||
|
||||
# Switch initialization
|
||||
async_add_entities(
|
||||
IndevoltSwitchEntity(coordinator=coordinator, description=description)
|
||||
for description in SWITCHES
|
||||
if device_gen in description.generation
|
||||
)
|
||||
|
||||
|
||||
class IndevoltSwitchEntity(IndevoltEntity, SwitchEntity):
|
||||
"""Represents a switch entity for Indevolt devices."""
|
||||
|
||||
entity_description: IndevoltSwitchEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: IndevoltCoordinator,
|
||||
description: IndevoltSwitchEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the Indevolt switch entity."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{self.serial_number}_{description.key}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if switch is on."""
|
||||
raw_value = self.coordinator.data.get(self.entity_description.read_key)
|
||||
if raw_value is None:
|
||||
return None
|
||||
|
||||
if raw_value == self.entity_description.read_on_value:
|
||||
return True
|
||||
|
||||
if raw_value == self.entity_description.read_off_value:
|
||||
return False
|
||||
|
||||
return None
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the switch on."""
|
||||
await self._async_toggle(1)
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the switch off."""
|
||||
await self._async_toggle(0)
|
||||
|
||||
async def _async_toggle(self, value: int) -> None:
|
||||
"""Toggle the switch on/off."""
|
||||
success = await self.coordinator.async_push_data(
|
||||
self.entity_description.write_key, value
|
||||
)
|
||||
|
||||
if success:
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
else:
|
||||
raise HomeAssistantError(f"Failed to set value {value} for {self.name}")
|
||||
@@ -4,7 +4,7 @@
|
||||
"7101": 1,
|
||||
"142": 1.79,
|
||||
"6105": 5,
|
||||
"2618": 250.5,
|
||||
"2618": 1001,
|
||||
"11009": 50.2,
|
||||
"2101": 0,
|
||||
"2108": 0,
|
||||
@@ -70,5 +70,7 @@
|
||||
"19174": 15.0,
|
||||
"19175": 15.1,
|
||||
"19176": 15.3,
|
||||
"19177": 14.9
|
||||
"19177": 14.9,
|
||||
"7171": 1,
|
||||
"680": 0
|
||||
}
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
# serializer version: 1
|
||||
# name: test_switch[2][switch.cms_sf2000_allow_grid_charging-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.cms_sf2000_allow_grid_charging',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Allow grid charging',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Allow grid charging',
|
||||
'platform': 'indevolt',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'grid_charging',
|
||||
'unique_id': 'SolidFlex2000-87654321_grid_charging',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_switch[2][switch.cms_sf2000_allow_grid_charging-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'switch',
|
||||
'friendly_name': 'CMS-SF2000 Allow grid charging',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.cms_sf2000_allow_grid_charging',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_switch[2][switch.cms_sf2000_bypass_socket-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.cms_sf2000_bypass_socket',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Bypass socket',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Bypass socket',
|
||||
'platform': 'indevolt',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'bypass',
|
||||
'unique_id': 'SolidFlex2000-87654321_bypass',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_switch[2][switch.cms_sf2000_bypass_socket-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'switch',
|
||||
'friendly_name': 'CMS-SF2000 Bypass socket',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.cms_sf2000_bypass_socket',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_switch[2][switch.cms_sf2000_led_indicator-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.cms_sf2000_led_indicator',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'LED indicator',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': <SwitchDeviceClass.SWITCH: 'switch'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'LED indicator',
|
||||
'platform': 'indevolt',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'light',
|
||||
'unique_id': 'SolidFlex2000-87654321_light',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_switch[2][switch.cms_sf2000_led_indicator-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'switch',
|
||||
'friendly_name': 'CMS-SF2000 LED indicator',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.cms_sf2000_led_indicator',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,219 @@
|
||||
"""Tests for the Indevolt switch platform."""
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.indevolt.coordinator import SCAN_INTERVAL
|
||||
from homeassistant.components.switch import SERVICE_TURN_OFF, SERVICE_TURN_ON
|
||||
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
|
||||
|
||||
KEY_READ_GRID_CHARGING = "2618"
|
||||
KEY_WRITE_GRID_CHARGING = "1143"
|
||||
|
||||
KEY_READ_LIGHT = "7171"
|
||||
KEY_WRITE_LIGHT = "7265"
|
||||
|
||||
KEY_READ_BYPASS = "680"
|
||||
KEY_WRITE_BYPASS = "7266"
|
||||
|
||||
DEFAULT_STATE_ON = 1
|
||||
DEFAULT_STATE_OFF = 0
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
async def test_switch(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_indevolt: AsyncMock,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test switch entity registration and states."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SWITCH]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
("entity_id", "read_key", "write_key", "on_value"),
|
||||
[
|
||||
(
|
||||
"switch.cms_sf2000_allow_grid_charging",
|
||||
KEY_READ_GRID_CHARGING,
|
||||
KEY_WRITE_GRID_CHARGING,
|
||||
1001,
|
||||
),
|
||||
(
|
||||
"switch.cms_sf2000_led_indicator",
|
||||
KEY_READ_LIGHT,
|
||||
KEY_WRITE_LIGHT,
|
||||
DEFAULT_STATE_ON,
|
||||
),
|
||||
(
|
||||
"switch.cms_sf2000_bypass_socket",
|
||||
KEY_READ_BYPASS,
|
||||
KEY_WRITE_BYPASS,
|
||||
DEFAULT_STATE_ON,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_switch_turn_on(
|
||||
hass: HomeAssistant,
|
||||
mock_indevolt: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_id: str,
|
||||
read_key: str,
|
||||
write_key: str,
|
||||
on_value: int,
|
||||
) -> None:
|
||||
"""Test turning switches on."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SWITCH]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Reset mock call count for this iteration
|
||||
mock_indevolt.set_data.reset_mock()
|
||||
|
||||
# Update mock data to reflect the new value
|
||||
mock_indevolt.fetch_data.return_value[read_key] = on_value
|
||||
|
||||
# Call the service to turn on
|
||||
await hass.services.async_call(
|
||||
Platform.SWITCH,
|
||||
SERVICE_TURN_ON,
|
||||
{"entity_id": entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# Verify set_data was called with correct parameters
|
||||
mock_indevolt.set_data.assert_called_with(write_key, 1)
|
||||
|
||||
# Verify updated state
|
||||
assert (state := hass.states.get(entity_id)) is not None
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
("entity_id", "read_key", "write_key", "off_value"),
|
||||
[
|
||||
(
|
||||
"switch.cms_sf2000_allow_grid_charging",
|
||||
KEY_READ_GRID_CHARGING,
|
||||
KEY_WRITE_GRID_CHARGING,
|
||||
1000,
|
||||
),
|
||||
(
|
||||
"switch.cms_sf2000_led_indicator",
|
||||
KEY_READ_LIGHT,
|
||||
KEY_WRITE_LIGHT,
|
||||
DEFAULT_STATE_OFF,
|
||||
),
|
||||
(
|
||||
"switch.cms_sf2000_bypass_socket",
|
||||
KEY_READ_BYPASS,
|
||||
KEY_WRITE_BYPASS,
|
||||
DEFAULT_STATE_OFF,
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_switch_turn_off(
|
||||
hass: HomeAssistant,
|
||||
mock_indevolt: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_id: str,
|
||||
read_key: str,
|
||||
write_key: str,
|
||||
off_value: int,
|
||||
) -> None:
|
||||
"""Test turning switches off."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SWITCH]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Reset mock call count for this iteration
|
||||
mock_indevolt.set_data.reset_mock()
|
||||
|
||||
# Update mock data to reflect the new value
|
||||
mock_indevolt.fetch_data.return_value[read_key] = off_value
|
||||
|
||||
# Call the service to turn off
|
||||
await hass.services.async_call(
|
||||
Platform.SWITCH,
|
||||
SERVICE_TURN_OFF,
|
||||
{"entity_id": entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# Verify set_data was called with correct parameters
|
||||
mock_indevolt.set_data.assert_called_with(write_key, 0)
|
||||
|
||||
# Verify updated state
|
||||
assert (state := hass.states.get(entity_id)) is not None
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
async def test_switch_set_value_error(
|
||||
hass: HomeAssistant,
|
||||
mock_indevolt: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test error handling when toggling a switch."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SWITCH]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Mock set_data to raise an error
|
||||
mock_indevolt.set_data.side_effect = HomeAssistantError(
|
||||
"Device communication failed"
|
||||
)
|
||||
|
||||
# Attempt to switch on
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
Platform.SWITCH,
|
||||
SERVICE_TURN_ON,
|
||||
{"entity_id": "switch.cms_sf2000_allow_grid_charging"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
# Verify set_data was called before failing
|
||||
mock_indevolt.set_data.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("generation", [2], indirect=True)
|
||||
async def test_switch_availability(
|
||||
hass: HomeAssistant,
|
||||
mock_indevolt: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
"""Test switch entity availability / non-availability."""
|
||||
with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.SWITCH]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Confirm current state is "on"
|
||||
assert (state := hass.states.get("switch.cms_sf2000_allow_grid_charging"))
|
||||
assert state.state == STATE_ON
|
||||
|
||||
# Simulate fetch_data error
|
||||
mock_indevolt.fetch_data.side_effect = ConnectionError
|
||||
freezer.tick(delta=timedelta(seconds=SCAN_INTERVAL))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Confirm current state is "unavailable"
|
||||
assert (state := hass.states.get("switch.cms_sf2000_allow_grid_charging"))
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
Reference in New Issue
Block a user