Add fan modes in Lyric integration (#100420)

* Add fan modes in Lyric integration

* add fan_mode only when available

* move supported_features to init

* mapped fan_modes to built-in modes

* log KeyError for fan_modes
This commit is contained in:
Jieyu Yan 2023-09-16 11:58:00 -07:00 committed by GitHub
parent f715f5c76f
commit 81af45347f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 7 deletions

View File

@ -133,6 +133,7 @@ class LyricEntity(CoordinatorEntity[DataUpdateCoordinator[Lyric]]):
self._location = location
self._mac_id = device.macID
self._update_thermostat = coordinator.data.update_thermostat
self._update_fan = coordinator.data.update_fan
@property
def unique_id(self) -> str:

View File

@ -14,6 +14,9 @@ import voluptuous as vol
from homeassistant.components.climate import (
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
FAN_AUTO,
FAN_DIFFUSE,
FAN_ON,
ClimateEntity,
ClimateEntityDescription,
ClimateEntityFeature,
@ -67,6 +70,10 @@ LYRIC_HVAC_MODE_HEAT = "Heat"
LYRIC_HVAC_MODE_COOL = "Cool"
LYRIC_HVAC_MODE_HEAT_COOL = "Auto"
LYRIC_FAN_MODE_ON = "On"
LYRIC_FAN_MODE_AUTO = "Auto"
LYRIC_FAN_MODE_DIFFUSE = "Circulate"
LYRIC_HVAC_MODES = {
HVACMode.OFF: LYRIC_HVAC_MODE_OFF,
HVACMode.HEAT: LYRIC_HVAC_MODE_HEAT,
@ -81,6 +88,18 @@ HVAC_MODES = {
LYRIC_HVAC_MODE_HEAT_COOL: HVACMode.HEAT_COOL,
}
LYRIC_FAN_MODES = {
FAN_ON: LYRIC_FAN_MODE_ON,
FAN_AUTO: LYRIC_FAN_MODE_AUTO,
FAN_DIFFUSE: LYRIC_FAN_MODE_DIFFUSE,
}
FAN_MODES = {
LYRIC_FAN_MODE_ON: FAN_ON,
LYRIC_FAN_MODE_AUTO: FAN_AUTO,
LYRIC_FAN_MODE_DIFFUSE: FAN_DIFFUSE,
}
HVAC_ACTIONS = {
LYRIC_HVAC_ACTION_OFF: HVACAction.OFF,
LYRIC_HVAC_ACTION_HEAT: HVACAction.HEATING,
@ -179,6 +198,25 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity):
):
self._attr_hvac_modes.append(HVACMode.HEAT_COOL)
# Setup supported features
if device.changeableValues.thermostatSetpointStatus:
self._attr_supported_features = SUPPORT_FLAGS_LCC
else:
self._attr_supported_features = SUPPORT_FLAGS_TCC
# Setup supported fan modes
if device_fan_modes := device.settings.attributes.get("fan", {}).get(
"allowedModes"
):
self._attr_fan_modes = [
FAN_MODES[device_fan_mode]
for device_fan_mode in device_fan_modes
if device_fan_mode in FAN_MODES
]
self._attr_supported_features = (
self._attr_supported_features | ClimateEntityFeature.FAN_MODE
)
super().__init__(
coordinator,
location,
@ -187,13 +225,6 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity):
)
self.entity_description = description
@property
def supported_features(self) -> ClimateEntityFeature:
"""Return the list of supported features."""
if self.device.changeableValues.thermostatSetpointStatus:
return SUPPORT_FLAGS_LCC
return SUPPORT_FLAGS_TCC
@property
def current_temperature(self) -> float | None:
"""Return the current temperature."""
@ -268,6 +299,16 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity):
return device.maxHeatSetpoint
return device.maxCoolSetpoint
@property
def fan_mode(self) -> str | None:
"""Return current fan mode."""
device = self.device
return FAN_MODES.get(
device.settings.attributes.get("fan", {})
.get("changeableValues", {})
.get("mode")
)
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
if self.hvac_mode == HVACMode.OFF:
@ -390,3 +431,20 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity):
except LYRIC_EXCEPTIONS as exception:
_LOGGER.error(exception)
await self.coordinator.async_refresh()
async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set fan mode."""
_LOGGER.debug("Set fan mode: %s", fan_mode)
try:
_LOGGER.debug("Fan mode passed to lyric: %s", LYRIC_FAN_MODES[fan_mode])
await self._update_fan(
self.location, self.device, mode=LYRIC_FAN_MODES[fan_mode]
)
except LYRIC_EXCEPTIONS as exception:
_LOGGER.error(exception)
except KeyError:
_LOGGER.error(
"The fan mode requested does not have a corresponding mode in lyric: %s",
fan_mode,
)
await self.coordinator.async_refresh()