Skip to content

Commit

Permalink
Merge branch 'feature/services'
Browse files Browse the repository at this point in the history
  • Loading branch information
mapero committed Oct 19, 2023
2 parents ce2fa9b + bb38cf0 commit 98b5d96
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 38 deletions.
26 changes: 25 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"xlocbuf": "cpp"
"xlocbuf": "cpp",
"array": "cpp",
"clocale": "cpp",
"deque": "cpp",
"ios": "cpp",
"istream": "cpp",
"iterator": "cpp",
"locale": "cpp",
"map": "cpp",
"ostream": "cpp",
"queue": "cpp",
"regex": "cpp",
"set": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xtree": "cpp"
}
}
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This is a esphome-based adaption of the HCPBridge. Credits for the initial devel

## Usage

### Example esphome configuration

```
esphome:
name: hcpbridge
Expand All @@ -31,6 +33,8 @@ esp32:
type: arduino
hcpbridge:
is_connected:
name: "HCPBridge Connected"
cover:
- platform: hcpbridge
Expand Down Expand Up @@ -59,6 +63,27 @@ ota:
safe_mode: true
```

### Home Assistant

![Home Assistant Device Overview](docs/device_overview.png)

### Cover

The component provides a cover component to control the garage door.

### Switch

The component provides a switch component to turn the light off and on.

### Services

Additionally, when using the cover component, you can use the following services:

- `esphome.hcpbridge_go_to_close`: To close the garage door
- `esphome.hcpbridge_go_to_half`: To move the garage door to half position
- `esphome.hcpbridge_go_to_vent`: To move the garage door to the vent position
- `esphome.hcpbridge_go_to_open`: To open the garage door

# Project

- HCPBridge from `Tysonpower` on an `Hörmann Promatic 4`
Expand All @@ -70,6 +95,7 @@ You can find more information on the project here: [Hörmann garage door via MQT
- [x] Initial working version
- [ ] Use esphome modbus component instead of own code
- [ ] Map additional functions to esphome
- [ ] Use callbacks instead of pollingComponent
- [ ] Expert options for the HCPBridge component (GPIOs ...)

# Contribute
Expand Down
15 changes: 14 additions & 1 deletion components/hcpbridge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@

import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import (
CONF_ID,
DEVICE_CLASS_CONNECTIVITY,
)

AUTO_LOAD = ["binary_sensor"]

CONF_IS_CONNECTED = "is_connected"

hcpbridge_ns = cg.esphome_ns.namespace("hcpbridge")
HCPBridge = hcpbridge_ns.class_("HCPBridge", cg.Component)

Expand All @@ -13,9 +19,16 @@
CONFIG_SCHEMA = (
cv.Schema({
cv.GenerateID(): cv.declare_id(HCPBridge),
cv.Required(CONF_IS_CONNECTED): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_CONNECTIVITY,
),
})
)

async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await cg.register_component(var, config)

if CONF_IS_CONNECTED in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_IS_CONNECTED])
cg.add(var.set_is_connected(sens))
75 changes: 55 additions & 20 deletions components/hcpbridge/cover/hcpbridge_cover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,38 @@ namespace esphome
{
static const char *const TAG = "hcpbridge.cover";

void HCPBridgeCover::setup()
{
register_service(&HCPBridgeCover::on_go_to_open, "go_to_open");
register_service(&HCPBridgeCover::on_go_to_close, "go_to_close");
register_service(&HCPBridgeCover::on_go_to_half, "go_to_half");
register_service(&HCPBridgeCover::on_go_to_vent, "go_to_vent");
}

void HCPBridgeCover::on_go_to_open()
{
ESP_LOGD(TAG, "HCPBridgeCover::on_go_to_open() - opening");
this->parent_->engine->openDoor();
}

void HCPBridgeCover::on_go_to_close()
{
ESP_LOGD(TAG, "HCPBridgeCover::on_go_to_close() - closing");
this->parent_->engine->closeDoor();
}

void HCPBridgeCover::on_go_to_half()
{
ESP_LOGD(TAG, "HCPBridgeCover::on_go_to_half() - half opening");
this->parent_->engine->halfPositionDoor();
}

void HCPBridgeCover::on_go_to_vent()
{
ESP_LOGD(TAG, "HCPBridgeCover::on_go_to_vent() - venting");
this->parent_->engine->ventilationPositionDoor();
}

cover::CoverTraits HCPBridgeCover::get_traits()
{
auto traits = cover::CoverTraits();
Expand All @@ -31,7 +63,8 @@ namespace esphome
{
this->parent_->engine->closeDoor();
}
else {
else
{
this->parent_->engine->setPosition((int)call.get_position().value() * 100.0f);
}
}
Expand All @@ -41,34 +74,36 @@ namespace esphome
{
if (!this->parent_->engine->state->valid)
{
ESP_LOGD(TAG, "HCPBridgeCover::update() - state is invalid");
this->status_set_warning();
if (!this->status_has_warning())
{
ESP_LOGD(TAG, "HCPBridgeCover::update() - state is invalid");
this->status_set_warning();
}
return;
}
ESP_LOGD(TAG, "HCPBridgeCover::update() - state is valid");
if (this->status_has_warning())
{
ESP_LOGD(TAG, "HCPBridgeCover::update() - clearing warning");
this->status_clear_warning();
}

switch (this->parent_->engine->state->state)
switch (this->parent_->engine->state->state)
{
case HoermannState::OPENING:
case HoermannState::MOVE_VENTING:
case HoermannState::MOVE_HALF:
this->current_operation = cover::COVER_OPERATION_OPENING;
break;
case HoermannState::CLOSING:
this->current_operation = cover::COVER_OPERATION_CLOSING;
break;
case HoermannState::OPEN:
case HoermannState::CLOSED:
case HoermannState::STOPPED:
case HoermannState::HALFOPEN:
case HoermannState::VENT:
this->current_operation = cover::COVER_OPERATION_IDLE;
break;
case HoermannState::OPENING:
case HoermannState::MOVE_VENTING:
case HoermannState::MOVE_HALF:
this->current_operation = cover::COVER_OPERATION_OPENING;
break;
case HoermannState::CLOSING:
this->current_operation = cover::COVER_OPERATION_CLOSING;
break;
case HoermannState::OPEN:
case HoermannState::CLOSED:
case HoermannState::STOPPED:
case HoermannState::HALFOPEN:
case HoermannState::VENT:
this->current_operation = cover::COVER_OPERATION_IDLE;
break;
}
this->position = this->parent_->engine->state->currentPosition;
if (this->previousPosition_ != this->position || this->previousOperation_ != this->current_operation)
Expand Down
28 changes: 18 additions & 10 deletions components/hcpbridge/cover/hcpbridge_cover.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@

#include "esphome/core/component.h"
#include "esphome/components/cover/cover.h"
#include "esphome/components/api/custom_api_device.h"
#include "../hcpbridge.h"

namespace esphome
{
namespace hcpbridge
{
class HCPBridgeCover : public cover::Cover, public PollingComponent
class HCPBridgeCover : public cover::Cover, public PollingComponent, public api::CustomAPIDevice
{
public:
cover::CoverTraits get_traits() override;
void control(const cover::CoverCall &call) override;
void set_hcpbridge_parent(HCPBridge *parent) { this->parent_ = parent; }
void update() override;
public:
cover::CoverTraits get_traits() override;
void control(const cover::CoverCall &call) override;
void set_hcpbridge_parent(HCPBridge *parent) { this->parent_ = parent; }
void update() override;
void setup() override;

private:
HCPBridge *parent_;
float previousPosition_ = 0.0f;
cover::CoverOperation previousOperation_ = cover::COVER_OPERATION_IDLE;
// Home Assistant service handler
void on_go_to_half();
void on_go_to_open();
void on_go_to_close();
void on_go_to_vent();

private:
HCPBridge *parent_;
float previousPosition_ = 0.0f;
cover::CoverOperation previousOperation_ = cover::COVER_OPERATION_IDLE;
};
}
}
13 changes: 13 additions & 0 deletions components/hcpbridge/hcpbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,22 @@ namespace esphome

void HCPBridge::setup()
{
this->is_connected_->publish_state(false);
this->engine = &HoermannGarageEngine::getInstance();
this->engine->setup();
}

void HCPBridge::update() {
if (this->engine->state->valid) {
if (this->is_connected_->state != true) {
this->is_connected_->publish_state(true);
}
} else {
if (this->is_connected_->state != false) {
this->is_connected_->publish_state(false);
}
}
}

}
}
12 changes: 10 additions & 2 deletions components/hcpbridge/hcpbridge.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#pragma once

#include "esphome/core/component.h"
#include "esphome/components/binary_sensor/binary_sensor.h"

#include "hoermann.h"

namespace esphome
{
namespace hcpbridge
{

class HCPBridge : public Component
class HCPBridge : public PollingComponent
{
public:
void setup() override;
void update() override;

void set_is_connected(binary_sensor::BinarySensor *is_connected) { this->is_connected_ = is_connected; }

HoermannGarageEngine* engine;
};

protected:
binary_sensor::BinarySensor *is_connected_;
};
}
}
7 changes: 3 additions & 4 deletions components/hcpbridge/switch/hcpbridge_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ namespace esphome
ESP_LOGD(TAG, "HCPBridgeSwitch::update() - clearing warning");
this->status_clear_warning();
}
this->state = this->parent_->engine->state->lightOn;
if (this->previousState_ != this->state)
if (this->previousState_ != this->parent_->engine->state->lightOn)
{
ESP_LOGD(TAG, "HCPBridgeSwitch::update() - state changed");
this->publish_state(false);
this->previousState_ = this->state;
this->publish_state(this->parent_->engine->state->lightOn);
this->previousState_ = this->parent_->engine->state->lightOn;
}
}

Expand Down
Binary file added docs/device_overview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions example_hcpbridge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ esp32:
type: arduino

hcpbridge:
is_connected:
name: "HCPBridge Connected"

cover:
- platform: hcpbridge
Expand Down

0 comments on commit 98b5d96

Please sign in to comment.