From afa436fe8f6f4ffccd99157c2d9539d957c384a6 Mon Sep 17 00:00:00 2001 From: 0hax <43876620+0hax@users.noreply.github.com> Date: Tue, 1 Jun 2021 03:32:09 +0200 Subject: [PATCH] teleinfo: use text_sensor and sensor. (#1403) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: Otto winter --- esphome/components/teleinfo/__init__.py | 27 ++++++++++ esphome/components/teleinfo/sensor.py | 50 ------------------- .../components/teleinfo/sensor/__init__.py | 26 ++++++++++ .../teleinfo/sensor/teleinfo_sensor.cpp | 14 ++++++ .../teleinfo/sensor/teleinfo_sensor.h | 16 ++++++ esphome/components/teleinfo/teleinfo.cpp | 17 +++---- esphome/components/teleinfo/teleinfo.h | 13 +++-- .../teleinfo/text_sensor/__init__.py | 28 +++++++++++ .../text_sensor/teleinfo_text_sensor.cpp | 11 ++++ .../text_sensor/teleinfo_text_sensor.h | 13 +++++ tests/test1.yaml | 35 ++++++------- 11 files changed, 163 insertions(+), 87 deletions(-) delete mode 100644 esphome/components/teleinfo/sensor.py create mode 100644 esphome/components/teleinfo/sensor/__init__.py create mode 100644 esphome/components/teleinfo/sensor/teleinfo_sensor.cpp create mode 100644 esphome/components/teleinfo/sensor/teleinfo_sensor.h create mode 100644 esphome/components/teleinfo/text_sensor/__init__.py create mode 100644 esphome/components/teleinfo/text_sensor/teleinfo_text_sensor.cpp create mode 100644 esphome/components/teleinfo/text_sensor/teleinfo_text_sensor.h diff --git a/esphome/components/teleinfo/__init__.py b/esphome/components/teleinfo/__init__.py index 2e279e892e..d7bf8999ef 100644 --- a/esphome/components/teleinfo/__init__.py +++ b/esphome/components/teleinfo/__init__.py @@ -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) diff --git a/esphome/components/teleinfo/sensor.py b/esphome/components/teleinfo/sensor.py deleted file mode 100644 index d8b4dbade7..0000000000 --- a/esphome/components/teleinfo/sensor.py +++ /dev/null @@ -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)) diff --git a/esphome/components/teleinfo/sensor/__init__.py b/esphome/components/teleinfo/sensor/__init__.py new file mode 100644 index 0000000000..ffdb1509be --- /dev/null +++ b/esphome/components/teleinfo/sensor/__init__.py @@ -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)) diff --git a/esphome/components/teleinfo/sensor/teleinfo_sensor.cpp b/esphome/components/teleinfo/sensor/teleinfo_sensor.cpp new file mode 100644 index 0000000000..c00bf8f441 --- /dev/null +++ b/esphome/components/teleinfo/sensor/teleinfo_sensor.cpp @@ -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 diff --git a/esphome/components/teleinfo/sensor/teleinfo_sensor.h b/esphome/components/teleinfo/sensor/teleinfo_sensor.h new file mode 100644 index 0000000000..de46cbfd1a --- /dev/null +++ b/esphome/components/teleinfo/sensor/teleinfo_sensor.h @@ -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 diff --git a/esphome/components/teleinfo/teleinfo.cpp b/esphome/components/teleinfo/teleinfo.cpp index 7c0a83d103..bd233c9595 100644 --- a/esphome/components/teleinfo/teleinfo.cpp +++ b/esphome/components/teleinfo/teleinfo.cpp @@ -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 diff --git a/esphome/components/teleinfo/teleinfo.h b/esphome/components/teleinfo/teleinfo.h index de9cf646c4..3932c4758e 100644 --- a/esphome/components/teleinfo/teleinfo.h +++ b/esphome/components/teleinfo/teleinfo.h @@ -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 teleinfo_sensors_{}; + std::vector teleinfo_listeners_{}; protected: uint32_t baud_rate_; diff --git a/esphome/components/teleinfo/text_sensor/__init__.py b/esphome/components/teleinfo/text_sensor/__init__.py new file mode 100644 index 0000000000..b1ade4df41 --- /dev/null +++ b/esphome/components/teleinfo/text_sensor/__init__.py @@ -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)) diff --git a/esphome/components/teleinfo/text_sensor/teleinfo_text_sensor.cpp b/esphome/components/teleinfo/text_sensor/teleinfo_text_sensor.cpp new file mode 100644 index 0000000000..ac48aeabbd --- /dev/null +++ b/esphome/components/teleinfo/text_sensor/teleinfo_text_sensor.cpp @@ -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 diff --git a/esphome/components/teleinfo/text_sensor/teleinfo_text_sensor.h b/esphome/components/teleinfo/text_sensor/teleinfo_text_sensor.h new file mode 100644 index 0000000000..be355151ce --- /dev/null +++ b/esphome/components/teleinfo/text_sensor/teleinfo_text_sensor.h @@ -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 diff --git a/tests/test1.yaml b/tests/test1.yaml index 31bfc5e9f0..98e7292091 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -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