Use climate enums in melissa (#70690)
This commit is contained in:
parent
661152527d
commit
a7bb74e151
2 changed files with 46 additions and 57 deletions
|
@ -3,18 +3,14 @@ from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature
|
from homeassistant.components.climate import ClimateEntity
|
||||||
from homeassistant.components.climate.const import (
|
from homeassistant.components.climate.const import (
|
||||||
FAN_AUTO,
|
FAN_AUTO,
|
||||||
FAN_HIGH,
|
FAN_HIGH,
|
||||||
FAN_LOW,
|
FAN_LOW,
|
||||||
FAN_MEDIUM,
|
FAN_MEDIUM,
|
||||||
HVAC_MODE_AUTO,
|
ClimateEntityFeature,
|
||||||
HVAC_MODE_COOL,
|
HVACMode,
|
||||||
HVAC_MODE_DRY,
|
|
||||||
HVAC_MODE_FAN_ONLY,
|
|
||||||
HVAC_MODE_HEAT,
|
|
||||||
HVAC_MODE_OFF,
|
|
||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_TEMPERATURE, PRECISION_WHOLE, TEMP_CELSIUS
|
from homeassistant.const import ATTR_TEMPERATURE, PRECISION_WHOLE, TEMP_CELSIUS
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
@ -26,11 +22,11 @@ from . import DATA_MELISSA
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
OP_MODES = [
|
OP_MODES = [
|
||||||
HVAC_MODE_HEAT,
|
HVACMode.HEAT,
|
||||||
HVAC_MODE_COOL,
|
HVACMode.COOL,
|
||||||
HVAC_MODE_DRY,
|
HVACMode.DRY,
|
||||||
HVAC_MODE_FAN_ONLY,
|
HVACMode.FAN_ONLY,
|
||||||
HVAC_MODE_OFF,
|
HVACMode.OFF,
|
||||||
]
|
]
|
||||||
|
|
||||||
FAN_MODES = [FAN_AUTO, FAN_HIGH, FAN_MEDIUM, FAN_LOW]
|
FAN_MODES = [FAN_AUTO, FAN_HIGH, FAN_MEDIUM, FAN_LOW]
|
||||||
|
@ -58,6 +54,7 @@ async def async_setup_platform(
|
||||||
class MelissaClimate(ClimateEntity):
|
class MelissaClimate(ClimateEntity):
|
||||||
"""Representation of a Melissa Climate device."""
|
"""Representation of a Melissa Climate device."""
|
||||||
|
|
||||||
|
_attr_hvac_modes = OP_MODES
|
||||||
_attr_supported_features = (
|
_attr_supported_features = (
|
||||||
ClimateEntityFeature.FAN_MODE | ClimateEntityFeature.TARGET_TEMPERATURE
|
ClimateEntityFeature.FAN_MODE | ClimateEntityFeature.TARGET_TEMPERATURE
|
||||||
)
|
)
|
||||||
|
@ -100,7 +97,7 @@ class MelissaClimate(ClimateEntity):
|
||||||
return PRECISION_WHOLE
|
return PRECISION_WHOLE
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hvac_mode(self):
|
def hvac_mode(self) -> HVACMode | None:
|
||||||
"""Return the current operation mode."""
|
"""Return the current operation mode."""
|
||||||
if self._cur_settings is None:
|
if self._cur_settings is None:
|
||||||
return None
|
return None
|
||||||
|
@ -111,15 +108,10 @@ class MelissaClimate(ClimateEntity):
|
||||||
)
|
)
|
||||||
|
|
||||||
if not is_on:
|
if not is_on:
|
||||||
return HVAC_MODE_OFF
|
return HVACMode.OFF
|
||||||
|
|
||||||
return self.melissa_op_to_hass(self._cur_settings[self._api.MODE])
|
return self.melissa_op_to_hass(self._cur_settings[self._api.MODE])
|
||||||
|
|
||||||
@property
|
|
||||||
def hvac_modes(self):
|
|
||||||
"""Return the list of available operation modes."""
|
|
||||||
return OP_MODES
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def fan_modes(self):
|
def fan_modes(self):
|
||||||
"""List of available fan modes."""
|
"""List of available fan modes."""
|
||||||
|
@ -157,9 +149,9 @@ class MelissaClimate(ClimateEntity):
|
||||||
melissa_fan_mode = self.hass_fan_to_melissa(fan_mode)
|
melissa_fan_mode = self.hass_fan_to_melissa(fan_mode)
|
||||||
await self.async_send({self._api.FAN: melissa_fan_mode})
|
await self.async_send({self._api.FAN: melissa_fan_mode})
|
||||||
|
|
||||||
async def async_set_hvac_mode(self, hvac_mode):
|
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
|
||||||
"""Set operation mode."""
|
"""Set operation mode."""
|
||||||
if hvac_mode == HVAC_MODE_OFF:
|
if hvac_mode == HVACMode.OFF:
|
||||||
await self.async_send({self._api.STATE: self._api.STATE_OFF})
|
await self.async_send({self._api.STATE: self._api.STATE_OFF})
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -195,20 +187,20 @@ class MelissaClimate(ClimateEntity):
|
||||||
def melissa_op_to_hass(self, mode):
|
def melissa_op_to_hass(self, mode):
|
||||||
"""Translate Melissa modes to hass states."""
|
"""Translate Melissa modes to hass states."""
|
||||||
if mode == self._api.MODE_HEAT:
|
if mode == self._api.MODE_HEAT:
|
||||||
return HVAC_MODE_HEAT
|
return HVACMode.HEAT
|
||||||
if mode == self._api.MODE_COOL:
|
if mode == self._api.MODE_COOL:
|
||||||
return HVAC_MODE_COOL
|
return HVACMode.COOL
|
||||||
if mode == self._api.MODE_DRY:
|
if mode == self._api.MODE_DRY:
|
||||||
return HVAC_MODE_DRY
|
return HVACMode.DRY
|
||||||
if mode == self._api.MODE_FAN:
|
if mode == self._api.MODE_FAN:
|
||||||
return HVAC_MODE_FAN_ONLY
|
return HVACMode.FAN_ONLY
|
||||||
_LOGGER.warning("Operation mode %s could not be mapped to hass", mode)
|
_LOGGER.warning("Operation mode %s could not be mapped to hass", mode)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def melissa_fan_to_hass(self, fan):
|
def melissa_fan_to_hass(self, fan):
|
||||||
"""Translate Melissa fan modes to hass modes."""
|
"""Translate Melissa fan modes to hass modes."""
|
||||||
if fan == self._api.FAN_AUTO:
|
if fan == self._api.FAN_AUTO:
|
||||||
return HVAC_MODE_AUTO
|
return FAN_AUTO
|
||||||
if fan == self._api.FAN_LOW:
|
if fan == self._api.FAN_LOW:
|
||||||
return FAN_LOW
|
return FAN_LOW
|
||||||
if fan == self._api.FAN_MEDIUM:
|
if fan == self._api.FAN_MEDIUM:
|
||||||
|
@ -220,19 +212,19 @@ class MelissaClimate(ClimateEntity):
|
||||||
|
|
||||||
def hass_mode_to_melissa(self, mode):
|
def hass_mode_to_melissa(self, mode):
|
||||||
"""Translate hass states to melissa modes."""
|
"""Translate hass states to melissa modes."""
|
||||||
if mode == HVAC_MODE_HEAT:
|
if mode == HVACMode.HEAT:
|
||||||
return self._api.MODE_HEAT
|
return self._api.MODE_HEAT
|
||||||
if mode == HVAC_MODE_COOL:
|
if mode == HVACMode.COOL:
|
||||||
return self._api.MODE_COOL
|
return self._api.MODE_COOL
|
||||||
if mode == HVAC_MODE_DRY:
|
if mode == HVACMode.DRY:
|
||||||
return self._api.MODE_DRY
|
return self._api.MODE_DRY
|
||||||
if mode == HVAC_MODE_FAN_ONLY:
|
if mode == HVACMode.FAN_ONLY:
|
||||||
return self._api.MODE_FAN
|
return self._api.MODE_FAN
|
||||||
_LOGGER.warning("Melissa have no setting for %s mode", mode)
|
_LOGGER.warning("Melissa have no setting for %s mode", mode)
|
||||||
|
|
||||||
def hass_fan_to_melissa(self, fan):
|
def hass_fan_to_melissa(self, fan):
|
||||||
"""Translate hass fan modes to melissa modes."""
|
"""Translate hass fan modes to melissa modes."""
|
||||||
if fan == HVAC_MODE_AUTO:
|
if fan == FAN_AUTO:
|
||||||
return self._api.FAN_AUTO
|
return self._api.FAN_AUTO
|
||||||
if fan == FAN_LOW:
|
if fan == FAN_LOW:
|
||||||
return self._api.FAN_LOW
|
return self._api.FAN_LOW
|
||||||
|
|
|
@ -6,13 +6,8 @@ from homeassistant.components.climate.const import (
|
||||||
FAN_HIGH,
|
FAN_HIGH,
|
||||||
FAN_LOW,
|
FAN_LOW,
|
||||||
FAN_MEDIUM,
|
FAN_MEDIUM,
|
||||||
HVAC_MODE_COOL,
|
ClimateEntityFeature,
|
||||||
HVAC_MODE_DRY,
|
HVACMode,
|
||||||
HVAC_MODE_FAN_ONLY,
|
|
||||||
HVAC_MODE_HEAT,
|
|
||||||
HVAC_MODE_OFF,
|
|
||||||
SUPPORT_FAN_MODE,
|
|
||||||
SUPPORT_TARGET_TEMPERATURE,
|
|
||||||
)
|
)
|
||||||
from homeassistant.components.melissa import DATA_MELISSA, climate as melissa
|
from homeassistant.components.melissa import DATA_MELISSA, climate as melissa
|
||||||
from homeassistant.components.melissa.climate import MelissaClimate
|
from homeassistant.components.melissa.climate import MelissaClimate
|
||||||
|
@ -137,7 +132,7 @@ async def test_current_operation(hass):
|
||||||
device = (await api.async_fetch_devices())[_SERIAL]
|
device = (await api.async_fetch_devices())[_SERIAL]
|
||||||
thermostat = MelissaClimate(api, _SERIAL, device)
|
thermostat = MelissaClimate(api, _SERIAL, device)
|
||||||
await thermostat.async_update()
|
await thermostat.async_update()
|
||||||
assert thermostat.state == HVAC_MODE_HEAT
|
assert thermostat.state == HVACMode.HEAT
|
||||||
|
|
||||||
thermostat._cur_settings = None
|
thermostat._cur_settings = None
|
||||||
assert thermostat.hvac_action is None
|
assert thermostat.hvac_action is None
|
||||||
|
@ -150,11 +145,11 @@ async def test_operation_list(hass):
|
||||||
device = (await api.async_fetch_devices())[_SERIAL]
|
device = (await api.async_fetch_devices())[_SERIAL]
|
||||||
thermostat = MelissaClimate(api, _SERIAL, device)
|
thermostat = MelissaClimate(api, _SERIAL, device)
|
||||||
assert [
|
assert [
|
||||||
HVAC_MODE_HEAT,
|
HVACMode.HEAT,
|
||||||
HVAC_MODE_COOL,
|
HVACMode.COOL,
|
||||||
HVAC_MODE_DRY,
|
HVACMode.DRY,
|
||||||
HVAC_MODE_FAN_ONLY,
|
HVACMode.FAN_ONLY,
|
||||||
HVAC_MODE_OFF,
|
HVACMode.OFF,
|
||||||
] == thermostat.hvac_modes
|
] == thermostat.hvac_modes
|
||||||
|
|
||||||
|
|
||||||
|
@ -187,7 +182,7 @@ async def test_state(hass):
|
||||||
device = (await api.async_fetch_devices())[_SERIAL]
|
device = (await api.async_fetch_devices())[_SERIAL]
|
||||||
thermostat = MelissaClimate(api, _SERIAL, device)
|
thermostat = MelissaClimate(api, _SERIAL, device)
|
||||||
await thermostat.async_update()
|
await thermostat.async_update()
|
||||||
assert thermostat.state == HVAC_MODE_HEAT
|
assert thermostat.state == HVACMode.HEAT
|
||||||
|
|
||||||
thermostat._cur_settings = None
|
thermostat._cur_settings = None
|
||||||
assert thermostat.state is None
|
assert thermostat.state is None
|
||||||
|
@ -226,7 +221,9 @@ async def test_supported_features(hass):
|
||||||
api = melissa_mock()
|
api = melissa_mock()
|
||||||
device = (await api.async_fetch_devices())[_SERIAL]
|
device = (await api.async_fetch_devices())[_SERIAL]
|
||||||
thermostat = MelissaClimate(api, _SERIAL, device)
|
thermostat = MelissaClimate(api, _SERIAL, device)
|
||||||
features = SUPPORT_TARGET_TEMPERATURE | SUPPORT_FAN_MODE
|
features = (
|
||||||
|
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
|
||||||
|
)
|
||||||
assert thermostat.supported_features == features
|
assert thermostat.supported_features == features
|
||||||
|
|
||||||
|
|
||||||
|
@ -262,9 +259,9 @@ async def test_set_operation_mode(hass):
|
||||||
thermostat = MelissaClimate(api, _SERIAL, device)
|
thermostat = MelissaClimate(api, _SERIAL, device)
|
||||||
await thermostat.async_update()
|
await thermostat.async_update()
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
await thermostat.async_set_hvac_mode(HVAC_MODE_COOL)
|
await thermostat.async_set_hvac_mode(HVACMode.COOL)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert thermostat.hvac_mode == HVAC_MODE_COOL
|
assert thermostat.hvac_mode == HVACMode.COOL
|
||||||
|
|
||||||
|
|
||||||
async def test_send(hass):
|
async def test_send(hass):
|
||||||
|
@ -296,7 +293,7 @@ async def test_update(hass):
|
||||||
thermostat = MelissaClimate(api, _SERIAL, device)
|
thermostat = MelissaClimate(api, _SERIAL, device)
|
||||||
await thermostat.async_update()
|
await thermostat.async_update()
|
||||||
assert thermostat.fan_mode == FAN_LOW
|
assert thermostat.fan_mode == FAN_LOW
|
||||||
assert thermostat.state == HVAC_MODE_HEAT
|
assert thermostat.state == HVACMode.HEAT
|
||||||
api.async_status = AsyncMock(side_effect=KeyError("boom"))
|
api.async_status = AsyncMock(side_effect=KeyError("boom"))
|
||||||
await thermostat.async_update()
|
await thermostat.async_update()
|
||||||
mocked_warning.assert_called_once_with(
|
mocked_warning.assert_called_once_with(
|
||||||
|
@ -310,10 +307,10 @@ async def test_melissa_op_to_hass(hass):
|
||||||
api = melissa_mock()
|
api = melissa_mock()
|
||||||
device = (await api.async_fetch_devices())[_SERIAL]
|
device = (await api.async_fetch_devices())[_SERIAL]
|
||||||
thermostat = MelissaClimate(api, _SERIAL, device)
|
thermostat = MelissaClimate(api, _SERIAL, device)
|
||||||
assert thermostat.melissa_op_to_hass(1) == HVAC_MODE_FAN_ONLY
|
assert thermostat.melissa_op_to_hass(1) == HVACMode.FAN_ONLY
|
||||||
assert thermostat.melissa_op_to_hass(2) == HVAC_MODE_HEAT
|
assert thermostat.melissa_op_to_hass(2) == HVACMode.HEAT
|
||||||
assert thermostat.melissa_op_to_hass(3) == HVAC_MODE_COOL
|
assert thermostat.melissa_op_to_hass(3) == HVACMode.COOL
|
||||||
assert thermostat.melissa_op_to_hass(4) == HVAC_MODE_DRY
|
assert thermostat.melissa_op_to_hass(4) == HVACMode.DRY
|
||||||
assert thermostat.melissa_op_to_hass(5) is None
|
assert thermostat.melissa_op_to_hass(5) is None
|
||||||
|
|
||||||
|
|
||||||
|
@ -338,10 +335,10 @@ async def test_hass_mode_to_melissa(hass):
|
||||||
api = melissa_mock()
|
api = melissa_mock()
|
||||||
device = (await api.async_fetch_devices())[_SERIAL]
|
device = (await api.async_fetch_devices())[_SERIAL]
|
||||||
thermostat = MelissaClimate(api, _SERIAL, device)
|
thermostat = MelissaClimate(api, _SERIAL, device)
|
||||||
assert thermostat.hass_mode_to_melissa(HVAC_MODE_FAN_ONLY) == 1
|
assert thermostat.hass_mode_to_melissa(HVACMode.FAN_ONLY) == 1
|
||||||
assert thermostat.hass_mode_to_melissa(HVAC_MODE_HEAT) == 2
|
assert thermostat.hass_mode_to_melissa(HVACMode.HEAT) == 2
|
||||||
assert thermostat.hass_mode_to_melissa(HVAC_MODE_COOL) == 3
|
assert thermostat.hass_mode_to_melissa(HVACMode.COOL) == 3
|
||||||
assert thermostat.hass_mode_to_melissa(HVAC_MODE_DRY) == 4
|
assert thermostat.hass_mode_to_melissa(HVACMode.DRY) == 4
|
||||||
thermostat.hass_mode_to_melissa("test")
|
thermostat.hass_mode_to_melissa("test")
|
||||||
mocked_warning.assert_called_once_with(
|
mocked_warning.assert_called_once_with(
|
||||||
"Melissa have no setting for %s mode", "test"
|
"Melissa have no setting for %s mode", "test"
|
||||||
|
|
Loading…
Add table
Reference in a new issue