Add attribute support to Home Assistant sensors (#1770)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Franck Nijhof
2021-05-17 01:16:22 +02:00
committed by GitHub
parent 9a7a205510
commit 5645be4e0f
19 changed files with 113 additions and 25 deletions
@@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor
from esphome.const import CONF_ENTITY_ID, CONF_ID
from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_ID
from .. import homeassistant_ns
DEPENDENCIES = ["api"]
@@ -13,6 +13,7 @@ CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(HomeassistantBinarySensor),
cv.Required(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_ATTRIBUTE): cv.string,
}
).extend(cv.COMPONENT_SCHEMA)
@@ -23,3 +24,5 @@ def to_code(config):
yield binary_sensor.register_binary_sensor(var, config)
cg.add(var.set_entity_id(config[CONF_ENTITY_ID]))
if CONF_ATTRIBUTE in config:
cg.add(var.set_attribute(config[CONF_ATTRIBUTE]))
@@ -8,7 +8,7 @@ namespace homeassistant {
static const char *TAG = "homeassistant.binary_sensor";
void HomeassistantBinarySensor::setup() {
api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) {
api::global_api_server->subscribe_home_assistant_state(this->entity_id_, this->attribute_, [this](std::string state) {
auto val = parse_on_off(state.c_str());
switch (val) {
case PARSE_NONE:
@@ -18,7 +18,12 @@ void HomeassistantBinarySensor::setup() {
case PARSE_ON:
case PARSE_OFF:
bool new_state = val == PARSE_ON;
ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_.c_str(), ONOFF(new_state));
if (this->attribute_.has_value()) {
ESP_LOGD(TAG, "'%s::%s': Got attribute state %s", this->entity_id_.c_str(), this->attribute_.value().c_str(),
ONOFF(new_state));
} else {
ESP_LOGD(TAG, "'%s': Got state %s", this->entity_id_.c_str(), ONOFF(new_state));
}
if (this->initial_)
this->publish_initial_state(new_state);
else
@@ -31,6 +36,9 @@ void HomeassistantBinarySensor::setup() {
void HomeassistantBinarySensor::dump_config() {
LOG_BINARY_SENSOR("", "Homeassistant Binary Sensor", this);
ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
if (this->attribute_.has_value()) {
ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_.value().c_str());
}
}
float HomeassistantBinarySensor::get_setup_priority() const { return setup_priority::AFTER_WIFI; }
@@ -9,12 +9,14 @@ namespace homeassistant {
class HomeassistantBinarySensor : public binary_sensor::BinarySensor, public Component {
public:
void set_entity_id(const std::string &entity_id) { entity_id_ = entity_id; }
void set_attribute(const std::string &attribute) { attribute_ = attribute; }
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
protected:
std::string entity_id_;
optional<std::string> attribute_;
bool initial_{true};
};
@@ -2,6 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (
CONF_ATTRIBUTE,
CONF_ENTITY_ID,
CONF_ID,
ICON_EMPTY,
@@ -22,6 +23,7 @@ CONFIG_SCHEMA = sensor.sensor_schema(
{
cv.GenerateID(): cv.declare_id(HomeassistantSensor),
cv.Required(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_ATTRIBUTE): cv.string,
}
)
@@ -32,3 +34,5 @@ def to_code(config):
yield sensor.register_sensor(var, config)
cg.add(var.set_entity_id(config[CONF_ENTITY_ID]))
if CONF_ATTRIBUTE in config:
cg.add(var.set_attribute(config[CONF_ATTRIBUTE]))
@@ -8,7 +8,7 @@ namespace homeassistant {
static const char *TAG = "homeassistant.sensor";
void HomeassistantSensor::setup() {
api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) {
api::global_api_server->subscribe_home_assistant_state(this->entity_id_, this->attribute_, [this](std::string state) {
auto val = parse_float(state);
if (!val.has_value()) {
ESP_LOGW(TAG, "Can't convert '%s' to number!", state.c_str());
@@ -16,13 +16,21 @@ void HomeassistantSensor::setup() {
return;
}
ESP_LOGD(TAG, "'%s': Got state %.2f", this->entity_id_.c_str(), *val);
if (this->attribute_.has_value()) {
ESP_LOGD(TAG, "'%s::%s': Got attribute state %.2f", this->entity_id_.c_str(), this->attribute_.value().c_str(),
*val);
} else {
ESP_LOGD(TAG, "'%s': Got state %.2f", this->entity_id_.c_str(), *val);
}
this->publish_state(*val);
});
}
void HomeassistantSensor::dump_config() {
LOG_SENSOR("", "Homeassistant Sensor", this);
ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
if (this->attribute_.has_value()) {
ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_.value().c_str());
}
}
float HomeassistantSensor::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
@@ -9,12 +9,14 @@ namespace homeassistant {
class HomeassistantSensor : public sensor::Sensor, public Component {
public:
void set_entity_id(const std::string &entity_id) { entity_id_ = entity_id; }
void set_attribute(const std::string &attribute) { attribute_ = attribute; }
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
protected:
std::string entity_id_;
optional<std::string> attribute_;
};
} // namespace homeassistant
@@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor
from esphome.const import CONF_ENTITY_ID, CONF_ID
from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_ID
from .. import homeassistant_ns
DEPENDENCIES = ["api"]
@@ -14,6 +14,7 @@ CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(HomeassistantTextSensor),
cv.Required(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_ATTRIBUTE): cv.string,
}
)
@@ -24,3 +25,5 @@ def to_code(config):
yield text_sensor.register_text_sensor(var, config)
cg.add(var.set_entity_id(config[CONF_ENTITY_ID]))
if CONF_ATTRIBUTE in config:
cg.add(var.set_attribute(config[CONF_ATTRIBUTE]))
@@ -7,16 +7,24 @@ namespace homeassistant {
static const char *TAG = "homeassistant.text_sensor";
void HomeassistantTextSensor::dump_config() {
LOG_TEXT_SENSOR("", "Homeassistant Text Sensor", this);
ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
}
void HomeassistantTextSensor::setup() {
api::global_api_server->subscribe_home_assistant_state(this->entity_id_, [this](std::string state) {
ESP_LOGD(TAG, "'%s': Got state '%s'", this->entity_id_.c_str(), state.c_str());
api::global_api_server->subscribe_home_assistant_state(this->entity_id_, this->attribute_, [this](std::string state) {
if (this->attribute_.has_value()) {
ESP_LOGD(TAG, "'%s::%s': Got attribute state '%s'", this->entity_id_.c_str(), this->attribute_.value().c_str(),
state.c_str());
} else {
ESP_LOGD(TAG, "'%s': Got state '%s'", this->entity_id_.c_str(), state.c_str());
}
this->publish_state(state);
});
}
void HomeassistantTextSensor::dump_config() {
LOG_TEXT_SENSOR("", "Homeassistant Text Sensor", this);
ESP_LOGCONFIG(TAG, " Entity ID: '%s'", this->entity_id_.c_str());
if (this->attribute_.has_value()) {
ESP_LOGCONFIG(TAG, " Attribute: '%s'", this->attribute_.value().c_str());
}
}
float HomeassistantTextSensor::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; }
} // namespace homeassistant
} // namespace esphome
@@ -9,11 +9,14 @@ namespace homeassistant {
class HomeassistantTextSensor : public text_sensor::TextSensor, public Component {
public:
void set_entity_id(const std::string &entity_id) { entity_id_ = entity_id; }
void dump_config() override;
void set_attribute(const std::string &attribute) { attribute_ = attribute; }
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
protected:
std::string entity_id_;
optional<std::string> attribute_;
};
} // namespace homeassistant