mirror of
https://github.com/esphome/esphome.git
synced 2024-11-24 07:58:09 +01:00
added manual configurable state_info text_sensor
This commit is contained in:
parent
d084160b33
commit
0743e192e9
6 changed files with 30 additions and 72 deletions
|
@ -19,7 +19,6 @@ CODEOWNERS = ["@j0ta29"]
|
||||||
DEPENDENCIES = ["text_sensor"]
|
DEPENDENCIES = ["text_sensor"]
|
||||||
AUTO_LOAD = []
|
AUTO_LOAD = []
|
||||||
MULTI_CONF = False
|
MULTI_CONF = False
|
||||||
CONF_DEVICE_INFO = "device_info"
|
|
||||||
|
|
||||||
optolink_ns = cg.esphome_ns.namespace("optolink")
|
optolink_ns = cg.esphome_ns.namespace("optolink")
|
||||||
CONF_OPTOLINK_ID = "optolink_id"
|
CONF_OPTOLINK_ID = "optolink_id"
|
||||||
|
@ -107,20 +106,6 @@ async def to_code(config):
|
||||||
)
|
)
|
||||||
await cg.register_component(debugSensor, config)
|
await cg.register_component(debugSensor, config)
|
||||||
|
|
||||||
if CONF_DEVICE_INFO in config:
|
|
||||||
debugSensor = cg.new_Pvariable(
|
|
||||||
config[DEVICE_INFO_SENSOR_ID], config[CONF_DEVICE_INFO], var
|
|
||||||
)
|
|
||||||
await ts.register_text_sensor(
|
|
||||||
debugSensor,
|
|
||||||
{
|
|
||||||
"id": config[DEVICE_INFO_SENSOR_ID],
|
|
||||||
"name": config[CONF_DEVICE_INFO],
|
|
||||||
"disabled_by_default": "false",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
await cg.register_component(debugSensor, config)
|
|
||||||
|
|
||||||
if CORE.is_esp32:
|
if CORE.is_esp32:
|
||||||
cg.add(var.set_rx_pin(config[CONF_RX_PIN]["number"]))
|
cg.add(var.set_rx_pin(config[CONF_RX_PIN]["number"]))
|
||||||
cg.add(var.set_tx_pin(config[CONF_TX_PIN]["number"]))
|
cg.add(var.set_tx_pin(config[CONF_TX_PIN]["number"]))
|
||||||
|
|
|
@ -79,21 +79,6 @@ size_t Optolink::write(uint8_t ch) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OptolinkDeviceInfoSensor::setup() {
|
|
||||||
datapoint_ = new Datapoint<conv4_1_UL>(get_name().c_str(), "optolink", 0x00f8, false);
|
|
||||||
datapoint_->setCallback([this](const IDatapoint &dp, DPValue dp_value) {
|
|
||||||
uint32_t value = dp_value.getU32();
|
|
||||||
ESP_LOGI(TAG, "recieved data for datapoint %s: %d", dp.getName(), value);
|
|
||||||
uint8_t *bytes = (uint8_t *) &value;
|
|
||||||
uint16_t tmp = esphome::byteswap(*((uint16_t *) bytes));
|
|
||||||
std::string geraetekennung = esphome::format_hex_pretty(&tmp, 1);
|
|
||||||
std::string hardware_revision = esphome::format_hex_pretty((uint8_t *) bytes + 2, 1);
|
|
||||||
std::string software_index = esphome::format_hex_pretty((uint8_t *) bytes + 3, 1);
|
|
||||||
publish_state("Device ID: " + geraetekennung + "|Hardware Revision: " + hardware_revision +
|
|
||||||
"|Software Index: " + software_index);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace optolink
|
} // namespace optolink
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
|
|
|
@ -37,41 +37,6 @@ class Optolink : public esphome::Component, public Print {
|
||||||
std::string get_error() { return error_; }
|
std::string get_error() { return error_; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class OptolinkStateSensor : public esphome::text_sensor::TextSensor, public esphome::PollingComponent {
|
|
||||||
public:
|
|
||||||
OptolinkStateSensor(std::string name, Optolink *optolink) {
|
|
||||||
optolink_ = optolink;
|
|
||||||
set_name(name.c_str());
|
|
||||||
set_update_interval(1000);
|
|
||||||
set_entity_category(esphome::ENTITY_CATEGORY_DIAGNOSTIC);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void setup() override{};
|
|
||||||
void update() override { publish_state(optolink_->get_error()); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
Optolink *optolink_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class OptolinkDeviceInfoSensor : public esphome::text_sensor::TextSensor, public esphome::PollingComponent {
|
|
||||||
public:
|
|
||||||
OptolinkDeviceInfoSensor(const std::string &name, Optolink *optolink) {
|
|
||||||
optolink_ = optolink;
|
|
||||||
set_name(name.c_str());
|
|
||||||
set_update_interval(1800000);
|
|
||||||
set_entity_category(esphome::ENTITY_CATEGORY_DIAGNOSTIC);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void setup() override;
|
|
||||||
void update() override { optolink_->read_value(datapoint_); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
Optolink *optolink_;
|
|
||||||
IDatapoint *datapoint_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace optolink
|
} // namespace optolink
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ MODE = {
|
||||||
"DAY_SCHEDULE": TextSensorMode.DAY_SCHEDULE,
|
"DAY_SCHEDULE": TextSensorMode.DAY_SCHEDULE,
|
||||||
"DAY_SCHEDULE_SYNCHRONIZED": TextSensorMode.DAY_SCHEDULE_SYNCHRONIZED,
|
"DAY_SCHEDULE_SYNCHRONIZED": TextSensorMode.DAY_SCHEDULE_SYNCHRONIZED,
|
||||||
"DEVICE_INFO": TextSensorMode.DEVICE_INFO,
|
"DEVICE_INFO": TextSensorMode.DEVICE_INFO,
|
||||||
|
"STATE_INFO": TextSensorMode.STATE_INFO,
|
||||||
}
|
}
|
||||||
DAY_OF_WEEK = {
|
DAY_OF_WEEK = {
|
||||||
"MONDAY": 0,
|
"MONDAY": 0,
|
||||||
|
@ -38,6 +39,19 @@ OptolinkTextSensor = optolink_ns.class_(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def check_address():
|
||||||
|
def validator_(config):
|
||||||
|
address_needed = config[CONF_MODE] in ["MAP", "RAW"]
|
||||||
|
address_defined = CONF_ADDRESS in config
|
||||||
|
if address_needed and not address_defined:
|
||||||
|
raise cv.Invalid(f"{CONF_ADDRESS} is required in mode MAP or RAW")
|
||||||
|
if not address_needed and address_defined:
|
||||||
|
raise cv.Invalid(f"{CONF_ADDRESS} is only allowed in mode MAP or RAW")
|
||||||
|
return config
|
||||||
|
|
||||||
|
return validator_
|
||||||
|
|
||||||
|
|
||||||
def check_bytes():
|
def check_bytes():
|
||||||
def validator_(config):
|
def validator_(config):
|
||||||
bytes_needed = config[CONF_MODE] in ["MAP", "RAW"]
|
bytes_needed = config[CONF_MODE] in ["MAP", "RAW"]
|
||||||
|
@ -45,9 +59,7 @@ def check_bytes():
|
||||||
if bytes_needed and not bytes_defined:
|
if bytes_needed and not bytes_defined:
|
||||||
raise cv.Invalid(f"{CONF_BYTES} is required in mode MAP or RAW")
|
raise cv.Invalid(f"{CONF_BYTES} is required in mode MAP or RAW")
|
||||||
if not bytes_needed and bytes_defined:
|
if not bytes_needed and bytes_defined:
|
||||||
raise cv.Invalid(
|
raise cv.Invalid(f"{CONF_BYTES} is only allowed in mode MAP or RAW")
|
||||||
f"{CONF_BYTES} is not allowed in mode DAY_SCHEDULE and DAY_SCHEDULE_SYNCHRONIZED"
|
|
||||||
)
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
return validator_
|
return validator_
|
||||||
|
@ -97,7 +109,8 @@ CONFIG_SCHEMA = cv.All(
|
||||||
text_sensor.TEXT_SENSOR_SCHEMA.extend(
|
text_sensor.TEXT_SENSOR_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.declare_id(OptolinkTextSensor),
|
cv.GenerateID(): cv.declare_id(OptolinkTextSensor),
|
||||||
cv.Optional(CONF_MODE, default="MAP"): cv.enum(MODE, upper=True),
|
cv.Required(CONF_MODE): cv.enum(MODE, upper=True),
|
||||||
|
cv.Optional(CONF_ADDRESS): cv.hex_uint32_t,
|
||||||
cv.Optional(CONF_BYTES): cv.int_range(min=1, max=9),
|
cv.Optional(CONF_BYTES): cv.int_range(min=1, max=9),
|
||||||
cv.Optional(CONF_DAY_OF_WEEK): cv.enum(DAY_OF_WEEK, upper=True),
|
cv.Optional(CONF_DAY_OF_WEEK): cv.enum(DAY_OF_WEEK, upper=True),
|
||||||
cv.Optional(CONF_ENTITY_ID): cv.entity_id,
|
cv.Optional(CONF_ENTITY_ID): cv.entity_id,
|
||||||
|
@ -105,6 +118,7 @@ CONFIG_SCHEMA = cv.All(
|
||||||
)
|
)
|
||||||
.extend(cv.COMPONENT_SCHEMA)
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
.extend(SENSOR_BASE_SCHEMA),
|
.extend(SENSOR_BASE_SCHEMA),
|
||||||
|
check_address(),
|
||||||
check_bytes(),
|
check_bytes(),
|
||||||
check_dow(),
|
check_dow(),
|
||||||
check_entity_id(),
|
check_entity_id(),
|
||||||
|
|
|
@ -108,6 +108,9 @@ void OptolinkTextSensor::setup() {
|
||||||
bytes_ = 4;
|
bytes_ = 4;
|
||||||
address_ = 0x00f8;
|
address_ = 0x00f8;
|
||||||
break;
|
break;
|
||||||
|
case STATE_INFO:
|
||||||
|
set_entity_category(esphome::ENTITY_CATEGORY_DIAGNOSTIC);
|
||||||
|
return; // no datapoint setup!
|
||||||
}
|
}
|
||||||
setup_datapoint();
|
setup_datapoint();
|
||||||
};
|
};
|
||||||
|
@ -136,6 +139,7 @@ void OptolinkTextSensor::value_changed(uint8_t *value, size_t length) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DEVICE_INFO:
|
case DEVICE_INFO:
|
||||||
|
case STATE_INFO:
|
||||||
case MAP:
|
case MAP:
|
||||||
unfitting_value_type();
|
unfitting_value_type();
|
||||||
break;
|
break;
|
||||||
|
@ -145,7 +149,6 @@ void OptolinkTextSensor::value_changed(uint8_t *value, size_t length) {
|
||||||
void OptolinkTextSensor::value_changed(uint32_t value) {
|
void OptolinkTextSensor::value_changed(uint32_t value) {
|
||||||
switch (mode_) {
|
switch (mode_) {
|
||||||
case DEVICE_INFO: {
|
case DEVICE_INFO: {
|
||||||
ESP_LOGI(TAG, "recieved data for datapoint %s: %d", datapoint_->getName(), value);
|
|
||||||
uint8_t *bytes = (uint8_t *) &value;
|
uint8_t *bytes = (uint8_t *) &value;
|
||||||
uint16_t tmp = esphome::byteswap(*((uint16_t *) bytes));
|
uint16_t tmp = esphome::byteswap(*((uint16_t *) bytes));
|
||||||
std::string geraetekennung = esphome::format_hex_pretty(&tmp, 1);
|
std::string geraetekennung = esphome::format_hex_pretty(&tmp, 1);
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace optolink {
|
namespace optolink {
|
||||||
|
|
||||||
enum TextSensorMode { MAP, RAW, DAY_SCHEDULE, DAY_SCHEDULE_SYNCHRONIZED, DEVICE_INFO };
|
enum TextSensorMode { MAP, RAW, DAY_SCHEDULE, DAY_SCHEDULE_SYNCHRONIZED, DEVICE_INFO, STATE_INFO };
|
||||||
|
|
||||||
class OptolinkTextSensor : public OptolinkSensorBase,
|
class OptolinkTextSensor : public OptolinkSensorBase,
|
||||||
public esphome::text_sensor::TextSensor,
|
public esphome::text_sensor::TextSensor,
|
||||||
|
@ -24,7 +24,13 @@ class OptolinkTextSensor : public OptolinkSensorBase,
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void update() override { optolink_->read_value(datapoint_); }
|
void update() override {
|
||||||
|
if (mode_ == STATE_INFO) {
|
||||||
|
publish_state(optolink_->get_error());
|
||||||
|
} else {
|
||||||
|
optolink_->read_value(datapoint_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const StringRef &get_component_name() override { return get_name(); }
|
const StringRef &get_component_name() override { return get_name(); }
|
||||||
void value_changed(float state) override { publish_state(std::to_string(state)); };
|
void value_changed(float state) override { publish_state(std::to_string(state)); };
|
||||||
|
|
Loading…
Reference in a new issue