Skip to content

Commit

Permalink
Component file structure refactor (#3)
Browse files Browse the repository at this point in the history
* Refactor separate switch component into nested switch component

* Refactor separate button component into nested button component

* Refactor separate binary_sensor component into nested binary_sensor component

* Refactor separate sensor component into nested sensor component

* Refactor switch and button component to be in-line with ESPHomes standard
  • Loading branch information
TillFleisch authored Jan 26, 2024
1 parent 22a86fe commit 3308c07
Show file tree
Hide file tree
Showing 22 changed files with 285 additions and 221 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ NamespaceIndentation: All
AccessModifierOffset: -4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
IndentCaseLabels: false
SortIncludes: false
ColumnLimit: 0
105 changes: 60 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This is a custom Component for [ESPHome](https://esphome.io/) based on the [ESPH
- Home Assistant Core 2021.12 or higher

## Usage

```yaml
external_components:
- source: github://TillFleisch/ESPHomeGenericSitStandDesk@main
Expand All @@ -35,70 +36,84 @@ generic_desk:
correction_term: 1.24836

sensor:
- platform: desk_height_sensor
- platform: generic_desk
desk_id: desk1
id: desk_height
name: "Desk Height"

binary_sensor:
- platform: desk_is_moving_sensor
desk_id: desk1
id: desk_is_moving
name: "Desk is Moving"

switch:
- platform: desk_switch
- platform: generic_desk
desk_id: desk1
name: "Up"
direction: true
id: desk_is_moving
name: "Desk is Moving"

- platform: desk_switch
switch:
- platform: generic_desk
desk_id: desk1
name: "Down"
direction: false
up:
name: "Up"
down:
name: "Down"

button:
- platform: desk_memory_button
- platform: generic_desk
desk_id: desk1
name: "M1"
memory_id: 1
M1:
name: "M1"
M2:
name: "M2"
M3:
name: "M3"
M4:
name: "M4"
```
- platform: desk_memory_button
desk_id: desk1
name: "M2"
memory_id: 2
An example configuration can be found [here](desk.yaml).
- platform: desk_memory_button
desk_id: desk1
name: "M3"
memory_id: 3
## Generic Desk
- platform: desk_memory_button
desk_id: desk1
name: "M4"
memory_id: 4
```
An example configuration can be found [here](desk.yaml).
The desks hub component to which other components refer.
## Entities
This Component can provide the following entities:
- **id**(**Required**, string): Controller ID which will be used by other components to refer to this desk.
- **display_uart**(**Required**, string): ID of the UART-Component connected desk-controller.
- **base_height**(**Required**, float): The Desks height measured in the lowest position in cm.
- **correction_term**(**Optional**, float): Optional correction factor, in case the height does not scale properly
**Controls:**
- M1
- M2
- M3
- M4
- Up
- Down
## Sensor
**Sensors:**
- height
- is moving
A sensor which reports the current height of the desk in cm.
- **desk_id**(**Required**, string): The desk controllers id to which this sensor belongs.
- All other options from [Sensor](https://esphome.io/components/sensor/index.html#base-sensor-configuration)
## Binary Sensor
A binary sensor which reports if the desk is currently moving.
- **desk_id**(**Required**, string): The desk controllers id to which this binary sensor belongs.
- All other options from [Binary Sensor](https://esphome.io/components/binary_sensor/index.html#base-binary-sensor-configuration)
## Switch
Switches which control the desks movement.
- **desk_id**(**Required**, string): The desk controllers id to which switches belongs.
- **up**(**Optional**): Moves the desk upwards. May contain all other options from [Switch](https://esphome.io/components/switch/index.html#base-switch-configuration).
- **down**(**Optional**): Moves the desk downwards. May contain all other options from [Switch](https://esphome.io/components/switch/index.html#base-switch-configuration).
## Button
Buttons which select a preset on the desks controller.
- **desk_id**(**Required**, string): The desk controllers id to which buttons belongs.
- **M1**(**Optional**): Activates the `M1` preset. May contain all other options from [Button](https://esphome.io/components/button/index.html#base-button-configuration).
- **M2**(**Optional**): Activates the `M2` preset. May contain all other options from [Button](https://esphome.io/components/button/index.html#base-button-configuration).
- **M3**(**Optional**): Activates the `M3` preset. May contain all other options from [Button](https://esphome.io/components/button/index.html#base-button-configuration).
- **M4**(**Optional**): Activates the `M4` preset. May contain all other options from [Button](https://esphome.io/components/button/index.html#base-button-configuration).

## Wiring

On my particular model the Handset is connected via a RJ-45 connector.
On my particular model the Handset is connected via a RJ-45 connector.

Control Box|Handset
:-:|:-:
Expand All @@ -117,9 +132,9 @@ GND ------- 6 (Green) - GND
TX ------- 7 (Brown/White) - RX
RX ------- 8 (Brown) - TX
```
Be aware that most ESPs run on 3.3v!
Be aware that most ESPs run on 3.3v!
## Communication
For information regarding the communication between the Controller and Handset look [here](communication.md).
For information regarding the communication between the Controller and Handset look [here](communication.md).
Empty file.
35 changes: 0 additions & 35 deletions components/desk_height_sensor/sensor.py

This file was deleted.

Empty file.
Empty file.
31 changes: 0 additions & 31 deletions components/desk_memory_button/button.py

This file was deleted.

Empty file removed components/desk_switch/__init__.py
Empty file.
29 changes: 0 additions & 29 deletions components/desk_switch/switch.py

This file was deleted.

11 changes: 1 addition & 10 deletions components/generic_desk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@
from esphome.const import CONF_ID

DEPENDENCIES = ["uart"]
AUTO_LOAD = [
"desk_memory_button",
"desk_switch",
"desk_height_sensor",
"desk_is_moving_sensor",
"sensor",
"binary_sensor",
"button",
"switch",
]

MULTI_CONF = True

CONF_DESK_ID = "desk_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import esphome.config_validation as cv
from esphome.components import binary_sensor

from ..generic_desk import CONF_DESK_ID, GenericDesk
from .. import CONF_DESK_ID, GenericDesk

DEPENDENCIES = ["generic_desk"]

Expand Down
50 changes: 50 additions & 0 deletions components/generic_desk/button/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import button
from esphome.const import CONF_ID

from .. import CONF_DESK_ID, GenericDesk

DEPENDENCIES = ["generic_desk"]

CONF_MEMORY_ID_1 = "M1"
CONF_MEMORY_ID_2 = "M2"
CONF_MEMORY_ID_3 = "M3"
CONF_MEMORY_ID_4 = "M4"

ID_CONFIGS = [
CONF_MEMORY_ID_1,
CONF_MEMORY_ID_2,
CONF_MEMORY_ID_3,
CONF_MEMORY_ID_4,
]

memory_button_ns = cg.esphome_ns.namespace("memory_button")
MemoryButton = memory_button_ns.class_("MemoryButton", button.Button, cg.Component)

BUTTON_CONFIG = button.BUTTON_SCHEMA.extend(
{cv.GenerateID(): cv.declare_id(MemoryButton)}
).extend(cv.COMPONENT_SCHEMA)

CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(CONF_DESK_ID): cv.use_id(GenericDesk),
cv.Optional(CONF_MEMORY_ID_1): BUTTON_CONFIG,
cv.Optional(CONF_MEMORY_ID_2): BUTTON_CONFIG,
cv.Optional(CONF_MEMORY_ID_3): BUTTON_CONFIG,
cv.Optional(CONF_MEMORY_ID_4): BUTTON_CONFIG,
}
),
cv.has_at_least_one_key(*ID_CONFIGS),
)


async def to_code(config):
parent = await cg.get_variable(config[CONF_DESK_ID])

for id, config_id in enumerate(ID_CONFIGS):
if button_config := config.get(config_id):
var = await button.new_button(button_config)
cg.add(var.set_memory_id(id))
cg.add(parent.add_button(var))
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ namespace esphome
{
public:
void dump_config() override;
void set_memory_id(int id) { this->memory_id = id - 1; };
void set_uart_device(uart::UARTDevice *uart_device) { this->uart_device = uart_device; };
void set_memory_id(int id)
{
this->memory_id = id;
};
void set_uart_device(uart::UARTDevice *uart_device)
{
this->uart_device = uart_device;
};

protected:
void press_action() override;
Expand Down
13 changes: 13 additions & 0 deletions components/generic_desk/generic_desk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,32 @@ namespace esphome
if (height != last_height)
{
last_height = height;
#ifdef USE_SENSOR
for (auto *height_sensor : this->height_sensors)
height_sensor->publish_state(height);
#endif
#ifdef USE_BINARY_SENSOR
for (auto *moving_sensor : this->moving_sensors)
moving_sensor->publish_state(true);
#endif

desk_moving_debounce_counter = 0;
}

// Debounce desk is moving to prevent state flickering
if (desk_moving_debounce_counter == DESK_MOVING_DEBOUNCE_THRESHOLD)
{
#ifdef USE_BINARY_SENSOR
// Set all sensors to false
for (auto *moving_sensor : this->moving_sensors)
moving_sensor->publish_state(false);
#endif

#ifdef USE_SWITCH
// Reset switches
for (auto *desk_switch : this->desk_switches)
desk_switch->publish_state(false);
#endif

desk_moving_debounce_counter++;
}
Expand All @@ -75,10 +84,14 @@ namespace esphome
void GenericDesk::dump_config()
{
ESP_LOGCONFIG(TAG, "Generic SitStand Desk");
#ifdef USE_SENSOR
for (auto *height_sensor : this->height_sensors)
LOG_SENSOR("", "Height sensor: ", height_sensor);
#endif
#ifdef USE_BINARY_SENSOR
for (auto *moving_sensor : this->moving_sensors)
LOG_BINARY_SENSOR("", "Is Moving binary sensor: ", moving_sensor);
#endif
}

/**
Expand Down
Loading

0 comments on commit 3308c07

Please sign in to comment.