|
| 1 | +""" |
| 2 | +/* |
| 3 | + * Copyright (C) 2024 Konnected Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + """ |
| 19 | + |
| 20 | +import esphome.codegen as cg |
| 21 | +import esphome.config_validation as cv |
| 22 | +import voluptuous as vol |
| 23 | +from esphome import pins |
| 24 | +from esphome.const import CONF_ID |
| 25 | + |
| 26 | +DEPENDENCIES = ["preferences"] |
| 27 | +MULTI_CONF = True |
| 28 | + |
| 29 | +konnectedgdo_ns = cg.esphome_ns.namespace("konnectedgdo") |
| 30 | +KONNECTEDGDO = konnectedgdo_ns.class_("GDOComponent", cg.Component) |
| 31 | + |
| 32 | +CONF_OUTPUT_GDO = "output_gdo_pin" |
| 33 | +DEFAULT_OUTPUT_GDO = ("1") |
| 34 | +CONF_INPUT_GDO = "input_gdo_pin" |
| 35 | +DEFAULT_INPUT_GDO = ("2") |
| 36 | +CONF_INPUT_OBST = "input_obst_pin" |
| 37 | +DEFAULT_INPUT_OBST = ("5") |
| 38 | + |
| 39 | +CONF_KONNECTEDGDO_ID = "konnectedgdo_id" |
| 40 | +CONF_PROTOCOL = "protocol" |
| 41 | + |
| 42 | +PROTOCOL_SECPLUSV1 = "secplusv1" |
| 43 | +PROTOCOL_SECPLUSV2 = "secplusv2" |
| 44 | +SUPPORTED_PROTOCOLS = [PROTOCOL_SECPLUSV1, PROTOCOL_SECPLUSV2] |
| 45 | + |
| 46 | +CONFIG_SCHEMA = cv.Schema( |
| 47 | + { |
| 48 | + cv.GenerateID(): cv.declare_id(KONNECTEDGDO), |
| 49 | + cv.Required(CONF_OUTPUT_GDO): pins.gpio_output_pin_schema, |
| 50 | + cv.Required(CONF_INPUT_GDO): pins.gpio_input_pin_schema, |
| 51 | + cv.Optional(CONF_INPUT_OBST, default=DEFAULT_INPUT_OBST): cv.Any( |
| 52 | + cv.none, pins.gpio_input_pin_schema |
| 53 | + ), |
| 54 | + cv.Optional(CONF_PROTOCOL, default=PROTOCOL_SECPLUSV2): vol.In( |
| 55 | + SUPPORTED_PROTOCOLS |
| 56 | + ), |
| 57 | + } |
| 58 | +).extend(cv.COMPONENT_SCHEMA) |
| 59 | + |
| 60 | +KONNECTED_GDO_CONFIG_SCHEMA = cv.Schema( |
| 61 | + { |
| 62 | + cv.Required(CONF_KONNECTEDGDO_ID): cv.use_id(KONNECTEDGDO), |
| 63 | + } |
| 64 | +) |
| 65 | + |
| 66 | +async def to_code(config): |
| 67 | + var = cg.new_Pvariable(config[CONF_ID]) |
| 68 | + await cg.register_component(var, config) |
| 69 | + cg.add_define("GDO_UART_TX_PIN", config[CONF_OUTPUT_GDO]['number']) |
| 70 | + cg.add_define("GDO_UART_RX_PIN", config[CONF_INPUT_GDO]['number']) |
| 71 | + if CONF_INPUT_OBST in config and config[CONF_INPUT_OBST]: |
| 72 | + cg.add_define("GDO_OBST_INPUT_PIN", config[CONF_INPUT_OBST]['number']) |
| 73 | + |
| 74 | + if config[CONF_PROTOCOL] == PROTOCOL_SECPLUSV1: |
| 75 | + cg.add_define("GDO_PROTOCOL", 1) |
| 76 | + else: |
| 77 | + cg.add_define("GDO_PROTOCOL", 2) |
0 commit comments