Skip to content

Commit c64974f

Browse files
committed
initial commit
0 parents  commit c64974f

23 files changed

+2182
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Gitignore settings for ESPHome
2+
# This is an example and may include too much for your use-case.
3+
# You can modify this file to suit your needs.
4+
.esphome
5+
secrets.yaml
6+
.vscode
7+
**/__pycache__/

.pre-commit-config.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v3.2.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-added-large-files

LICENSE

+674
Large diffs are not rendered by default.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Konnected GDO for ESPHome

components/konnectedgdo/__init__.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
from esphome.components import binary_sensor
23+
from esphome.const import CONF_ID
24+
25+
from .. import KONNECTED_GDO_CONFIG_SCHEMA, konnectedgdo_ns, CONF_KONNECTEDGDO_ID
26+
27+
DEPENDENCIES = ["konnectedgdo"]
28+
29+
GDOBinarySensor = konnectedgdo_ns.class_(
30+
"GDOBinarySensor", binary_sensor.BinarySensor, cg.Component
31+
)
32+
33+
CONF_TYPE = "type"
34+
TYPES = {
35+
"motion": "register_motion",
36+
"obstruction": "register_obstruction",
37+
"motor": "register_motor",
38+
"button": "register_button",
39+
}
40+
41+
42+
CONFIG_SCHEMA = (
43+
binary_sensor.binary_sensor_schema(GDOBinarySensor)
44+
.extend(
45+
{
46+
cv.Required(CONF_TYPE): cv.enum(TYPES, lower=True),
47+
}
48+
)
49+
.extend(KONNECTED_GDO_CONFIG_SCHEMA)
50+
)
51+
52+
53+
async def to_code(config):
54+
var = cg.new_Pvariable(config[CONF_ID])
55+
await binary_sensor.register_binary_sensor(var, config)
56+
await cg.register_component(var, config)
57+
parent = await cg.get_variable(config[CONF_KONNECTEDGDO_ID])
58+
fcall = str(parent) + "->" + str(TYPES[config[CONF_TYPE]])
59+
text = fcall + "(std::bind(&" + str(GDOBinarySensor) + "::publish_state," + str(config[CONF_ID]) + ",std::placeholders::_1))"
60+
cg.add((cg.RawExpression(text)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2024 Konnected Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
# pragma once
19+
20+
#include "esphome/core/component.h"
21+
#include "esphome/components/binary_sensor/binary_sensor.h"
22+
23+
namespace esphome {
24+
namespace konnectedgdo {
25+
26+
class GDOBinarySensor : public binary_sensor::BinarySensor, public Component {};
27+
28+
} // namespace konnectedgdo
29+
} // namespace esphome
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
from esphome.components import cover
23+
from esphome.const import CONF_ID
24+
25+
from .. import KONNECTED_GDO_CONFIG_SCHEMA, konnectedgdo_ns, CONF_KONNECTEDGDO_ID
26+
27+
DEPENDENCIES = ["konnectedgdo"]
28+
29+
GDODoor = konnectedgdo_ns.class_("GDODoor", cover.Cover, cg.Component)
30+
31+
CONFIG_SCHEMA = cover.COVER_SCHEMA.extend(
32+
{
33+
cv.GenerateID(): cv.declare_id(GDODoor),
34+
}
35+
).extend(KONNECTED_GDO_CONFIG_SCHEMA)
36+
37+
38+
async def to_code(config):
39+
var = cg.new_Pvariable(config[CONF_ID])
40+
await cg.register_component(var, config)
41+
await cover.register_cover(var, config)
42+
parent = await cg.get_variable(config[CONF_KONNECTEDGDO_ID])
43+
text = "std::bind(&" + str(GDODoor) + "::set_state," + str(config[CONF_ID]) + ",std::placeholders::_1,std::placeholders::_2)"
44+
cg.add(parent.register_door(cg.RawExpression(text)))
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (C) 2024 Konnected Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#pragma once
19+
20+
#include "esphome/components/cover/cover.h"
21+
#include "esphome/core/component.h"
22+
#include "../gdolib.h"
23+
24+
namespace esphome {
25+
namespace konnectedgdo {
26+
27+
using namespace esphome::cover;
28+
29+
class GDODoor : public cover::Cover, public Component {
30+
public:
31+
cover::CoverTraits get_traits() override {
32+
auto traits = CoverTraits();
33+
traits.set_supports_stop(true);
34+
traits.set_supports_toggle(true);
35+
traits.set_supports_position(true);
36+
return traits;
37+
}
38+
39+
void set_state(gdo_door_state_t state, float position) {
40+
bool save_to_flash = false; //true;
41+
switch (state) {
42+
case GDO_DOOR_STATE_OPEN:
43+
this->position = COVER_OPEN;
44+
this->current_operation = COVER_OPERATION_IDLE;
45+
break;
46+
case GDO_DOOR_STATE_CLOSED:
47+
this->position = COVER_CLOSED;
48+
this->current_operation = COVER_OPERATION_IDLE;
49+
break;
50+
case GDO_DOOR_STATE_OPENING:
51+
this->current_operation = COVER_OPERATION_OPENING;
52+
this->position = position;
53+
save_to_flash = false;
54+
break;
55+
case GDO_DOOR_STATE_CLOSING:
56+
this->current_operation = COVER_OPERATION_CLOSING;
57+
this->position = position;
58+
save_to_flash = false;
59+
break;
60+
case GDO_DOOR_STATE_STOPPED:
61+
this->current_operation = COVER_OPERATION_IDLE;
62+
this->position = position;
63+
break;
64+
case GDO_DOOR_STATE_MAX:
65+
default:
66+
this->current_operation = COVER_OPERATION_IDLE;
67+
this->position = position;
68+
break;
69+
}
70+
71+
this->publish_state(save_to_flash);
72+
}
73+
74+
protected:
75+
void control(const cover::CoverCall& call) override {
76+
if (call.get_stop()) {
77+
gdo_door_stop();
78+
}
79+
if (call.get_toggle()) {
80+
gdo_door_toggle();
81+
}
82+
if (call.get_position().has_value()) {
83+
auto pos = *call.get_position();
84+
if (pos == COVER_OPEN) {
85+
gdo_door_open();
86+
} else if (pos == COVER_CLOSED) {
87+
gdo_door_close();
88+
} else {
89+
gdo_door_move_to_target(pos * 10000);
90+
}
91+
}
92+
}
93+
};
94+
95+
} // namespace konnectedgdo
96+
} // namespace esphome

0 commit comments

Comments
 (0)