Bump pycoolmasternet-async and add coolmaster swing mode (#82809)

* Add filter and error code support to CoolMastetNet

* Create separate entities

* coolmaster swing_mode support

* Changed default to False

* Raise HomeAssistantError

* Add tests for init and climate

* Fixed bad merge

* Catch only ValueError
This commit is contained in:
amitfin 2023-01-03 20:21:11 +02:00 committed by GitHub
parent ca7384f96e
commit b5664f9eaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 430 additions and 37 deletions

View file

@ -1,7 +1,11 @@
"""CoolMasterNet platform to control of CoolMasterNet Climate Devices."""
from __future__ import annotations
import logging
from typing import Any
from pycoolmasternet_async import SWING_MODES
from homeassistant.components.climate import (
ClimateEntity,
ClimateEntityFeature,
@ -10,6 +14,7 @@ from homeassistant.components.climate import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_SUPPORTED_MODES, DATA_COORDINATOR, DATA_INFO, DOMAIN
@ -48,10 +53,6 @@ async def async_setup_entry(
class CoolmasterClimate(CoolmasterEntity, ClimateEntity):
"""Representation of a coolmaster climate device."""
_attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
)
def __init__(self, coordinator, unit_id, info, supported_modes):
"""Initialize the climate device."""
super().__init__(coordinator, unit_id, info)
@ -67,6 +68,16 @@ class CoolmasterClimate(CoolmasterEntity, ClimateEntity):
"""Return the name of the climate device."""
return self.unique_id
@property
def supported_features(self) -> ClimateEntityFeature:
"""Return the list of supported features."""
supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
)
if self.swing_mode:
supported_features |= ClimateEntityFeature.SWING_MODE
return supported_features
@property
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
@ -109,6 +120,16 @@ class CoolmasterClimate(CoolmasterEntity, ClimateEntity):
"""Return the list of available fan modes."""
return FAN_MODES
@property
def swing_mode(self) -> str | None:
"""Return the swing mode setting."""
return self._unit.swing
@property
def swing_modes(self) -> list[str] | None:
"""Return swing modes if supported."""
return SWING_MODES if self.swing_mode is not None else None
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperatures."""
if (temp := kwargs.get(ATTR_TEMPERATURE)) is not None:
@ -122,6 +143,15 @@ class CoolmasterClimate(CoolmasterEntity, ClimateEntity):
self._unit = await self._unit.set_fan_speed(fan_mode)
self.async_write_ha_state()
async def async_set_swing_mode(self, swing_mode: str) -> None:
"""Set new swing mode."""
_LOGGER.debug("Setting swing mode of %s to %s", self.unique_id, swing_mode)
try:
self._unit = await self._unit.set_swing(swing_mode)
except ValueError as error:
raise HomeAssistantError(error) from error
self.async_write_ha_state()
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new operation mode."""
_LOGGER.debug("Setting operation mode of %s to %s", self.unique_id, hvac_mode)