mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add emergency heat switch to Lyric (#193373)
This commit is contained in:
@@ -24,7 +24,13 @@ from .services import async_setup_services
|
||||
|
||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SELECT, Platform.SENSOR]
|
||||
PLATFORMS = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.CLIMATE,
|
||||
Platform.SELECT,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
]
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
|
||||
@@ -33,6 +33,8 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
LYRIC_EXCEPTIONS,
|
||||
LYRIC_HVAC_MODE_EMERGENCY_HEAT,
|
||||
LYRIC_HVAC_MODE_HEAT,
|
||||
PRESET_HOLD_UNTIL,
|
||||
PRESET_NO_HOLD,
|
||||
PRESET_PERMANENT_HOLD,
|
||||
@@ -60,7 +62,6 @@ LYRIC_HVAC_ACTION_HEAT = "Heat"
|
||||
LYRIC_HVAC_ACTION_COOL = "Cool"
|
||||
|
||||
LYRIC_HVAC_MODE_OFF = "Off"
|
||||
LYRIC_HVAC_MODE_HEAT = "Heat"
|
||||
LYRIC_HVAC_MODE_COOL = "Cool"
|
||||
LYRIC_HVAC_MODE_HEAT_COOL = "Auto"
|
||||
|
||||
@@ -78,6 +79,7 @@ LYRIC_HVAC_MODES = {
|
||||
HVAC_MODES = {
|
||||
LYRIC_HVAC_MODE_OFF: HVACMode.OFF,
|
||||
LYRIC_HVAC_MODE_HEAT: HVACMode.HEAT,
|
||||
LYRIC_HVAC_MODE_EMERGENCY_HEAT: HVACMode.HEAT,
|
||||
LYRIC_HVAC_MODE_COOL: HVACMode.COOL,
|
||||
LYRIC_HVAC_MODE_HEAT_COOL: HVACMode.HEAT_COOL,
|
||||
}
|
||||
@@ -347,6 +349,11 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity):
|
||||
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
|
||||
"""Set hvac mode."""
|
||||
_LOGGER.debug("HVAC mode: %s", hvac_mode)
|
||||
if (
|
||||
hvac_mode == HVACMode.HEAT
|
||||
and self.device.changeable_values.mode == LYRIC_HVAC_MODE_EMERGENCY_HEAT
|
||||
):
|
||||
return
|
||||
match self._attr_thermostat_type:
|
||||
case LyricThermostatType.TCC:
|
||||
update = self._async_set_hvac_mode_tcc(hvac_mode)
|
||||
|
||||
@@ -14,6 +14,9 @@ PRESET_HOLD_UNTIL = "HoldUntil"
|
||||
PRESET_PERMANENT_HOLD = "PermanentHold"
|
||||
PRESET_VACATION_HOLD = "VacationHold"
|
||||
|
||||
LYRIC_HVAC_MODE_HEAT = "Heat"
|
||||
LYRIC_HVAC_MODE_EMERGENCY_HEAT = "EmergencyHeat"
|
||||
|
||||
LYRIC_EXCEPTIONS = (
|
||||
LyricAuthenticationException,
|
||||
LyricException,
|
||||
|
||||
@@ -9,6 +9,11 @@
|
||||
"setpoint_status": {
|
||||
"default": "mdi:thermostat"
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"emergency_heat": {
|
||||
"default": "mdi:heat-wave"
|
||||
}
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
|
||||
@@ -84,9 +84,17 @@
|
||||
"setpoint_status": {
|
||||
"name": "Setpoint status"
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"emergency_heat": {
|
||||
"name": "Emergency heat"
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"failed_to_set_emergency_heat": {
|
||||
"message": "Failed to set emergency heat"
|
||||
},
|
||||
"failed_to_set_fan_mode": {
|
||||
"message": "Failed to set fan mode"
|
||||
},
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
"""Support for Honeywell Lyric switch platform."""
|
||||
|
||||
import logging
|
||||
from typing import Any, override
|
||||
|
||||
from aiolyric.objects.device import LyricDevice
|
||||
from aiolyric.objects.location import LyricLocation
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
LYRIC_EXCEPTIONS,
|
||||
LYRIC_HVAC_MODE_EMERGENCY_HEAT,
|
||||
LYRIC_HVAC_MODE_HEAT,
|
||||
)
|
||||
from .coordinator import LyricConfigEntry, LyricDataUpdateCoordinator
|
||||
from .entity import LyricDeviceEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: LyricConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Honeywell Lyric switch entities based on a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
LyricEmergencyHeatSwitch(coordinator, location, device)
|
||||
for location in coordinator.data.locations
|
||||
for device in location.devices
|
||||
if LYRIC_HVAC_MODE_EMERGENCY_HEAT in device.allowed_modes
|
||||
)
|
||||
|
||||
|
||||
class LyricEmergencyHeatSwitch(LyricDeviceEntity, SwitchEntity):
|
||||
"""Switch entity for Honeywell Lyric thermostat emergency heat."""
|
||||
|
||||
_attr_translation_key = "emergency_heat"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: LyricDataUpdateCoordinator,
|
||||
location: LyricLocation,
|
||||
device: LyricDevice,
|
||||
) -> None:
|
||||
"""Initialize the emergency heat switch entity."""
|
||||
super().__init__(
|
||||
coordinator,
|
||||
location,
|
||||
device,
|
||||
f"{device.mac_id}_emergency_heat",
|
||||
)
|
||||
|
||||
@property
|
||||
@override
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if emergency heat is on."""
|
||||
return self.device.changeable_values.mode == LYRIC_HVAC_MODE_EMERGENCY_HEAT
|
||||
|
||||
@override
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn emergency heat on."""
|
||||
await self._async_set_mode(LYRIC_HVAC_MODE_EMERGENCY_HEAT)
|
||||
|
||||
@override
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn emergency heat off, returning the thermostat to heat."""
|
||||
if self.is_on:
|
||||
await self._async_set_mode(LYRIC_HVAC_MODE_HEAT)
|
||||
|
||||
async def _async_set_mode(self, mode: str) -> None:
|
||||
"""Set the thermostat mode, then refresh the coordinator."""
|
||||
_LOGGER.debug("HVAC mode passed to lyric: %s", mode)
|
||||
try:
|
||||
await self._update_thermostat(self.location, self.device, mode=mode)
|
||||
except LYRIC_EXCEPTIONS as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="failed_to_set_emergency_heat",
|
||||
) from err
|
||||
finally:
|
||||
await self.coordinator.async_refresh()
|
||||
@@ -7,7 +7,7 @@
|
||||
"deviceModel": "T9-T10",
|
||||
"units": "Celsius",
|
||||
"indoorTemperature": 21.5,
|
||||
"allowedModes": ["Heat", "Off", "Cool", "Auto"],
|
||||
"allowedModes": ["EmergencyHeat", "Heat", "Off", "Cool", "Auto"],
|
||||
"minHeatSetpoint": 4.5,
|
||||
"maxHeatSetpoint": 32,
|
||||
"minCoolSetpoint": 10,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
# serializer version: 1
|
||||
# name: test_switch[switch.hallway_thermostat_emergency_heat-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=[('mac', 'AABBCC000011')]>,
|
||||
'disabled_by': None,
|
||||
'domain': 'switch',
|
||||
'entity_category': None,
|
||||
'entity_id': 'switch.hallway_thermostat_emergency_heat',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Emergency heat',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Emergency heat',
|
||||
'platform': 'lyric',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'emergency_heat',
|
||||
'unique_id': 'AABBCC000011_emergency_heat',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_switch[switch.hallway_thermostat_emergency_heat-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Hallway Thermostat Emergency heat',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'switch.hallway_thermostat_emergency_heat',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
@@ -97,10 +97,25 @@ async def test_hvac_action_idle(
|
||||
)
|
||||
|
||||
|
||||
async def test_emergency_heat_mode(
|
||||
hass: HomeAssistant, mock_lyric: MagicMock, freezer: FrozenDateTimeFactory
|
||||
) -> None:
|
||||
"""Test emergency heat shows as heat."""
|
||||
set_changeable_values(mock_lyric, LCC_MAC_ID, mode="EmergencyHeat")
|
||||
freezer.tick(timedelta(minutes=5))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
state = hass.states.get(LCC_ENTITY_ID)
|
||||
assert state.state == HVACMode.HEAT
|
||||
assert state.attributes[ATTR_TEMPERATURE] == 20
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("mode", "expected"),
|
||||
[
|
||||
pytest.param("Heat", {"heat_setpoint": 21}, id="heat"),
|
||||
pytest.param("EmergencyHeat", {"heat_setpoint": 21}, id="emergency_heat"),
|
||||
pytest.param("Cool", {"cool_setpoint": 21}, id="cool"),
|
||||
],
|
||||
)
|
||||
@@ -225,6 +240,22 @@ async def test_set_hvac_mode_lcc(
|
||||
mock_lyric.update_thermostat.assert_awaited_once_with(ANY, ANY, **expected)
|
||||
|
||||
|
||||
async def test_set_hvac_mode_heat_keeps_emergency_heat(
|
||||
hass: HomeAssistant, mock_lyric: MagicMock
|
||||
) -> None:
|
||||
"""Test setting heat while in emergency heat leaves emergency heat on."""
|
||||
set_changeable_values(mock_lyric, LCC_MAC_ID, mode="EmergencyHeat")
|
||||
|
||||
await hass.services.async_call(
|
||||
CLIMATE_DOMAIN,
|
||||
SERVICE_SET_HVAC_MODE,
|
||||
{ATTR_ENTITY_ID: LCC_ENTITY_ID, ATTR_HVAC_MODE: HVACMode.HEAT},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
mock_lyric.update_thermostat.assert_not_awaited()
|
||||
|
||||
|
||||
async def test_set_hvac_mode_tcc(hass: HomeAssistant, mock_lyric: MagicMock) -> None:
|
||||
"""Test setting the HVAC mode on a TCC thermostat."""
|
||||
await hass.services.async_call(
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
"""Tests for the Honeywell Lyric switch platform."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from datetime import timedelta
|
||||
from unittest.mock import ANY, MagicMock, patch
|
||||
|
||||
from aiolyric.exceptions import LyricException
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from pytest_homeassistant.common import (
|
||||
MockConfigEntry,
|
||||
async_fire_time_changed,
|
||||
snapshot_platform,
|
||||
)
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_ON,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
ENTITY_ID = "switch.hallway_thermostat_emergency_heat"
|
||||
LCC_MAC_ID = "AABBCC000011"
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def platforms() -> Generator[None]:
|
||||
"""Only set up the switch platform."""
|
||||
with patch("homeassistant.components.lyric.PLATFORMS", [Platform.SWITCH]):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def mock_lyric(
|
||||
hass: HomeAssistant,
|
||||
setup_credentials: None,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_lyric_thermostats: MagicMock,
|
||||
) -> MagicMock:
|
||||
"""Set up the integration and return the mocked Lyric client."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
return mock_lyric_thermostats
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_lyric")
|
||||
async def test_switch(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test only thermostats that allow emergency heat get a switch."""
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
async def _async_set_mode(
|
||||
hass: HomeAssistant,
|
||||
mock_lyric: MagicMock,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
mode: str,
|
||||
) -> None:
|
||||
"""Set the thermostat mode and refresh the coordinator."""
|
||||
device = mock_lyric.locations[0].devices_dict[LCC_MAC_ID]
|
||||
device.attributes["changeableValues"]["mode"] = mode
|
||||
freezer.tick(timedelta(minutes=5))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done(wait_background_tasks=True)
|
||||
|
||||
|
||||
async def test_switch_on(
|
||||
hass: HomeAssistant, mock_lyric: MagicMock, freezer: FrozenDateTimeFactory
|
||||
) -> None:
|
||||
"""Test the switch is on while the thermostat is in emergency heat."""
|
||||
await _async_set_mode(hass, mock_lyric, freezer, "EmergencyHeat")
|
||||
|
||||
assert hass.states.get(ENTITY_ID).state == STATE_ON
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("current_mode", "service", "mode"),
|
||||
[
|
||||
pytest.param("Heat", SERVICE_TURN_ON, "EmergencyHeat", id="turn_on"),
|
||||
pytest.param("EmergencyHeat", SERVICE_TURN_OFF, "Heat", id="turn_off"),
|
||||
],
|
||||
)
|
||||
async def test_turn_on_off(
|
||||
hass: HomeAssistant,
|
||||
mock_lyric: MagicMock,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
current_mode: str,
|
||||
service: str,
|
||||
mode: str,
|
||||
) -> None:
|
||||
"""Test turning emergency heat on and off."""
|
||||
await _async_set_mode(hass, mock_lyric, freezer, current_mode)
|
||||
mock_lyric.get_locations.reset_mock()
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, service, {ATTR_ENTITY_ID: ENTITY_ID}, blocking=True
|
||||
)
|
||||
|
||||
mock_lyric.update_thermostat.assert_awaited_once_with(ANY, ANY, mode=mode)
|
||||
mock_lyric.get_locations.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("current_mode", ["Off", "Heat", "Cool", "Auto"])
|
||||
async def test_turn_off_keeps_other_modes(
|
||||
hass: HomeAssistant,
|
||||
mock_lyric: MagicMock,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
current_mode: str,
|
||||
) -> None:
|
||||
"""Test turning the switch off when emergency heat is off leaves the mode alone."""
|
||||
await _async_set_mode(hass, mock_lyric, freezer, current_mode)
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID}, blocking=True
|
||||
)
|
||||
|
||||
mock_lyric.update_thermostat.assert_not_awaited()
|
||||
|
||||
|
||||
async def test_turn_on_error(hass: HomeAssistant, mock_lyric: MagicMock) -> None:
|
||||
"""Test API errors are raised as translated errors and data is still refreshed."""
|
||||
mock_lyric.update_thermostat.side_effect = LyricException("error")
|
||||
mock_lyric.get_locations.reset_mock()
|
||||
|
||||
with pytest.raises(HomeAssistantError) as err:
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_ID}, blocking=True
|
||||
)
|
||||
|
||||
assert err.value.translation_key == "failed_to_set_emergency_heat"
|
||||
mock_lyric.get_locations.assert_awaited_once()
|
||||
Reference in New Issue
Block a user