From f7af51b92c9d3fee019f2ff5abda5880cf18061e Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Mon, 24 Jun 2024 10:22:07 +0400 Subject: [PATCH] [haier] climate ID auto generation (#6949) --- esphome/components/haier/binary_sensor/__init__.py | 8 ++++---- esphome/components/haier/button/__init__.py | 2 +- esphome/components/haier/climate.py | 11 +++-------- esphome/components/haier/sensor/__init__.py | 10 +++++----- esphome/components/haier/text_sensor/__init__.py | 8 ++++---- tests/components/haier/test.esp32-c3-ard.yaml | 4 ---- tests/components/haier/test.esp32-c3-idf.yaml | 4 ---- tests/components/haier/test.esp32-idf.yaml | 4 ---- tests/components/haier/test.esp8266-ard.yaml | 4 ---- tests/components/haier/test.rp2040-ard.yaml | 4 ---- 10 files changed, 17 insertions(+), 42 deletions(-) diff --git a/esphome/components/haier/binary_sensor/__init__.py b/esphome/components/haier/binary_sensor/__init__.py index 8e9d5ec578..3a4935b22d 100644 --- a/esphome/components/haier/binary_sensor/__init__.py +++ b/esphome/components/haier/binary_sensor/__init__.py @@ -56,7 +56,7 @@ SENSOR_TYPES = { CONFIG_SCHEMA = cv.Schema( { - cv.Required(CONF_HAIER_ID): cv.use_id(HonClimate), + cv.GenerateID(CONF_HAIER_ID): cv.use_id(HonClimate), } ).extend({cv.Optional(type): schema for type, schema in SENSOR_TYPES.items()}) @@ -64,8 +64,8 @@ CONFIG_SCHEMA = cv.Schema( async def to_code(config): paren = await cg.get_variable(config[CONF_HAIER_ID]) - for type, _ in SENSOR_TYPES.items(): - if conf := config.get(type): + for type_ in SENSOR_TYPES: + if conf := config.get(type_): sens = await binary_sensor.new_binary_sensor(conf) - binary_sensor_type = getattr(BinarySensorTypeEnum, type.upper()) + binary_sensor_type = getattr(BinarySensorTypeEnum, type_.upper()) cg.add(paren.set_sub_binary_sensor(binary_sensor_type, sens)) diff --git a/esphome/components/haier/button/__init__.py b/esphome/components/haier/button/__init__.py index efe6180aaf..745ad95fb6 100644 --- a/esphome/components/haier/button/__init__.py +++ b/esphome/components/haier/button/__init__.py @@ -21,7 +21,7 @@ ICON_SPRAY_BOTTLE = "mdi:spray-bottle" CONFIG_SCHEMA = cv.Schema( { - cv.Required(CONF_HAIER_ID): cv.use_id(HonClimate), + cv.GenerateID(CONF_HAIER_ID): cv.use_id(HonClimate), cv.Optional(CONF_SELF_CLEANING): button.button_schema( SelfCleaningButton, icon=ICON_SPRAY_BOTTLE, diff --git a/esphome/components/haier/climate.py b/esphome/components/haier/climate.py index 1562708a4f..3dcb35708c 100644 --- a/esphome/components/haier/climate.py +++ b/esphome/components/haier/climate.py @@ -183,7 +183,6 @@ BASE_CONFIG_SCHEMA = ( cv.Optional( CONF_SUPPORTED_SWING_MODES, default=[ - "OFF", "VERTICAL", "HORIZONTAL", "BOTH", @@ -211,7 +210,7 @@ CONFIG_SCHEMA = cv.All( ): cv.boolean, cv.Optional( CONF_SUPPORTED_PRESETS, - default=list(["BOOST", "COMFORT"]), # No AWAY by default + default=["BOOST", "COMFORT"], # No AWAY by default ): cv.ensure_list( cv.enum(SUPPORTED_CLIMATE_PRESETS_SMARTAIR2_OPTIONS, upper=True) ), @@ -231,7 +230,7 @@ CONFIG_SCHEMA = cv.All( ): cv.int_range(min=PROTOCOL_CONTROL_PACKET_SIZE, max=50), cv.Optional( CONF_SUPPORTED_PRESETS, - default=list(["BOOST", "ECO", "SLEEP"]), # No AWAY by default + default=["BOOST", "ECO", "SLEEP"], # No AWAY by default ): cv.ensure_list( cv.enum(SUPPORTED_CLIMATE_PRESETS_HON_OPTIONS, upper=True) ), @@ -427,11 +426,7 @@ def _final_validate(config): "No logger component found, logging for Haier protocol is disabled" ) cg.add_build_flag("-DHAIER_LOG_LEVEL=0") - if ( - (CONF_WIFI_SIGNAL in config) - and (config[CONF_WIFI_SIGNAL]) - and CONF_WIFI not in full_config - ): + if config.get(CONF_WIFI_SIGNAL) and CONF_WIFI not in full_config: raise cv.Invalid( f"No WiFi configured, if you want to use haier climate without WiFi add {CONF_WIFI_SIGNAL}: false to climate configuration" ) diff --git a/esphome/components/haier/sensor/__init__.py b/esphome/components/haier/sensor/__init__.py index b2717631e0..23c1d6f008 100644 --- a/esphome/components/haier/sensor/__init__.py +++ b/esphome/components/haier/sensor/__init__.py @@ -137,16 +137,16 @@ SENSOR_TYPES = { CONFIG_SCHEMA = cv.Schema( { - cv.Required(CONF_HAIER_ID): cv.use_id(HonClimate), + cv.GenerateID(CONF_HAIER_ID): cv.use_id(HonClimate), } -).extend({cv.Optional(type): schema for type, schema in SENSOR_TYPES.items()}) +).extend({cv.Optional(type_): schema for type_, schema in SENSOR_TYPES.items()}) async def to_code(config): paren = await cg.get_variable(config[CONF_HAIER_ID]) - for type, _ in SENSOR_TYPES.items(): - if conf := config.get(type): + for type_ in SENSOR_TYPES: + if conf := config.get(type_): sens = await sensor.new_sensor(conf) - sensor_type = getattr(SensorTypeEnum, type.upper()) + sensor_type = getattr(SensorTypeEnum, type_.upper()) cg.add(paren.set_sub_sensor(sensor_type, sens)) diff --git a/esphome/components/haier/text_sensor/__init__.py b/esphome/components/haier/text_sensor/__init__.py index 528b70d83e..d28c5a8c0e 100644 --- a/esphome/components/haier/text_sensor/__init__.py +++ b/esphome/components/haier/text_sensor/__init__.py @@ -39,7 +39,7 @@ TEXT_SENSOR_TYPES = { CONFIG_SCHEMA = cv.Schema( { - cv.Required(CONF_HAIER_ID): cv.use_id(HonClimate), + cv.GenerateID(CONF_HAIER_ID): cv.use_id(HonClimate), } ).extend({cv.Optional(type): schema for type, schema in TEXT_SENSOR_TYPES.items()}) @@ -47,8 +47,8 @@ CONFIG_SCHEMA = cv.Schema( async def to_code(config): paren = await cg.get_variable(config[CONF_HAIER_ID]) - for type, _ in TEXT_SENSOR_TYPES.items(): - if conf := config.get(type): + for type_ in TEXT_SENSOR_TYPES: + if conf := config.get(type_): sens = await text_sensor.new_text_sensor(conf) - text_sensor_type = getattr(TextSensorTypeEnum, type.upper()) + text_sensor_type = getattr(TextSensorTypeEnum, type_.upper()) cg.add(paren.set_sub_text_sensor(text_sensor_type, sens)) diff --git a/tests/components/haier/test.esp32-c3-ard.yaml b/tests/components/haier/test.esp32-c3-ard.yaml index fed573bd1d..0053220669 100644 --- a/tests/components/haier/test.esp32-c3-ard.yaml +++ b/tests/components/haier/test.esp32-c3-ard.yaml @@ -54,7 +54,6 @@ climate: sensor: - platform: haier - haier_id: haier_ac outdoor_temperature: name: Haier outdoor temperature humidity: @@ -80,7 +79,6 @@ sensor: binary_sensor: - platform: haier - haier_id: haier_ac compressor_status: name: Haier Outdoor Compressor Status defrost_status: @@ -96,7 +94,6 @@ binary_sensor: button: - platform: haier - haier_id: haier_ac self_cleaning: name: Haier start self cleaning steri_cleaning: @@ -104,7 +101,6 @@ button: text_sensor: - platform: haier - haier_id: haier_ac appliance_name: name: Haier appliance name cleaning_status: diff --git a/tests/components/haier/test.esp32-c3-idf.yaml b/tests/components/haier/test.esp32-c3-idf.yaml index fed573bd1d..0053220669 100644 --- a/tests/components/haier/test.esp32-c3-idf.yaml +++ b/tests/components/haier/test.esp32-c3-idf.yaml @@ -54,7 +54,6 @@ climate: sensor: - platform: haier - haier_id: haier_ac outdoor_temperature: name: Haier outdoor temperature humidity: @@ -80,7 +79,6 @@ sensor: binary_sensor: - platform: haier - haier_id: haier_ac compressor_status: name: Haier Outdoor Compressor Status defrost_status: @@ -96,7 +94,6 @@ binary_sensor: button: - platform: haier - haier_id: haier_ac self_cleaning: name: Haier start self cleaning steri_cleaning: @@ -104,7 +101,6 @@ button: text_sensor: - platform: haier - haier_id: haier_ac appliance_name: name: Haier appliance name cleaning_status: diff --git a/tests/components/haier/test.esp32-idf.yaml b/tests/components/haier/test.esp32-idf.yaml index efff532d25..54e384f3ce 100644 --- a/tests/components/haier/test.esp32-idf.yaml +++ b/tests/components/haier/test.esp32-idf.yaml @@ -54,7 +54,6 @@ climate: sensor: - platform: haier - haier_id: haier_ac outdoor_temperature: name: Haier outdoor temperature humidity: @@ -80,7 +79,6 @@ sensor: binary_sensor: - platform: haier - haier_id: haier_ac compressor_status: name: Haier Outdoor Compressor Status defrost_status: @@ -96,7 +94,6 @@ binary_sensor: button: - platform: haier - haier_id: haier_ac self_cleaning: name: Haier start self cleaning steri_cleaning: @@ -104,7 +101,6 @@ button: text_sensor: - platform: haier - haier_id: haier_ac appliance_name: name: Haier appliance name cleaning_status: diff --git a/tests/components/haier/test.esp8266-ard.yaml b/tests/components/haier/test.esp8266-ard.yaml index fed573bd1d..0053220669 100644 --- a/tests/components/haier/test.esp8266-ard.yaml +++ b/tests/components/haier/test.esp8266-ard.yaml @@ -54,7 +54,6 @@ climate: sensor: - platform: haier - haier_id: haier_ac outdoor_temperature: name: Haier outdoor temperature humidity: @@ -80,7 +79,6 @@ sensor: binary_sensor: - platform: haier - haier_id: haier_ac compressor_status: name: Haier Outdoor Compressor Status defrost_status: @@ -96,7 +94,6 @@ binary_sensor: button: - platform: haier - haier_id: haier_ac self_cleaning: name: Haier start self cleaning steri_cleaning: @@ -104,7 +101,6 @@ button: text_sensor: - platform: haier - haier_id: haier_ac appliance_name: name: Haier appliance name cleaning_status: diff --git a/tests/components/haier/test.rp2040-ard.yaml b/tests/components/haier/test.rp2040-ard.yaml index fed573bd1d..0053220669 100644 --- a/tests/components/haier/test.rp2040-ard.yaml +++ b/tests/components/haier/test.rp2040-ard.yaml @@ -54,7 +54,6 @@ climate: sensor: - platform: haier - haier_id: haier_ac outdoor_temperature: name: Haier outdoor temperature humidity: @@ -80,7 +79,6 @@ sensor: binary_sensor: - platform: haier - haier_id: haier_ac compressor_status: name: Haier Outdoor Compressor Status defrost_status: @@ -96,7 +94,6 @@ binary_sensor: button: - platform: haier - haier_id: haier_ac self_cleaning: name: Haier start self cleaning steri_cleaning: @@ -104,7 +101,6 @@ button: text_sensor: - platform: haier - haier_id: haier_ac appliance_name: name: Haier appliance name cleaning_status: