|
| 1 | +import esphome.codegen as cg |
| 2 | +from esphome.components import button |
| 3 | +import esphome.config_validation as cv |
| 4 | +from esphome.const import ( |
| 5 | + CONF_FACTORY_RESET, |
| 6 | + CONF_RESTART, |
| 7 | + DEVICE_CLASS_RESTART, |
| 8 | + ENTITY_CATEGORY_CONFIG, |
| 9 | + ENTITY_CATEGORY_DIAGNOSTIC, |
| 10 | + ICON_DATABASE, |
| 11 | + ICON_PULSE, |
| 12 | + ICON_RESTART, |
| 13 | + ICON_RESTART_ALERT, |
| 14 | +) |
| 15 | + |
| 16 | +from .. import CONF_LD2412_ID, LD2412_ns, LD2412Component |
| 17 | + |
| 18 | +FactoryResetButton = LD2412_ns.class_("FactoryResetButton", button.Button) |
| 19 | +QueryButton = LD2412_ns.class_("QueryButton", button.Button) |
| 20 | +RestartButton = LD2412_ns.class_("RestartButton", button.Button) |
| 21 | +StartDynamicBackgroundCorrectionButton = LD2412_ns.class_( |
| 22 | + "StartDynamicBackgroundCorrectionButton", button.Button |
| 23 | +) |
| 24 | + |
| 25 | +CONF_QUERY_PARAMS = "query_params" |
| 26 | +CONF_START_DYNAMIC_BACKGROUND_CORRECTION = "start_dynamic_background_correction" |
| 27 | + |
| 28 | +CONFIG_SCHEMA = { |
| 29 | + cv.GenerateID(CONF_LD2412_ID): cv.use_id(LD2412Component), |
| 30 | + cv.Optional(CONF_FACTORY_RESET): button.button_schema( |
| 31 | + FactoryResetButton, |
| 32 | + device_class=DEVICE_CLASS_RESTART, |
| 33 | + entity_category=ENTITY_CATEGORY_CONFIG, |
| 34 | + icon=ICON_RESTART_ALERT, |
| 35 | + ), |
| 36 | + cv.Optional(CONF_QUERY_PARAMS): button.button_schema( |
| 37 | + QueryButton, |
| 38 | + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, |
| 39 | + icon=ICON_DATABASE, |
| 40 | + ), |
| 41 | + cv.Optional(CONF_RESTART): button.button_schema( |
| 42 | + RestartButton, |
| 43 | + device_class=DEVICE_CLASS_RESTART, |
| 44 | + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, |
| 45 | + icon=ICON_RESTART, |
| 46 | + ), |
| 47 | + cv.Optional(CONF_START_DYNAMIC_BACKGROUND_CORRECTION): button.button_schema( |
| 48 | + StartDynamicBackgroundCorrectionButton, |
| 49 | + entity_category=ENTITY_CATEGORY_CONFIG, |
| 50 | + icon=ICON_PULSE, |
| 51 | + ), |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +async def to_code(config): |
| 56 | + LD2412_component = await cg.get_variable(config[CONF_LD2412_ID]) |
| 57 | + if factory_reset_config := config.get(CONF_FACTORY_RESET): |
| 58 | + b = await button.new_button(factory_reset_config) |
| 59 | + await cg.register_parented(b, config[CONF_LD2412_ID]) |
| 60 | + cg.add(LD2412_component.set_factory_reset_button(b)) |
| 61 | + if query_params_config := config.get(CONF_QUERY_PARAMS): |
| 62 | + b = await button.new_button(query_params_config) |
| 63 | + await cg.register_parented(b, config[CONF_LD2412_ID]) |
| 64 | + cg.add(LD2412_component.set_query_button(b)) |
| 65 | + if restart_config := config.get(CONF_RESTART): |
| 66 | + b = await button.new_button(restart_config) |
| 67 | + await cg.register_parented(b, config[CONF_LD2412_ID]) |
| 68 | + cg.add(LD2412_component.set_restart_button(b)) |
| 69 | + if start_dynamic_background_correction_config := config.get( |
| 70 | + CONF_START_DYNAMIC_BACKGROUND_CORRECTION |
| 71 | + ): |
| 72 | + b = await button.new_button(start_dynamic_background_correction_config) |
| 73 | + await cg.register_parented(b, config[CONF_LD2412_ID]) |
| 74 | + cg.add(LD2412_component.set_start_dynamic_background_correction_button(b)) |
0 commit comments