mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add an operating mode select to ViCare heating circuits (#191172)
This commit is contained in:
@@ -5,6 +5,7 @@ from typing import override
|
||||
|
||||
from PyViCare.PyViCareDevice import Device as PyViCareDevice
|
||||
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
||||
from PyViCare.PyViCareHeatingDevice import HeatingCircuit as PyViCareHeatingCircuit
|
||||
from PyViCare.PyViCareUtils import PyViCareNotSupportedFeatureError
|
||||
|
||||
from homeassistant.components.select import SelectEntity
|
||||
@@ -14,7 +15,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .entity import ViCareEntity
|
||||
from .types import ViCareConfigEntry, ViCareDevice
|
||||
from .utils import is_supported
|
||||
from .utils import get_circuits, is_supported
|
||||
|
||||
# Map API values to snake_case for HA, and back
|
||||
DHW_MODE_API_TO_HA: dict[str, str] = {
|
||||
@@ -24,12 +25,27 @@ DHW_MODE_API_TO_HA: dict[str, str] = {
|
||||
}
|
||||
DHW_MODE_HA_TO_API: dict[str, str] = {v: k for k, v in DHW_MODE_API_TO_HA.items()}
|
||||
|
||||
CIRCUIT_MODE_API_TO_HA: dict[str, str] = {
|
||||
"cooling": "cooling",
|
||||
"dhw": "dhw",
|
||||
"dhwAndHeating": "dhw_and_heating",
|
||||
"dhwAndHeatingCooling": "dhw_and_heating_cooling",
|
||||
"forcedNormal": "forced_normal",
|
||||
"forcedReduced": "forced_reduced",
|
||||
"heating": "heating",
|
||||
"heatingCooling": "heating_cooling",
|
||||
"standby": "standby",
|
||||
}
|
||||
CIRCUIT_MODE_HA_TO_API: dict[str, str] = {
|
||||
v: k for k, v in CIRCUIT_MODE_API_TO_HA.items()
|
||||
}
|
||||
|
||||
|
||||
def _build_entities(
|
||||
device_list: list[ViCareDevice],
|
||||
) -> list[ViCareDHWOperatingModeSelect]:
|
||||
) -> list[SelectEntity]:
|
||||
"""Create ViCare select entities for a device."""
|
||||
return [
|
||||
entities: list[SelectEntity] = [
|
||||
ViCareDHWOperatingModeSelect(
|
||||
device.serial,
|
||||
device.config,
|
||||
@@ -42,6 +58,18 @@ def _build_entities(
|
||||
device.api,
|
||||
)
|
||||
]
|
||||
entities.extend(
|
||||
ViCareCircuitOperatingModeSelect(
|
||||
device.serial,
|
||||
device.config,
|
||||
device.api,
|
||||
circuit,
|
||||
)
|
||||
for device in device_list
|
||||
for circuit in get_circuits(device.api)
|
||||
if is_supported("operating_mode", lambda api: api.getActiveMode(), circuit)
|
||||
)
|
||||
return entities
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@@ -99,3 +127,46 @@ class ViCareDHWOperatingModeSelect(ViCareEntity, SelectEntity):
|
||||
self._api.setDomesticHotWaterOperatingMode(api_mode)
|
||||
self._attr_current_option = option
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
|
||||
class ViCareCircuitOperatingModeSelect(ViCareEntity, SelectEntity):
|
||||
"""Representation of the operating mode of a ViCare heating circuit."""
|
||||
|
||||
_attr_translation_key = "operating_mode"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
device_serial: str | None,
|
||||
device_config: PyViCareDeviceConfig,
|
||||
device: PyViCareDevice,
|
||||
circuit: PyViCareHeatingCircuit,
|
||||
) -> None:
|
||||
"""Initialize the operating mode select entity."""
|
||||
super().__init__(
|
||||
"operating_mode", device_serial, device_config, device, circuit
|
||||
)
|
||||
self._attr_options = [
|
||||
CIRCUIT_MODE_API_TO_HA.get(mode, mode) for mode in circuit.getModes()
|
||||
]
|
||||
active = circuit.getActiveMode()
|
||||
self._attr_current_option = CIRCUIT_MODE_API_TO_HA.get(active, active)
|
||||
|
||||
def update(self) -> None:
|
||||
"""Update state from the ViCare API."""
|
||||
with self.vicare_api_handler():
|
||||
with suppress(PyViCareNotSupportedFeatureError):
|
||||
self._attr_options = [
|
||||
CIRCUIT_MODE_API_TO_HA.get(mode, mode)
|
||||
for mode in self._api.getModes()
|
||||
]
|
||||
|
||||
with suppress(PyViCareNotSupportedFeatureError):
|
||||
active = self._api.getActiveMode()
|
||||
self._attr_current_option = CIRCUIT_MODE_API_TO_HA.get(active, active)
|
||||
|
||||
@override
|
||||
def select_option(self, option: str) -> None:
|
||||
"""Set the operating mode of the heating circuit."""
|
||||
self._api.setMode(CIRCUIT_MODE_HA_TO_API.get(option, option))
|
||||
self._attr_current_option = option
|
||||
self.schedule_update_ha_state()
|
||||
|
||||
@@ -165,6 +165,20 @@
|
||||
"efficient_with_min_comfort": "Efficient with minimum comfort",
|
||||
"off": "[%key:common::state::off%]"
|
||||
}
|
||||
},
|
||||
"operating_mode": {
|
||||
"name": "Operating mode",
|
||||
"state": {
|
||||
"cooling": "[%key:component::vicare::entity::sensor::active_mode::state::cooling%]",
|
||||
"dhw": "DHW only",
|
||||
"dhw_and_heating": "DHW and heating",
|
||||
"dhw_and_heating_cooling": "DHW, heating and cooling",
|
||||
"forced_normal": "Permanently normal",
|
||||
"forced_reduced": "Permanently reduced",
|
||||
"heating": "[%key:component::vicare::entity::sensor::active_mode::state::heating%]",
|
||||
"heating_cooling": "Heating and cooling",
|
||||
"standby": "[%key:common::state::standby%]"
|
||||
}
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
|
||||
@@ -60,3 +60,62 @@
|
||||
'state': 'efficient',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.model0_operating_mode-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
<SelectEntityCapabilityAttribute.OPTIONS: 'options'>: list([
|
||||
'heating',
|
||||
'standby',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': None,
|
||||
'device_class': None,
|
||||
'device_id': <DeviceEntry identifiers=[('vicare', 'gateway0_deviceSerialVitocal250A')]>,
|
||||
'disabled_by': None,
|
||||
'domain': 'select',
|
||||
'entity_category': None,
|
||||
'entity_id': 'select.model0_operating_mode',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Operating mode',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Operating mode',
|
||||
'platform': 'vicare',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'operating_mode',
|
||||
'unique_id': 'gateway0_deviceSerialVitocal250A-operating_mode-0',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.model0_operating_mode-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'model0 Operating mode',
|
||||
<SelectEntityCapabilityAttribute.OPTIONS: 'options'>: list([
|
||||
'heating',
|
||||
'standby',
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'select.model0_operating_mode',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'heating',
|
||||
})
|
||||
# ---
|
||||
|
||||
@@ -8,8 +8,13 @@ from pytest_homeassistant.common import MockConfigEntry, snapshot_platform
|
||||
from PyViCare.PyViCareUtils import PyViCareRateLimitError
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.components.select import (
|
||||
ATTR_OPTION,
|
||||
ATTR_OPTIONS,
|
||||
DOMAIN as SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity_component import async_update_entity
|
||||
@@ -92,3 +97,49 @@ async def test_api_error_logged_on_the_edge(
|
||||
with patch(GET_DHW_MODES, side_effect=error):
|
||||
await async_update_entity(hass, entity_id)
|
||||
assert len(logged()) == 2
|
||||
|
||||
|
||||
async def test_circuit_operating_mode(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the operating mode select of a heating circuit."""
|
||||
fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")]
|
||||
mock_vicare = MockPyViCare(fixtures)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.helpers.config_entry_oauth2_flow.OAuth2Session.async_ensure_token_valid",
|
||||
),
|
||||
patch(f"{MODULE}.PyViCare", return_value=mock_vicare),
|
||||
patch(f"{MODULE}.PLATFORMS", [Platform.SELECT]),
|
||||
):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get("select.model0_operating_mode")
|
||||
assert state is not None
|
||||
assert state.state == "dhw"
|
||||
assert state.attributes[ATTR_OPTIONS] == [
|
||||
"standby",
|
||||
"dhw",
|
||||
"dhw_and_heating",
|
||||
"forced_reduced",
|
||||
"forced_normal",
|
||||
]
|
||||
|
||||
await hass.services.async_call(
|
||||
SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{
|
||||
ATTR_ENTITY_ID: "select.model0_operating_mode",
|
||||
ATTR_OPTION: "dhw_and_heating",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_vicare.devices[0].service.setProperty.assert_called_once_with(
|
||||
mock_vicare.devices[0].accessor,
|
||||
"heating.circuits.0.operating.modes.active",
|
||||
"setMode",
|
||||
{"mode": "dhwAndHeating"},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user