-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add select entity to change the operation mode (Closes: #53)
- Loading branch information
Showing
10 changed files
with
509 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import esphome.codegen as cg | ||
from esphome.components import button | ||
import esphome.config_validation as cv | ||
from esphome.const import CONF_ICON, CONF_ID | ||
|
||
from .. import ( | ||
CONF_SOYOSOURCE_DISPLAY_COMPONENT_SCHEMA, | ||
CONF_SOYOSOURCE_DISPLAY_ID, | ||
soyosource_display_ns, | ||
) | ||
|
||
DEPENDENCIES = ["soyosource_display"] | ||
|
||
CODEOWNERS = ["@syssi"] | ||
|
||
CONF_RESTART = "restart" | ||
|
||
ICON_RESTART = "mdi:restart" | ||
|
||
BUTTONS = { | ||
CONF_RESTART: 0x11, | ||
} | ||
|
||
SoyosourceButton = soyosource_display_ns.class_( | ||
"SoyosourceButton", button.Button, cg.Component | ||
) | ||
|
||
CONFIG_SCHEMA = CONF_SOYOSOURCE_DISPLAY_COMPONENT_SCHEMA.extend( | ||
{ | ||
cv.Optional(CONF_RESTART): button.BUTTON_SCHEMA.extend( | ||
{ | ||
cv.GenerateID(): cv.declare_id(SoyosourceButton), | ||
cv.Optional(CONF_ICON, default=ICON_RESTART): cv.icon, | ||
} | ||
).extend(cv.COMPONENT_SCHEMA), | ||
} | ||
) | ||
|
||
|
||
async def to_code(config): | ||
hub = await cg.get_variable(config[CONF_SOYOSOURCE_DISPLAY_ID]) | ||
for key, address in BUTTONS.items(): | ||
if key in config: | ||
conf = config[key] | ||
var = cg.new_Pvariable(conf[CONF_ID]) | ||
await cg.register_component(var, conf) | ||
await button.register_button(var, conf) | ||
cg.add(var.set_parent(hub)) | ||
cg.add(var.set_holding_register(address)) |
16 changes: 16 additions & 0 deletions
16
components/soyosource_display/button/soyosource_button.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "soyosource_button.h" | ||
#include "esphome/core/log.h" | ||
#include "esphome/core/application.h" | ||
|
||
namespace esphome { | ||
namespace soyosource_display { | ||
|
||
static const char *const TAG = "soyosource_display.button"; | ||
|
||
void SoyosourceButton::dump_config() { LOG_BUTTON("", "SoyosourceDisplay Button", this); } | ||
void SoyosourceButton::press_action() { | ||
this->parent_->send_command(this->holding_register_, 0x30, 0x2D, 0x5A, 0x64, 0x06, 0x00); | ||
} | ||
|
||
} // namespace soyosource_display | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include "../soyosource_display.h" | ||
#include "esphome/core/component.h" | ||
#include "esphome/components/button/button.h" | ||
|
||
namespace esphome { | ||
namespace soyosource_display { | ||
|
||
class SoyosourceDisplay; | ||
|
||
class SoyosourceButton : public button::Button, public Component { | ||
public: | ||
void set_parent(SoyosourceDisplay *parent) { this->parent_ = parent; }; | ||
void set_holding_register(uint8_t holding_register) { this->holding_register_ = holding_register; }; | ||
void dump_config() override; | ||
void loop() override {} | ||
float get_setup_priority() const override { return setup_priority::DATA; } | ||
|
||
protected: | ||
void press_action() override; | ||
SoyosourceDisplay *parent_; | ||
uint8_t holding_register_; | ||
}; | ||
|
||
} // namespace soyosource_display | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import esphome.codegen as cg | ||
from esphome.components import number | ||
import esphome.config_validation as cv | ||
from esphome.const import ( | ||
CONF_ENTITY_CATEGORY, | ||
CONF_ICON, | ||
CONF_ID, | ||
CONF_MAX_VALUE, | ||
CONF_MIN_VALUE, | ||
CONF_MODE, | ||
CONF_STEP, | ||
CONF_UNIT_OF_MEASUREMENT, | ||
ENTITY_CATEGORY_CONFIG, | ||
ICON_EMPTY, | ||
UNIT_SECOND, | ||
UNIT_VOLT, | ||
UNIT_WATT, | ||
) | ||
|
||
from .. import ( | ||
CONF_SOYOSOURCE_DISPLAY_COMPONENT_SCHEMA, | ||
CONF_SOYOSOURCE_DISPLAY_ID, | ||
soyosource_display_ns, | ||
) | ||
|
||
DEPENDENCIES = ["soyosource_display"] | ||
|
||
CODEOWNERS = ["@syssi"] | ||
|
||
DEFAULT_STEP = 1 | ||
|
||
CONF_START_VOLTAGE = "start_voltage" | ||
CONF_SHUTDOWN_VOLTAGE = "shutdown_voltage" | ||
CONF_OUTPUT_POWER_LIMIT = "output_power_limit" | ||
CONF_START_DELAY = "start_delay" | ||
|
||
# Write settings frame: 0x55 0x0B 0x30 0x2D 0x5A 0x64 0x00 0x00 0x00 0x06 0x01 0xD2 | ||
# 0 1 2 3 4 5 6 7 8 9 10 11 | ||
# | | | | | | | | | | | CRC | ||
# | | | | | | | | | | Operation mode | ||
# | | | | | | | | | Start delay | ||
# | | | | | | | | Unused | ||
# | | | | | | | Unused | ||
# | | | | | | Unused | ||
# | | | | | Grid frequency | ||
# | | | | Output power | ||
# | | | Shutdown voltage | ||
# | | Start voltage | ||
# | Write settings | ||
# SOF Request | ||
|
||
# Settings frame: 0xA6 0x00 0x00 0x63 0x02 0xD4 0x30 0x30 0x2D 0x00 0xFA 0x64 0x5A 0x06 0x7B | ||
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ||
# | | | | | | | | | | | CRC | ||
# | | | | | | | | | | Start delay | ||
# | | | | | | | | | Output power | ||
# | | | | | | | | Grid frequency | ||
# | | | | | | Grid voltage (2 bytes) | ||
# | | | | | Shutdown voltage | ||
# | | | | Start voltage | ||
# | | | Device type | ||
# | | Device model | ||
# | Operation status bitmask | ||
# Operation mode (High nibble), Frame function (Low nibble) | ||
NUMBERS = { | ||
CONF_START_VOLTAGE: 0x02, | ||
CONF_SHUTDOWN_VOLTAGE: 0x03, | ||
CONF_OUTPUT_POWER_LIMIT: 0x04, | ||
CONF_START_DELAY: 0x09, | ||
} | ||
|
||
SoyosourceNumber = soyosource_display_ns.class_( | ||
"SoyosourceNumber", number.Number, cg.Component | ||
) | ||
|
||
SOYOSOURCE_NUMBER_SCHEMA = number.NUMBER_SCHEMA.extend( | ||
{ | ||
cv.GenerateID(): cv.declare_id(SoyosourceNumber), | ||
cv.Optional(CONF_ICON, default=ICON_EMPTY): number.icon, | ||
cv.Optional(CONF_STEP, default=0.01): cv.float_, | ||
cv.Optional(CONF_UNIT_OF_MEASUREMENT, default=UNIT_VOLT): cv.string_strict, | ||
cv.Optional(CONF_MODE, default="BOX"): cv.enum(number.NUMBER_MODES, upper=True), | ||
cv.Optional( | ||
CONF_ENTITY_CATEGORY, default=ENTITY_CATEGORY_CONFIG | ||
): cv.entity_category, | ||
} | ||
).extend(cv.COMPONENT_SCHEMA) | ||
|
||
CONFIG_SCHEMA = CONF_SOYOSOURCE_DISPLAY_COMPONENT_SCHEMA.extend( | ||
{ | ||
cv.Optional(CONF_START_VOLTAGE): SOYOSOURCE_NUMBER_SCHEMA.extend( | ||
{ | ||
cv.Optional(CONF_MIN_VALUE, default=1): cv.float_, | ||
cv.Optional(CONF_MAX_VALUE, default=90): cv.float_, | ||
cv.Optional(CONF_STEP, default=1): cv.float_, | ||
} | ||
), | ||
cv.Optional(CONF_SHUTDOWN_VOLTAGE): SOYOSOURCE_NUMBER_SCHEMA.extend( | ||
{ | ||
cv.Optional(CONF_MIN_VALUE, default=1): cv.float_, | ||
cv.Optional(CONF_MAX_VALUE, default=90): cv.float_, | ||
cv.Optional(CONF_STEP, default=1): cv.float_, | ||
} | ||
), | ||
cv.Optional(CONF_OUTPUT_POWER_LIMIT): SOYOSOURCE_NUMBER_SCHEMA.extend( | ||
{ | ||
cv.Optional(CONF_MIN_VALUE, default=10): cv.float_, | ||
cv.Optional(CONF_MAX_VALUE, default=900): cv.float_, | ||
cv.Optional(CONF_STEP, default=10): cv.float_, | ||
cv.Optional( | ||
CONF_UNIT_OF_MEASUREMENT, default=UNIT_WATT | ||
): cv.string_strict, | ||
} | ||
), | ||
cv.Optional(CONF_START_DELAY): SOYOSOURCE_NUMBER_SCHEMA.extend( | ||
{ | ||
cv.Optional(CONF_MIN_VALUE, default=1): cv.float_, | ||
cv.Optional(CONF_MAX_VALUE, default=60): cv.float_, | ||
cv.Optional(CONF_STEP, default=1): cv.float_, | ||
cv.Optional( | ||
CONF_UNIT_OF_MEASUREMENT, default=UNIT_SECOND | ||
): cv.string_strict, | ||
} | ||
), | ||
} | ||
) | ||
|
||
|
||
async def to_code(config): | ||
hub = await cg.get_variable(config[CONF_SOYOSOURCE_DISPLAY_ID]) | ||
for key, address in NUMBERS.items(): | ||
if key in config: | ||
conf = config[key] | ||
var = cg.new_Pvariable(conf[CONF_ID]) | ||
await cg.register_component(var, conf) | ||
await number.register_number( | ||
var, | ||
conf, | ||
min_value=conf[CONF_MIN_VALUE], | ||
max_value=conf[CONF_MAX_VALUE], | ||
step=conf[CONF_STEP], | ||
) | ||
cg.add(getattr(hub, f"set_{key}_number")(var)) | ||
cg.add(var.set_parent(hub)) | ||
cg.add(var.set_holding_register(address)) |
13 changes: 13 additions & 0 deletions
13
components/soyosource_display/number/soyosource_number.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "soyosource_number.h" | ||
#include "esphome/core/log.h" | ||
|
||
namespace esphome { | ||
namespace soyosource_display { | ||
|
||
static const char *const TAG = "soyosource_display.number"; | ||
|
||
void SoyosourceNumber::dump_config() { LOG_NUMBER("", "SoyosourceDisplay Number", this); } | ||
void SoyosourceNumber::control(float value) { this->parent_->update_setting(this->holding_register_, value); } | ||
|
||
} // namespace soyosource_display | ||
} // namespace esphome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include "../soyosource_display.h" | ||
#include "esphome/core/component.h" | ||
#include "esphome/components/number/number.h" | ||
|
||
namespace esphome { | ||
namespace soyosource_display { | ||
|
||
class SoyosourceDisplay; | ||
|
||
class SoyosourceNumber : public number::Number, public Component { | ||
public: | ||
void set_parent(SoyosourceDisplay *parent) { this->parent_ = parent; }; | ||
void set_holding_register(uint8_t holding_register) { this->holding_register_ = holding_register; }; | ||
void dump_config() override; | ||
|
||
protected: | ||
void control(float value) override; | ||
|
||
SoyosourceDisplay *parent_; | ||
uint8_t holding_register_; | ||
}; | ||
|
||
} // namespace soyosource_display | ||
} // namespace esphome |
Oops, something went wrong.