From 38790793ddb350efa19df82b63d15e341a915640 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Tue, 13 May 2025 08:53:46 +1200 Subject: [PATCH] [opentherm] Update to use schema methods (#8756) --- .../opentherm/binary_sensor/__init__.py | 6 ++-- .../components/opentherm/number/__init__.py | 31 +++++++------------ .../components/opentherm/switch/__init__.py | 8 ++--- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/esphome/components/opentherm/binary_sensor/__init__.py b/esphome/components/opentherm/binary_sensor/__init__.py index ce5a701a53..35228228ea 100644 --- a/esphome/components/opentherm/binary_sensor/__init__.py +++ b/esphome/components/opentherm/binary_sensor/__init__.py @@ -11,10 +11,8 @@ COMPONENT_TYPE = const.BINARY_SENSOR def get_entity_validation_schema(entity: schema.BinarySensorSchema) -> cv.Schema: return binary_sensor.binary_sensor_schema( - device_class=( - entity.device_class or cv.UNDEFINED # pylint: disable=protected-access - ), - icon=(entity.icon or cv.UNDEFINED), # pylint: disable=protected-access + device_class=(entity.device_class or cv.UNDEFINED), + icon=(entity.icon or cv.UNDEFINED), ) diff --git a/esphome/components/opentherm/number/__init__.py b/esphome/components/opentherm/number/__init__.py index 00aa62483c..a65864647a 100644 --- a/esphome/components/opentherm/number/__init__.py +++ b/esphome/components/opentherm/number/__init__.py @@ -3,13 +3,7 @@ from typing import Any import esphome.codegen as cg from esphome.components import number import esphome.config_validation as cv -from esphome.const import ( - CONF_ID, - CONF_INITIAL_VALUE, - CONF_RESTORE_VALUE, - CONF_STEP, - CONF_UNIT_OF_MEASUREMENT, -) +from esphome.const import CONF_INITIAL_VALUE, CONF_RESTORE_VALUE, CONF_STEP from .. import const, generate, input, schema, validate @@ -22,33 +16,30 @@ OpenthermNumber = generate.opentherm_ns.class_( async def new_openthermnumber(config: dict[str, Any]) -> cg.Pvariable: - var = cg.new_Pvariable(config[CONF_ID]) - await cg.register_component(var, config) - await number.register_number( - var, + var = await number.new_number( config, min_value=config[input.CONF_min_value], max_value=config[input.CONF_max_value], step=config[input.CONF_step], ) + await cg.register_component(var, config) input.generate_setters(var, config) - if CONF_INITIAL_VALUE in config: - cg.add(var.set_initial_value(config[CONF_INITIAL_VALUE])) - if CONF_RESTORE_VALUE in config: - cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE])) + if (initial_value := config.get(CONF_INITIAL_VALUE, None)) is not None: + cg.add(var.set_initial_value(initial_value)) + if (restore_value := config.get(CONF_RESTORE_VALUE, None)) is not None: + cg.add(var.set_restore_value(restore_value)) return var def get_entity_validation_schema(entity: schema.InputSchema) -> cv.Schema: return ( - number.NUMBER_SCHEMA.extend( + number.number_schema( + OpenthermNumber, unit_of_measurement=entity.unit_of_measurement + ) + .extend( { - cv.GenerateID(): cv.declare_id(OpenthermNumber), - cv.Optional( - CONF_UNIT_OF_MEASUREMENT, entity.unit_of_measurement - ): cv.string_strict, cv.Optional(CONF_STEP, entity.step): cv.float_, cv.Optional(CONF_INITIAL_VALUE): cv.float_, cv.Optional(CONF_RESTORE_VALUE): cv.boolean, diff --git a/esphome/components/opentherm/switch/__init__.py b/esphome/components/opentherm/switch/__init__.py index ead086d24b..f8f09b3967 100644 --- a/esphome/components/opentherm/switch/__init__.py +++ b/esphome/components/opentherm/switch/__init__.py @@ -3,7 +3,6 @@ from typing import Any import esphome.codegen as cg from esphome.components import switch import esphome.config_validation as cv -from esphome.const import CONF_ID from .. import const, generate, schema, validate @@ -16,15 +15,14 @@ OpenthermSwitch = generate.opentherm_ns.class_( async def new_openthermswitch(config: dict[str, Any]) -> cg.Pvariable: - var = cg.new_Pvariable(config[CONF_ID]) + var = await switch.new_switch(config) await cg.register_component(var, config) - await switch.register_switch(var, config) return var def get_entity_validation_schema(entity: schema.SwitchSchema) -> cv.Schema: - return switch.SWITCH_SCHEMA.extend( - {cv.GenerateID(): cv.declare_id(OpenthermSwitch)} + return switch.switch_schema( + OpenthermSwitch, default_restore_mode=entity.default_mode ).extend(cv.COMPONENT_SCHEMA)