Skip to content

Commit

Permalink
Merge pull request #1287 from hmmbob/patch-1
Browse files Browse the repository at this point in the history
Replace deprecated constants to fix warnings in HA 2024.1
  • Loading branch information
AlexxIT authored Jan 4, 2024
2 parents 56f2055 + b1dc8a4 commit ca812ac
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 97 deletions.
83 changes: 38 additions & 45 deletions custom_components/sonoff/climate.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_HEAT,
HVAC_MODE_HEAT_COOL,
HVAC_MODE_OFF,
SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE_RANGE,
ClimateEntityFeature,
HVACMode,
)
from homeassistant.const import TEMP_CELSIUS
from homeassistant.const import UnitOfTemperature

from .core.const import DOMAIN
from .core.entity import XEntity
Expand All @@ -33,13 +26,13 @@ class XClimateTH(XEntity, ClimateEntity):

_attr_entity_registry_enabled_default = False
_attr_hvac_mode = None
_attr_hvac_modes = [HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL, HVAC_MODE_DRY]
_attr_hvac_modes = [HVACMode.OFF, HVACMode.HEAT, HVACMode.COOL, HVACMode.DRY]
_attr_max_temp = 99
_attr_min_temp = 1
_attr_supported_features = SUPPORT_TARGET_TEMPERATURE_RANGE
_attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
_attr_target_temperature_high = None
_attr_target_temperature_low = None
_attr_temperature_unit = TEMP_CELSIUS
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_target_temperature_step = 1

heat: bool = None
Expand All @@ -53,16 +46,16 @@ def set_state(self, params: dict):
self._attr_target_temperature_low = float(lo["targetLow"])

if params["deviceType"] == "normal":
self._attr_hvac_mode = HVAC_MODE_OFF
self._attr_hvac_mode = HVACMode.OFF
elif params["deviceType"] == "humidity":
self._attr_hvac_mode = HVAC_MODE_DRY
self._attr_hvac_mode = HVACMode.DRY
elif self.is_aux_heat:
self._attr_hvac_mode = HVAC_MODE_HEAT
self._attr_hvac_mode = HVACMode.HEAT
else:
self._attr_hvac_mode = HVAC_MODE_COOL
self._attr_hvac_mode = HVACMode.COOL

try:
if self.hvac_mode != HVAC_MODE_DRY:
if self.hvac_mode != HVACMode.DRY:
value = float(params.get("currentTemperature") or params["temperature"])
value = round(value, 1)
else:
Expand All @@ -84,19 +77,19 @@ def get_targets(self, heat: bool) -> list:
]

async def async_set_hvac_mode(self, hvac_mode: str) -> None:
if hvac_mode == HVAC_MODE_HEAT:
if hvac_mode == HVACMode.HEAT:
params = {
"mainSwitch": "on",
"deviceType": "temperature",
"targets": self.get_targets(True),
}
elif hvac_mode == HVAC_MODE_COOL:
elif hvac_mode == HVACMode.COOL:
params = {
"mainSwitch": "on",
"deviceType": "temperature",
"targets": self.get_targets(False),
}
elif hvac_mode == HVAC_MODE_DRY:
elif hvac_mode == HVACMode.DRY:
params = {
"mainSwitch": "on",
"deviceType": "humidity",
Expand All @@ -116,13 +109,13 @@ async def async_set_temperature(
heat = self.is_aux_heat
if hvac_mode is None:
params = {}
elif hvac_mode == HVAC_MODE_HEAT:
elif hvac_mode == HVACMode.HEAT:
heat = True
params = {"mainSwitch": "on", "deviceType": "temperature"}
elif hvac_mode == HVAC_MODE_COOL:
elif hvac_mode == HVACMode.COOL:
heat = False
params = {"mainSwitch": "on", "deviceType": "temperature"}
elif hvac_mode == HVAC_MODE_DRY:
elif hvac_mode == HVACMode.DRY:
params = {"mainSwitch": "on", "deviceType": "humidity"}
else:
params = {"mainSwitch": "off", "deviceType": "normal"}
Expand All @@ -147,11 +140,11 @@ class XClimateNS(XEntity, ClimateEntity):
params = {"ATCEnable", "ATCMode", "temperature", "tempCorrection"}

_attr_entity_registry_enabled_default = False
_attr_hvac_modes = [HVAC_MODE_OFF, HVAC_MODE_HEAT_COOL, HVAC_MODE_AUTO]
_attr_hvac_modes = [HVACMode.OFF, HVACMode.HEAT_COOL, HVACMode.AUTO]
_attr_max_temp = 31
_attr_min_temp = 16
_attr_supported_features = SUPPORT_TARGET_TEMPERATURE
_attr_temperature_unit = TEMP_CELSIUS
_attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_target_temperature_step = 1

def set_state(self, params: dict):
Expand All @@ -161,18 +154,18 @@ def set_state(self, params: dict):

if "HMI_ATCDevice" in params and "etype" in params["HMI_ATCDevice"]:
if cache["HMI_ATCDevice"]["etype"] == "cold":
self._attr_hvac_modes[1] = HVAC_MODE_COOL
self._attr_hvac_modes[1] = HVACMode.COOL
else:
self._attr_hvac_modes[1] = HVAC_MODE_HEAT
self._attr_hvac_modes[1] = HVACMode.HEAT

if "ATCEnable" in params or "ATCMode" in params:
if cache["ATCEnable"]:
if cache["ATCMode"]:
self.set_hvac_attr(HVAC_MODE_AUTO)
self.set_hvac_attr(HVACMode.AUTO)
else:
self.set_hvac_attr(self._attr_hvac_modes[1])
else:
self.set_hvac_attr(HVAC_MODE_OFF)
self.set_hvac_attr(HVACMode.OFF)

if "ATCExpect0" in params:
self._attr_target_temperature = cache["ATCExpect0"]
Expand All @@ -188,25 +181,25 @@ def set_state(self, params: dict):
pass

def set_hvac_attr(self, hvac_mode: str) -> None:
if hvac_mode == HVAC_MODE_AUTO:
if hvac_mode == HVACMode.AUTO:
self._attr_hvac_mode = hvac_mode
self._attr_supported_features = 0
elif hvac_mode == HVAC_MODE_OFF:
elif hvac_mode == HVACMode.OFF:
self._attr_hvac_mode = hvac_mode
self._attr_supported_features = SUPPORT_TARGET_TEMPERATURE
elif hvac_mode in (HVAC_MODE_COOL, HVAC_MODE_HEAT, HVAC_MODE_HEAT_COOL):
self._attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
elif hvac_mode in (HVACMode.COOL, HVACMode.HEAT, HVACMode.HEAT_COOL):
self._attr_hvac_mode = self._attr_hvac_modes[1]
self._attr_supported_features = SUPPORT_TARGET_TEMPERATURE
self._attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE

@staticmethod
def get_params(hvac_mode: str) -> dict:
if hvac_mode == HVAC_MODE_AUTO:
if hvac_mode == HVACMode.AUTO:
return {"ATCEnable": 1, "ATCMode": 1}
elif hvac_mode in (HVAC_MODE_COOL, HVAC_MODE_HEAT):
elif hvac_mode in (HVACMode.COOL, HVACMode.HEAT):
return {"ATCEnable": 1, "ATCMode": 0}
elif hvac_mode == HVAC_MODE_HEAT_COOL:
elif hvac_mode == HVACMode.HEAT_COOL:
return {"ATCEnable": 1} # async_turn_on
elif hvac_mode == HVAC_MODE_OFF:
elif hvac_mode == HVACMode.OFF:
return {"ATCEnable": 0}
else:
return {}
Expand All @@ -233,12 +226,12 @@ class XThermostat(XEntity, ClimateEntity):
params = {"switch", "targetTemp", "temperature", "workMode", "workState"}

# @bwp91 https://github.com/AlexxIT/SonoffLAN/issues/358
_attr_hvac_modes = [HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_AUTO]
_attr_hvac_modes = [HVACMode.OFF, HVACMode.HEAT, HVACMode.AUTO]
_attr_max_temp = 45
_attr_min_temp = 5
_attr_preset_modes = ["manual", "programmed", "economical"]
_attr_supported_features = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE
_attr_temperature_unit = TEMP_CELSIUS
_attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_target_temperature_step = 0.5

def set_state(self, params: dict):
Expand All @@ -250,7 +243,7 @@ def set_state(self, params: dict):
# workState: 1=heating, 2=auto
self._attr_hvac_mode = self.hvac_modes[cache["workState"]]
else:
self._attr_hvac_mode = HVAC_MODE_OFF
self._attr_hvac_mode = HVACMode.OFF

if "workMode" in params:
self._attr_preset_mode = self.preset_modes[params["workMode"] - 1]
Expand Down Expand Up @@ -278,7 +271,7 @@ async def async_set_temperature(
) -> None:
if hvac_mode is None:
params = {}
elif hvac_mode is HVAC_MODE_OFF:
elif hvac_mode is HVACMode.OFF:
params = {"switch": "off"}
else:
i = self.hvac_modes.index(hvac_mode)
Expand Down
7 changes: 5 additions & 2 deletions custom_components/sonoff/core/backward.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Why Hass v2021.12 minimal?
"""Why Hass v2023.1 minimal?
- v2021.7 - new Entity attributes style
- v2021.8 - new electric unit_of_measurement
Expand All @@ -7,7 +7,10 @@
- v2021.12 - new ButtonEntity
- v2021.12 - new FanEntity percentage logic
- v2021.12 - new SensorDeviceClass, SensorStateClass classes
- v2022.5 - new LightEntity ColorMode and LightEntityFeature
- v2022.11 - new UnitOfEnergy, UnitOfPower, UnitOfTemperature
- V2023.1 - new UnitOfElectricCurrent, UnitOfElectricPotential
"""
from homeassistant.const import MAJOR_VERSION, MINOR_VERSION

hass_version_supported = (MAJOR_VERSION, MINOR_VERSION) >= (2021, 12)
hass_version_supported = (MAJOR_VERSION, MINOR_VERSION) >= (2023, 1)
5 changes: 2 additions & 3 deletions custom_components/sonoff/fan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from homeassistant.components.fan import (
SUPPORT_PRESET_MODE,
SUPPORT_SET_SPEED,
FanEntity,
FanEntityFeature,
)

from .core.const import DOMAIN
Expand Down Expand Up @@ -29,7 +28,7 @@ async def async_setup_entry(hass, config_entry, add_entities):
class XFan(XEntity, FanEntity):
params = {"switches", "fan"}
_attr_speed_count = 3
_attr_supported_features = SUPPORT_SET_SPEED | SUPPORT_PRESET_MODE
_attr_supported_features = FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE
_attr_preset_modes = [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]

def set_state(self, params: dict):
Expand Down
Loading

0 comments on commit ca812ac

Please sign in to comment.