teleinfo: use text_sensor and sensor. (#1403)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
0hax 2021-06-01 03:32:09 +02:00 committed by GitHub
parent 48b5ea9e59
commit afa436fe8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 163 additions and 87 deletions

View file

@ -1 +1,28 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart
from esphome.const import CONF_ID
CODEOWNERS = ["@0hax"]
teleinfo_ns = cg.esphome_ns.namespace("teleinfo")
TeleInfo = teleinfo_ns.class_("TeleInfo", cg.PollingComponent, uart.UARTDevice)
CONF_TELEINFO_ID = "teleinfo_id"
CONF_HISTORICAL_MODE = "historical_mode"
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(TeleInfo),
cv.Optional(CONF_HISTORICAL_MODE, default=False): cv.boolean,
}
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID], config[CONF_HISTORICAL_MODE])
yield cg.register_component(var, config)
yield uart.register_uart_device(var, config)

View file

@ -1,50 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, uart
from esphome.const import (
CONF_ID,
CONF_SENSOR,
DEVICE_CLASS_POWER,
ICON_EMPTY,
UNIT_WATT_HOURS,
)
DEPENDENCIES = ["uart"]
teleinfo_ns = cg.esphome_ns.namespace("teleinfo")
TeleInfo = teleinfo_ns.class_("TeleInfo", cg.PollingComponent, uart.UARTDevice)
CONF_TAG_NAME = "tag_name"
TELEINFO_TAG_SCHEMA = cv.Schema(
{
cv.Required(CONF_TAG_NAME): cv.string,
cv.Required(CONF_SENSOR): sensor.sensor_schema(
UNIT_WATT_HOURS, ICON_EMPTY, 0, DEVICE_CLASS_POWER
),
}
)
CONF_TAGS = "tags"
CONF_HISTORICAL_MODE = "historical_mode"
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(TeleInfo),
cv.Optional(CONF_HISTORICAL_MODE, default=False): cv.boolean,
cv.Optional(CONF_TAGS): cv.ensure_list(TELEINFO_TAG_SCHEMA),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID], config[CONF_HISTORICAL_MODE])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
if CONF_TAGS in config:
for tag in config[CONF_TAGS]:
sens = await sensor.new_sensor(tag[CONF_SENSOR])
cg.add(var.register_teleinfo_sensor(tag[CONF_TAG_NAME], sens))

View file

@ -0,0 +1,26 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import CONF_ID, ICON_FLASH, UNIT_WATT_HOURS
from .. import teleinfo_ns, TeleInfo, CONF_TELEINFO_ID
CONF_TAG_NAME = "tag_name"
TeleInfoSensor = teleinfo_ns.class_("TeleInfoSensor", sensor.Sensor, cg.Component)
CONFIG_SCHEMA = sensor.sensor_schema(UNIT_WATT_HOURS, ICON_FLASH, 0).extend(
{
cv.GenerateID(): cv.declare_id(TeleInfoSensor),
cv.GenerateID(CONF_TELEINFO_ID): cv.use_id(TeleInfo),
cv.Required(CONF_TAG_NAME): cv.string,
}
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID], config[CONF_TAG_NAME])
yield cg.register_component(var, config)
yield sensor.register_sensor(var, config)
teleinfo = yield cg.get_variable(config[CONF_TELEINFO_ID])
cg.add(teleinfo.register_teleinfo_listener(var))

View file

@ -0,0 +1,14 @@
#include "esphome/core/log.h"
#include "teleinfo_sensor.h"
namespace esphome {
namespace teleinfo {
static const char *TAG = "teleinfo_sensor";
TeleInfoSensor::TeleInfoSensor(const char *tag) { this->tag = std::string(tag); }
void TeleInfoSensor::publish_val(std::string val) {
auto newval = parse_float(val);
publish_state(*newval);
}
void TeleInfoSensor::dump_config() { LOG_SENSOR(" ", tag.c_str(), this); }
} // namespace teleinfo
} // namespace esphome

View file

@ -0,0 +1,16 @@
#pragma once
#include "esphome/components/teleinfo/teleinfo.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome {
namespace teleinfo {
class TeleInfoSensor : public TeleInfoListener, public sensor::Sensor, public Component {
public:
TeleInfoSensor(const char *tag);
void publish_val(std::string val) override;
void dump_config() override;
};
} // namespace teleinfo
} // namespace esphome

View file

@ -149,16 +149,14 @@ void TeleInfo::loop() {
}
}
void TeleInfo::publish_value_(std::string tag, std::string val) {
/* It will return 0 if tag is not a float. */
auto newval = parse_float(val);
for (auto element : teleinfo_sensors_)
if (tag == element->tag)
element->sensor->publish_state(*newval);
for (auto element : teleinfo_listeners_) {
if (tag != element->tag)
continue;
element->publish_val(val);
}
}
void TeleInfo::dump_config() {
ESP_LOGCONFIG(TAG, "TeleInfo:");
for (auto element : teleinfo_sensors_)
LOG_SENSOR(" ", element->tag, element->sensor);
this->check_uart_settings(baud_rate_, 1, uart::UART_CONFIG_PARITY_EVEN, 7);
}
TeleInfo::TeleInfo(bool historical_mode) {
@ -175,10 +173,7 @@ TeleInfo::TeleInfo(bool historical_mode) {
baud_rate_ = 9600;
}
}
void TeleInfo::register_teleinfo_sensor(const char *tag, sensor::Sensor *sensor) {
const TeleinfoSensorElement *teleinfo_sensor = new TeleinfoSensorElement{tag, sensor};
teleinfo_sensors_.push_back(teleinfo_sensor);
}
void TeleInfo::register_teleinfo_listener(TeleInfoListener *listener) { teleinfo_listeners_.push_back(listener); }
} // namespace teleinfo
} // namespace esphome

View file

@ -1,7 +1,6 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
@ -14,20 +13,20 @@ static const uint8_t MAX_TAG_SIZE = 64;
static const uint16_t MAX_VAL_SIZE = 256;
static const uint16_t MAX_BUF_SIZE = 1024;
struct TeleinfoSensorElement {
const char *tag;
sensor::Sensor *sensor;
class TeleInfoListener {
public:
std::string tag;
virtual void publish_val(std::string val){};
};
class TeleInfo : public PollingComponent, public uart::UARTDevice {
public:
TeleInfo(bool historical_mode);
void register_teleinfo_sensor(const char *tag, sensor::Sensor *sensors);
void register_teleinfo_listener(TeleInfoListener *listener);
void loop() override;
void setup() override;
void update() override;
void dump_config() override;
std::vector<const TeleinfoSensorElement *> teleinfo_sensors_{};
std::vector<TeleInfoListener *> teleinfo_listeners_{};
protected:
uint32_t baud_rate_;

View file

@ -0,0 +1,28 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor
from esphome.const import CONF_ID
from .. import teleinfo_ns, TeleInfo, CONF_TELEINFO_ID
CONF_TAG_NAME = "tag_name"
TeleInfoTextSensor = teleinfo_ns.class_(
"TeleInfoTextSensor", text_sensor.TextSensor, cg.Component
)
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(TeleInfoTextSensor),
cv.GenerateID(CONF_TELEINFO_ID): cv.use_id(TeleInfo),
cv.Required(CONF_TAG_NAME): cv.string,
}
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID], config[CONF_TAG_NAME])
yield cg.register_component(var, config)
yield text_sensor.register_text_sensor(var, config)
teleinfo = yield cg.get_variable(config[CONF_TELEINFO_ID])
cg.add(teleinfo.register_teleinfo_listener(var))

View file

@ -0,0 +1,11 @@
#include "esphome/core/log.h"
#include "teleinfo_text_sensor.h"
namespace esphome {
namespace teleinfo {
static const char *TAG = "teleinfo_text_sensor";
TeleInfoTextSensor::TeleInfoTextSensor(const char *tag) { this->tag = std::string(tag); }
void TeleInfoTextSensor::publish_val(std::string val) { publish_state(val); }
void TeleInfoTextSensor::dump_config() { LOG_TEXT_SENSOR(" ", tag.c_str(), this); }
} // namespace teleinfo
} // namespace esphome

View file

@ -0,0 +1,13 @@
#pragma once
#include "esphome/components/teleinfo/teleinfo.h"
#include "esphome/components/text_sensor/text_sensor.h"
namespace esphome {
namespace teleinfo {
class TeleInfoTextSensor : public TeleInfoListener, public text_sensor::TextSensor, public Component {
public:
TeleInfoTextSensor(const char *tag);
void publish_val(std::string val) override;
void dump_config() override;
};
} // namespace teleinfo
} // namespace esphome

View file

@ -888,25 +888,11 @@ sensor:
name: 'AQI'
calculation_type: 'CAQI'
- platform: teleinfo
uart_id: uart0
tags:
- tag_name: 'HCHC'
sensor:
name: 'hchc'
unit_of_measurement: 'Wh'
icon: mdi:flash
- tag_name: 'HCHP'
sensor:
name: 'hchp'
unit_of_measurement: 'Wh'
icon: mdi:flash
- tag_name: 'PAPP'
sensor:
name: 'papp'
unit_of_measurement: 'VA'
icon: mdi:flash
update_interval: 60s
historical_mode: true
tag_name: "HCHC"
name: "hchc"
unit_of_measurement: "Wh"
icon: mdi:flash
teleinfo_id: myteleinfo
- platform: mcp9808
name: 'MCP9808 Temperature'
update_interval: 15s
@ -1975,6 +1961,7 @@ display:
row_start: 0
lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height());
tm1651:
id: tm1651_battery
clk_pin: GPIO23
@ -2163,6 +2150,10 @@ text_sensor:
- platform: version
name: 'ESPHome Version No Timestamp'
hide_timestamp: True
- platform: teleinfo
tag_name: "OPTARIF"
name: "optarif"
teleinfo_id: myteleinfo
sn74hc595:
- id: 'sn74hc595_hub'
@ -2193,3 +2184,9 @@ canbus:
lambda: 'return x[0] == 0x11;'
then:
light.toggle: ${roomname}_lights
teleinfo:
id: myteleinfo
uart_id: uart0
update_interval: 60s
historical_mode: true