From 650115e7e2d6fd2c0ad675e0c250f6e9ac720ce1 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Mon, 27 Sep 2021 21:36:49 +0200 Subject: [PATCH] Improved config structure. --- components/xiaomi_bslamp2/__init__.py | 65 ++++++++++--------- .../xiaomi_bslamp2/binary_sensor/__init__.py | 2 +- components/xiaomi_bslamp2/light/__init__.py | 6 +- components/xiaomi_bslamp2/output/__init__.py | 7 +- components/xiaomi_bslamp2/sensor/__init__.py | 2 +- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/components/xiaomi_bslamp2/__init__.py b/components/xiaomi_bslamp2/__init__.py index d62bf47..c479b2b 100644 --- a/components/xiaomi_bslamp2/__init__.py +++ b/components/xiaomi_bslamp2/__init__.py @@ -5,23 +5,17 @@ from esphome.components.ledc.output import LEDCOutput from esphome.components.gpio.output import GPIOBinaryOutput from esphome.components.i2c import I2CBus, I2CDevice from esphome.const import ( - CONF_RED, CONF_GREEN, CONF_BLUE, CONF_WHITE, - CONF_TRIGGER_PIN, CONF_SDA, CONF_SCL, CONF_ADDRESS + CONF_LIGHT, CONF_RED, CONF_GREEN, CONF_BLUE, CONF_WHITE, + CONF_I2C, CONF_ADDRESS, CONF_TRIGGER_PIN, CONF_ID ) -# TODO subsection in config for leds and front_panel. - CODEOWNERS = ["@mmakaay"] CONF_MASTER1 = "master1" CONF_MASTER2 = "master2" -CONF_FP_I2C = "fp_i2c" -CONF_FP_I2C_ADDRESS = "fp_i2c_address" -CONF_FP_TRIGGER_PIN = "fp_trigger_pin" +CONF_FRONT_PANEL = "front_panel" CONF_LIGHT_HAL_ID = "light_hal_id" CONF_FRONT_PANEL_HAL_ID = "front_panel_hal_id" -CONF_ON_BRIGHTNESS = "on_brightness" -CONF_LEDS = "leds" xiaomi_ns = cg.esphome_ns.namespace("xiaomi") bslamp2_ns = xiaomi_ns.namespace("bslamp2") @@ -48,40 +42,47 @@ FRONT_PANEL_LED_OPTIONS = { CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({ # RGBWW Light - cv.GenerateID(CONF_LIGHT_HAL_ID): cv.declare_id(LightHAL), - cv.Required(CONF_RED): cv.use_id(LEDCOutput), - cv.Required(CONF_GREEN): cv.use_id(LEDCOutput), - cv.Required(CONF_BLUE): cv.use_id(LEDCOutput), - cv.Required(CONF_WHITE): cv.use_id(LEDCOutput), - cv.Required(CONF_MASTER1): cv.use_id(GPIOBinaryOutput), - cv.Required(CONF_MASTER2): cv.use_id(GPIOBinaryOutput), - + cv.Required(CONF_LIGHT): cv.Schema( + { + cv.GenerateID(CONF_LIGHT_HAL_ID): cv.declare_id(LightHAL), + cv.Required(CONF_RED): cv.use_id(LEDCOutput), + cv.Required(CONF_GREEN): cv.use_id(LEDCOutput), + cv.Required(CONF_BLUE): cv.use_id(LEDCOutput), + cv.Required(CONF_WHITE): cv.use_id(LEDCOutput), + cv.Required(CONF_MASTER1): cv.use_id(GPIOBinaryOutput), + cv.Required(CONF_MASTER2): cv.use_id(GPIOBinaryOutput), + } + ), # Front panel I2C - cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.declare_id(FrontPanelHAL), - cv.Required(CONF_FP_I2C): cv.use_id(I2CBus), - cv.Required(CONF_FP_I2C_ADDRESS): cv.i2c_address, - cv.Required(CONF_FP_TRIGGER_PIN): cv.All(pins.internal_gpio_input_pin_schema) + cv.Required(CONF_FRONT_PANEL): cv.Schema( + { + cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.declare_id(FrontPanelHAL), + cv.Required(CONF_I2C): cv.use_id(I2CBus), + cv.Required(CONF_ADDRESS): cv.i2c_address, + cv.Required(CONF_TRIGGER_PIN): cv.All(pins.internal_gpio_input_pin_schema) + } + ), }) async def make_light_hal(config): - light_hal = cg.new_Pvariable(config[CONF_LIGHT_HAL_ID]) + light_hal = cg.new_Pvariable(config[CONF_LIGHT][CONF_LIGHT_HAL_ID]) await cg.register_component(light_hal, config) - cg.add(light_hal.set_red_pin(await cg.get_variable(config[CONF_RED]))) - cg.add(light_hal.set_green_pin(await cg.get_variable(config[CONF_GREEN]))) - cg.add(light_hal.set_blue_pin(await cg.get_variable(config[CONF_BLUE]))) - cg.add(light_hal.set_white_pin(await cg.get_variable(config[CONF_WHITE]))) - cg.add(light_hal.set_master1_pin(await cg.get_variable(config[CONF_MASTER1]))) - cg.add(light_hal.set_master2_pin(await cg.get_variable(config[CONF_MASTER2]))) + cg.add(light_hal.set_red_pin(await cg.get_variable(config[CONF_LIGHT][CONF_RED]))) + cg.add(light_hal.set_green_pin(await cg.get_variable(config[CONF_LIGHT][CONF_GREEN]))) + cg.add(light_hal.set_blue_pin(await cg.get_variable(config[CONF_LIGHT][CONF_BLUE]))) + cg.add(light_hal.set_white_pin(await cg.get_variable(config[CONF_LIGHT][CONF_WHITE]))) + cg.add(light_hal.set_master1_pin(await cg.get_variable(config[CONF_LIGHT][CONF_MASTER1]))) + cg.add(light_hal.set_master2_pin(await cg.get_variable(config[CONF_LIGHT][CONF_MASTER2]))) async def make_front_panel_hal(config): - fp_hal = cg.new_Pvariable(config[CONF_FRONT_PANEL_HAL_ID]) + fp_hal = cg.new_Pvariable(config[CONF_FRONT_PANEL][CONF_FRONT_PANEL_HAL_ID]) await cg.register_component(fp_hal, config) - trigger_pin = await cg.gpio_pin_expression(config[CONF_FP_TRIGGER_PIN]) + trigger_pin = await cg.gpio_pin_expression(config[CONF_FRONT_PANEL][CONF_TRIGGER_PIN]) cg.add(fp_hal.set_trigger_pin(trigger_pin)) - fp_i2c_var = await cg.get_variable(config[CONF_FP_I2C]) + fp_i2c_var = await cg.get_variable(config[CONF_FRONT_PANEL][CONF_I2C]) cg.add(fp_hal.set_i2c_bus(fp_i2c_var)) - cg.add(fp_hal.set_i2c_address(config[CONF_FP_I2C_ADDRESS])) + cg.add(fp_hal.set_i2c_address(config[CONF_FRONT_PANEL][CONF_ADDRESS])) async def to_code(config): await make_light_hal(config) diff --git a/components/xiaomi_bslamp2/binary_sensor/__init__.py b/components/xiaomi_bslamp2/binary_sensor/__init__.py index 839f13a..2125bab 100644 --- a/components/xiaomi_bslamp2/binary_sensor/__init__.py +++ b/components/xiaomi_bslamp2/binary_sensor/__init__.py @@ -8,7 +8,7 @@ from .. import ( CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL ) -AUTO_LOAD = ["xiaomi_bslamp2"] +DEPENDENCIES = ["xiaomi_bslamp2"] CONF_PART = "part" diff --git a/components/xiaomi_bslamp2/light/__init__.py b/components/xiaomi_bslamp2/light/__init__.py index d57e6d6..0217d64 100644 --- a/components/xiaomi_bslamp2/light/__init__.py +++ b/components/xiaomi_bslamp2/light/__init__.py @@ -10,17 +10,15 @@ from esphome.const import ( ) from .. import bslamp2_ns, CODEOWNERS, CONF_LIGHT_HAL_ID, LightHAL -AUTO_LOAD = ["xiaomi_bslamp2"] +DEPENDENCIES = ["xiaomi_bslamp2"] -CONF_MASTER1 = "master1" -CONF_MASTER2 = "master2" CONF_ON_BRIGHTNESS = "on_brightness" CONF_PRESET_ID = "preset_id" CONF_PRESETS_ID = "presets_id" +CONF_PRESET = "preset" CONF_PRESETS = "presets" CONF_NEXT = "next" CONF_GROUP = "group" -CONF_PRESET = "preset" MIRED_MIN = 153 MIRED_MAX = 588 diff --git a/components/xiaomi_bslamp2/output/__init__.py b/components/xiaomi_bslamp2/output/__init__.py index 0e13e9e..3f51a53 100644 --- a/components/xiaomi_bslamp2/output/__init__.py +++ b/components/xiaomi_bslamp2/output/__init__.py @@ -5,11 +5,12 @@ from esphome.const import CONF_ID, CONF_LEVEL from esphome import automation from .. import ( bslamp2_ns, CODEOWNERS, - CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL, FRONT_PANEL_LED_OPTIONS, - CONF_LEDS + CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL, FRONT_PANEL_LED_OPTIONS ) -AUTO_LOAD = ["xiaomi_bslamp2"] +CONF_LEDS = "leds" + +DEPENDENCIES = ["xiaomi_bslamp2"] XiaomiBslamp2FrontPanelOutput = bslamp2_ns.class_( "XiaomiBslamp2FrontPanelOutput", output.FloatOutput, cg.Component) diff --git a/components/xiaomi_bslamp2/sensor/__init__.py b/components/xiaomi_bslamp2/sensor/__init__.py index 039ae65..fd95a29 100644 --- a/components/xiaomi_bslamp2/sensor/__init__.py +++ b/components/xiaomi_bslamp2/sensor/__init__.py @@ -7,7 +7,7 @@ from .. import ( CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL ) -AUTO_LOAD = ["xiaomi_bslamp2"] +DEPENDENCIES = ["xiaomi_bslamp2"] XiaomiBslamp2SliderSensor = bslamp2_ns.class_( "XiaomiBslamp2SliderSensor", sensor.Sensor, cg.Component)