2024/02/18更新:2024.2版本新增了ClimateEntityFeature: TURN_ON, TURN_OFF二個flag(詳情請見官方公告New entity features in Climate entity),更新後SmartIR又有新的錯誤訊息出現,這裡一併修正。
Home Assistant在2024.1版本出現某一些constant將在未來的2025.1版本中被取代的錯誤訊息,不過我在SmartIR只有用到climate相關的功能,總共有9條相關的錯誤訊息,fan和media_player有沒有需要修改就不知道了,錯誤訊息如下
HVAC_MODE_OFF was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use HVACMode.OFF instead, please report it to the author of the 'smartir' custom integration
HVAC_MODE_HEAT was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use HVACMode.HEAT instead, please report it to the author of the 'smartir' custom integration
HVAC_MODE_COOL was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use HVACMode.COOL instead, please report it to the author of the 'smartir' custom integration
HVAC_MODE_DRY was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use HVACMode.DRY instead, please report it to the author of the 'smartir' custom integration
HVAC_MODE_FAN_ONLY was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use HVACMode.FAN_ONLY instead, please report it to the author of the 'smartir' custom integration
HVAC_MODE_AUTO was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use HVACMode.AUTO instead, please report it to the author of the 'smartir' custom integration
SUPPORT_TARGET_TEMPERATURE was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use ClimateEntityFeature.TARGET_TEMPERATURE instead, please report it to the author of the 'smartir' custom integration
SUPPORT_FAN_MODE was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use ClimateEntityFeature.FAN_MODE instead, please report it to the author of the 'smartir' custom integration
SUPPORT_SWING_MODE was used from smartir, this is a deprecated constant which will be removed in HA Core 2025.1. Use ClimateEntityFeature.SWING_MODE instead, please report it to the author of the 'smartir' custom integration
2024.2版本出現的錯誤訊息如下
Entity None (<class 'custom_components.smartir.climate.SmartIRClimate'>) does not set ClimateEntityFeature.TURN_OFF but implements the turn_off method. Please report it to the author of the 'smartir' custom integration
Entity None (<class 'custom_components.smartir.climate.SmartIRClimate'>) does not set ClimateEntityFeature.TURN_ON but implements the turn_on method. Please report it to the author of the 'smartir' custom integration
Entity None (<class 'custom_components.smartir.climate.SmartIRClimate'>) implements HVACMode(s): off, auto, cool, heat, dry and therefore implicitly supports the turn_on/turn_off methods without setting the proper ClimateEntityFeature. Please report it to the author of the 'smartir' custom integration
找到climate.py開始修改,新舊程式碼執行diff的結果如下,有很多地方需要修改
--- climate_origin.py 2023-12-27 23:53:00.749348878 +0800
+++ climate.py 2024-02-18 17:31:42.051116500 +0800
@@ -7,10 +7,8 @@
from homeassistant.components.climate import ClimateEntity, PLATFORM_SCHEMA
from homeassistant.components.climate.const import (
- HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL,
- HVAC_MODE_DRY, HVAC_MODE_FAN_ONLY, HVAC_MODE_AUTO,
- SUPPORT_TARGET_TEMPERATURE, SUPPORT_FAN_MODE,
- SUPPORT_SWING_MODE, HVAC_MODES, ATTR_HVAC_MODE)
+ HVACMode, ClimateEntityFeature,
+ HVAC_MODES, ATTR_HVAC_MODE)
from homeassistant.const import (
CONF_NAME, STATE_ON, STATE_OFF, STATE_UNKNOWN, STATE_UNAVAILABLE, ATTR_TEMPERATURE,
PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE)
@@ -36,8 +34,10 @@
CONF_POWER_SENSOR_RESTORE_STATE = 'power_sensor_restore_state'
SUPPORT_FLAGS = (
- SUPPORT_TARGET_TEMPERATURE |
- SUPPORT_FAN_MODE
+ ClimateEntityFeature.TARGET_TEMPERATURE
+ | ClimateEntityFeature.FAN_MODE
+ | ClimateEntityFeature.TURN_ON
+ | ClimateEntityFeature.TURN_OFF
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
@@ -96,6 +96,8 @@
)])
class SmartIRClimate(ClimateEntity, RestoreEntity):
+ _enable_turn_on_off_backwards_compatibility = False
+
def __init__(self, hass, config, device_data):
_LOGGER.debug(f"SmartIRClimate init started for device {config.get(CONF_NAME)} supported models {device_data['supportedModels']}")
self.hass = hass
@@ -119,13 +121,13 @@
valid_hvac_modes = [x for x in device_data['operationModes'] if x in HVAC_MODES]
- self._operation_modes = [HVAC_MODE_OFF] + valid_hvac_modes
+ self._operation_modes = [HVACMode.OFF] + valid_hvac_modes
self._fan_modes = device_data['fanModes']
self._swing_modes = device_data.get('swingModes')
self._commands = device_data['commands']
self._target_temperature = self._min_temperature
- self._hvac_mode = HVAC_MODE_OFF
+ self._hvac_mode = HVACMode.OFF
self._current_fan_mode = self._fan_modes[0]
self._current_swing_mode = None
self._last_on_operation = None
@@ -140,7 +142,7 @@
self._support_swing = False
if self._swing_modes:
- self._support_flags = self._support_flags | SUPPORT_SWING_MODE
+ self._support_flags = self._support_flags | ClimateEntityFeature.SWING_MODE
self._current_swing_mode = self._swing_modes[0]
self._support_swing = True
@@ -204,9 +206,9 @@
@property
def state(self):
"""Return the current state."""
- if self.hvac_mode != HVAC_MODE_OFF:
+ if self.hvac_mode != HVACMode.OFF:
return self.hvac_mode
- return HVAC_MODE_OFF
+ return HVACMode.OFF
@property
def temperature_unit(self):
@@ -316,7 +318,7 @@
await self.async_set_hvac_mode(hvac_mode)
return
- if not self._hvac_mode.lower() == HVAC_MODE_OFF:
+ if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()
self.async_write_ha_state()
@@ -325,7 +327,7 @@
"""Set operation mode."""
self._hvac_mode = hvac_mode
- if not hvac_mode == HVAC_MODE_OFF:
+ if not hvac_mode == HVACMode.OFF:
self._last_on_operation = hvac_mode
await self.send_command()
@@ -335,7 +337,7 @@
"""Set fan mode."""
self._current_fan_mode = fan_mode
- if not self._hvac_mode.lower() == HVAC_MODE_OFF:
+ if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()
self.async_write_ha_state()
@@ -343,13 +345,13 @@
"""Set swing mode."""
self._current_swing_mode = swing_mode
- if not self._hvac_mode.lower() == HVAC_MODE_OFF:
+ if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()
self.async_write_ha_state()
async def async_turn_off(self):
"""Turn off."""
- await self.async_set_hvac_mode(HVAC_MODE_OFF)
+ await self.async_set_hvac_mode(HVACMode.OFF)
async def async_turn_on(self):
"""Turn on."""
@@ -367,7 +369,7 @@
swing_mode = self._current_swing_mode
target_temperature = '{0:g}'.format(self._target_temperature)
- if operation_mode.lower() == HVAC_MODE_OFF:
+ if operation_mode.lower() == HVACMode.OFF:
await self._controller.send(self._commands['off'])
return
@@ -409,7 +411,7 @@
if old_state is not None and new_state.state == old_state.state:
return
- if new_state.state == STATE_ON and self._hvac_mode == HVAC_MODE_OFF:
+ if new_state.state == STATE_ON and self._hvac_mode == HVACMode.OFF:
self._on_by_remote = True
if self._power_sensor_restore_state == True and self._last_on_operation is not None:
self._hvac_mode = self._last_on_operation
@@ -420,8 +422,8 @@
if new_state.state == STATE_OFF:
self._on_by_remote = False
- if self._hvac_mode != HVAC_MODE_OFF:
- self._hvac_mode = HVAC_MODE_OFF
+ if self._hvac_mode != HVACMode.OFF:
+ self._hvac_mode = HVACMode.OFF
self.async_write_ha_state()
@callback
修改過後的程式碼如下
import asyncio
import json
import logging
import os.path
import voluptuous as vol
from homeassistant.components.climate import ClimateEntity, PLATFORM_SCHEMA
from homeassistant.components.climate.const import (
HVACMode, ClimateEntityFeature,
HVAC_MODES, ATTR_HVAC_MODE)
from homeassistant.const import (
CONF_NAME, STATE_ON, STATE_OFF, STATE_UNKNOWN, STATE_UNAVAILABLE, ATTR_TEMPERATURE,
PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE)
from homeassistant.core import callback
from homeassistant.helpers.event import async_track_state_change
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from . import COMPONENT_ABS_DIR, Helper
from .controller import get_controller
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "SmartIR Climate"
DEFAULT_DELAY = 0.5
CONF_UNIQUE_ID = 'unique_id'
CONF_DEVICE_CODE = 'device_code'
CONF_CONTROLLER_DATA = "controller_data"
CONF_DELAY = "delay"
CONF_TEMPERATURE_SENSOR = 'temperature_sensor'
CONF_HUMIDITY_SENSOR = 'humidity_sensor'
CONF_POWER_SENSOR = 'power_sensor'
CONF_POWER_SENSOR_RESTORE_STATE = 'power_sensor_restore_state'
SUPPORT_FLAGS = (
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.FAN_MODE
| ClimateEntityFeature.TURN_ON
| ClimateEntityFeature.TURN_OFF
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Required(CONF_DEVICE_CODE): cv.positive_int,
vol.Required(CONF_CONTROLLER_DATA): cv.string,
vol.Optional(CONF_DELAY, default=DEFAULT_DELAY): cv.positive_float,
vol.Optional(CONF_TEMPERATURE_SENSOR): cv.entity_id,
vol.Optional(CONF_HUMIDITY_SENSOR): cv.entity_id,
vol.Optional(CONF_POWER_SENSOR): cv.entity_id,
vol.Optional(CONF_POWER_SENSOR_RESTORE_STATE, default=False): cv.boolean
})
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the IR Climate platform."""
_LOGGER.debug("Setting up the startir platform")
device_code = config.get(CONF_DEVICE_CODE)
device_files_subdir = os.path.join('codes', 'climate')
device_files_absdir = os.path.join(COMPONENT_ABS_DIR, device_files_subdir)
if not os.path.isdir(device_files_absdir):
os.makedirs(device_files_absdir)
device_json_filename = str(device_code) + '.json'
device_json_path = os.path.join(device_files_absdir, device_json_filename)
if not os.path.exists(device_json_path):
_LOGGER.warning("Couldn't find the device Json file. The component will " \
"try to download it from the GitHub repo.")
try:
codes_source = ("https://raw.githubusercontent.com/"
"smartHomeHub/SmartIR/master/"
"codes/climate/{}.json")
await Helper.downloader(codes_source.format(device_code), device_json_path)
except Exception:
_LOGGER.error("There was an error while downloading the device Json file. " \
"Please check your internet connection or if the device code " \
"exists on GitHub. If the problem still exists please " \
"place the file manually in the proper directory.")
return
with open(device_json_path) as j:
try:
_LOGGER.debug(f"loading json file {device_json_path}")
device_data = json.load(j)
_LOGGER.debug(f"{device_json_path} file loaded")
except Exception:
_LOGGER.error("The device Json file is invalid")
return
async_add_entities([SmartIRClimate(
hass, config, device_data
)])
class SmartIRClimate(ClimateEntity, RestoreEntity):
_enable_turn_on_off_backwards_compatibility = False
def __init__(self, hass, config, device_data):
_LOGGER.debug(f"SmartIRClimate init started for device {config.get(CONF_NAME)} supported models {device_data['supportedModels']}")
self.hass = hass
self._unique_id = config.get(CONF_UNIQUE_ID)
self._name = config.get(CONF_NAME)
self._device_code = config.get(CONF_DEVICE_CODE)
self._controller_data = config.get(CONF_CONTROLLER_DATA)
self._delay = config.get(CONF_DELAY)
self._temperature_sensor = config.get(CONF_TEMPERATURE_SENSOR)
self._humidity_sensor = config.get(CONF_HUMIDITY_SENSOR)
self._power_sensor = config.get(CONF_POWER_SENSOR)
self._power_sensor_restore_state = config.get(CONF_POWER_SENSOR_RESTORE_STATE)
self._manufacturer = device_data['manufacturer']
self._supported_models = device_data['supportedModels']
self._supported_controller = device_data['supportedController']
self._commands_encoding = device_data['commandsEncoding']
self._min_temperature = device_data['minTemperature']
self._max_temperature = device_data['maxTemperature']
self._precision = device_data['precision']
valid_hvac_modes = [x for x in device_data['operationModes'] if x in HVAC_MODES]
self._operation_modes = [HVACMode.OFF] + valid_hvac_modes
self._fan_modes = device_data['fanModes']
self._swing_modes = device_data.get('swingModes')
self._commands = device_data['commands']
self._target_temperature = self._min_temperature
self._hvac_mode = HVACMode.OFF
self._current_fan_mode = self._fan_modes[0]
self._current_swing_mode = None
self._last_on_operation = None
self._current_temperature = None
self._current_humidity = None
self._unit = hass.config.units.temperature_unit
#Supported features
self._support_flags = SUPPORT_FLAGS
self._support_swing = False
if self._swing_modes:
self._support_flags = self._support_flags | ClimateEntityFeature.SWING_MODE
self._current_swing_mode = self._swing_modes[0]
self._support_swing = True
self._temp_lock = asyncio.Lock()
self._on_by_remote = False
#Init the IR/RF controller
self._controller = get_controller(
self.hass,
self._supported_controller,
self._commands_encoding,
self._controller_data,
self._delay)
async def async_added_to_hass(self):
"""Run when entity about to be added."""
await super().async_added_to_hass()
_LOGGER.debug(f"async_added_to_hass {self} {self.name} {self.supported_features}")
last_state = await self.async_get_last_state()
if last_state is not None:
self._hvac_mode = last_state.state
self._current_fan_mode = last_state.attributes['fan_mode']
self._current_swing_mode = last_state.attributes.get('swing_mode')
self._target_temperature = last_state.attributes['temperature']
if 'last_on_operation' in last_state.attributes:
self._last_on_operation = last_state.attributes['last_on_operation']
if self._temperature_sensor:
async_track_state_change(self.hass, self._temperature_sensor,
self._async_temp_sensor_changed)
temp_sensor_state = self.hass.states.get(self._temperature_sensor)
if temp_sensor_state and temp_sensor_state.state != STATE_UNKNOWN:
self._async_update_temp(temp_sensor_state)
if self._humidity_sensor:
async_track_state_change(self.hass, self._humidity_sensor,
self._async_humidity_sensor_changed)
humidity_sensor_state = self.hass.states.get(self._humidity_sensor)
if humidity_sensor_state and humidity_sensor_state.state != STATE_UNKNOWN:
self._async_update_humidity(humidity_sensor_state)
if self._power_sensor:
async_track_state_change(self.hass, self._power_sensor,
self._async_power_sensor_changed)
@property
def unique_id(self):
"""Return a unique ID."""
return self._unique_id
@property
def name(self):
"""Return the name of the climate device."""
return self._name
@property
def state(self):
"""Return the current state."""
if self.hvac_mode != HVACMode.OFF:
return self.hvac_mode
return HVACMode.OFF
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return self._unit
@property
def min_temp(self):
"""Return the polling state."""
return self._min_temperature
@property
def max_temp(self):
"""Return the polling state."""
return self._max_temperature
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temperature
@property
def target_temperature_step(self):
"""Return the supported step of target temperature."""
return self._precision
@property
def hvac_modes(self):
"""Return the list of available operation modes."""
return self._operation_modes
@property
def hvac_mode(self):
"""Return hvac mode ie. heat, cool."""
return self._hvac_mode
@property
def last_on_operation(self):
"""Return the last non-idle operation ie. heat, cool."""
return self._last_on_operation
@property
def fan_modes(self):
"""Return the list of available fan modes."""
return self._fan_modes
@property
def fan_mode(self):
"""Return the fan setting."""
return self._current_fan_mode
@property
def swing_modes(self):
"""Return the swing modes currently supported for this device."""
return self._swing_modes
@property
def swing_mode(self):
"""Return the current swing mode."""
return self._current_swing_mode
@property
def current_temperature(self):
"""Return the current temperature."""
return self._current_temperature
@property
def current_humidity(self):
"""Return the current humidity."""
return self._current_humidity
@property
def supported_features(self):
"""Return the list of supported features."""
return self._support_flags
@property
def extra_state_attributes(self):
"""Platform specific attributes."""
return {
'last_on_operation': self._last_on_operation,
'device_code': self._device_code,
'manufacturer': self._manufacturer,
'supported_models': self._supported_models,
'supported_controller': self._supported_controller,
'commands_encoding': self._commands_encoding
}
async def async_set_temperature(self, **kwargs):
"""Set new target temperatures."""
hvac_mode = kwargs.get(ATTR_HVAC_MODE)
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature is None:
return
if temperature < self._min_temperature or temperature > self._max_temperature:
_LOGGER.warning('The temperature value is out of min/max range')
return
if self._precision == PRECISION_WHOLE:
self._target_temperature = round(temperature)
else:
self._target_temperature = round(temperature, 1)
if hvac_mode:
await self.async_set_hvac_mode(hvac_mode)
return
if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()
self.async_write_ha_state()
async def async_set_hvac_mode(self, hvac_mode):
"""Set operation mode."""
self._hvac_mode = hvac_mode
if not hvac_mode == HVACMode.OFF:
self._last_on_operation = hvac_mode
await self.send_command()
self.async_write_ha_state()
async def async_set_fan_mode(self, fan_mode):
"""Set fan mode."""
self._current_fan_mode = fan_mode
if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()
self.async_write_ha_state()
async def async_set_swing_mode(self, swing_mode):
"""Set swing mode."""
self._current_swing_mode = swing_mode
if not self._hvac_mode.lower() == HVACMode.OFF:
await self.send_command()
self.async_write_ha_state()
async def async_turn_off(self):
"""Turn off."""
await self.async_set_hvac_mode(HVACMode.OFF)
async def async_turn_on(self):
"""Turn on."""
if self._last_on_operation is not None:
await self.async_set_hvac_mode(self._last_on_operation)
else:
await self.async_set_hvac_mode(self._operation_modes[1])
async def send_command(self):
async with self._temp_lock:
try:
self._on_by_remote = False
operation_mode = self._hvac_mode
fan_mode = self._current_fan_mode
swing_mode = self._current_swing_mode
target_temperature = '{0:g}'.format(self._target_temperature)
if operation_mode.lower() == HVACMode.OFF:
await self._controller.send(self._commands['off'])
return
if 'on' in self._commands:
await self._controller.send(self._commands['on'])
await asyncio.sleep(self._delay)
if self._support_swing == True:
await self._controller.send(
self._commands[operation_mode][fan_mode][swing_mode][target_temperature])
else:
await self._controller.send(
self._commands[operation_mode][fan_mode][target_temperature])
except Exception as e:
_LOGGER.exception(e)
async def _async_temp_sensor_changed(self, entity_id, old_state, new_state):
"""Handle temperature sensor changes."""
if new_state is None:
return
self._async_update_temp(new_state)
self.async_write_ha_state()
async def _async_humidity_sensor_changed(self, entity_id, old_state, new_state):
"""Handle humidity sensor changes."""
if new_state is None:
return
self._async_update_humidity(new_state)
self.async_write_ha_state()
async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
"""Handle power sensor changes."""
if new_state is None:
return
if old_state is not None and new_state.state == old_state.state:
return
if new_state.state == STATE_ON and self._hvac_mode == HVACMode.OFF:
self._on_by_remote = True
if self._power_sensor_restore_state == True and self._last_on_operation is not None:
self._hvac_mode = self._last_on_operation
else:
self._hvac_mode = STATE_ON
self.async_write_ha_state()
if new_state.state == STATE_OFF:
self._on_by_remote = False
if self._hvac_mode != HVACMode.OFF:
self._hvac_mode = HVACMode.OFF
self.async_write_ha_state()
@callback
def _async_update_temp(self, state):
"""Update thermostat with latest state from temperature sensor."""
try:
if state.state != STATE_UNKNOWN and state.state != STATE_UNAVAILABLE:
self._current_temperature = float(state.state)
except ValueError as ex:
_LOGGER.error("Unable to update from temperature sensor: %s", ex)
@callback
def _async_update_humidity(self, state):
"""Update thermostat with latest state from humidity sensor."""
try:
if state.state != STATE_UNKNOWN and state.state != STATE_UNAVAILABLE:
self._current_humidity = float(state.state)
except ValueError as ex:
_LOGGER.error("Unable to update from humidity sensor: %s", ex)