File tree 3 files changed +79
-0
lines changed
3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change
1
+ import esphome .codegen as cg
2
+ import esphome .config_validation as cv
3
+ from esphome import automation
4
+ from esphome .components import uart
5
+ from esphome .components import text_sensor
6
+ from esphome .const import CONF_ID , CONF_STATE
7
+
8
+ DEPENDENCIES = ['uart' ]
9
+
10
+ serial_ns = cg .esphome_ns .namespace ('serial' )
11
+
12
+ SerialTextSensor = serial_ns .class_ ('SerialTextSensor' , cg .Component ,
13
+ text_sensor .TextSensor , uart .UARTDevice )
14
+
15
+ CONFIG_SCHEMA = text_sensor .TEXT_SENSOR_SCHEMA .extend ({
16
+ cv .GenerateID (): cv .declare_id (SerialTextSensor ),
17
+ }).extend (uart .UART_DEVICE_SCHEMA )
18
+
19
+
20
+ def to_code (config ):
21
+ var = cg .new_Pvariable (config [CONF_ID ])
22
+ yield cg .register_component (var , config )
23
+ yield text_sensor .register_text_sensor (var , config )
24
+ yield uart .register_uart_device (var , config )
25
+
Original file line number Diff line number Diff line change
1
+ #include " serial_text_sensor.h"
2
+ #include " esphome/core/log.h"
3
+
4
+ namespace esphome {
5
+ namespace serial {
6
+
7
+ static const char *TAG = " serial.text_sensor" ;
8
+
9
+ void SerialTextSensor::loop () {
10
+ while (this ->available ()) {
11
+ uint8_t c;
12
+ this ->read_byte (&c);
13
+ this ->handle_char_ (c);
14
+ }
15
+ }
16
+
17
+ void SerialTextSensor::handle_char_ (uint8_t c) {
18
+ if (c == ' \r ' )
19
+ return ;
20
+ if (c == ' \n ' ) {
21
+ std::string s (this ->rx_message_ .begin (), this ->rx_message_ .end ());
22
+ this ->publish_state (s);
23
+ this ->rx_message_ .clear ();
24
+ return ;
25
+ }
26
+ this ->rx_message_ .push_back (c);
27
+ }
28
+
29
+ void SerialTextSensor::dump_config () { LOG_TEXT_SENSOR (" " , " Serial Sensor" , this ); }
30
+
31
+ } // namespace serial
32
+ } // namespace esphome
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ #include " esphome/core/component.h"
4
+ #include " esphome/components/text_sensor/text_sensor.h"
5
+ #include " esphome/components/uart/uart.h"
6
+
7
+ namespace esphome {
8
+ namespace serial {
9
+
10
+ class SerialTextSensor : public Component , public text_sensor ::TextSensor, public uart::UARTDevice {
11
+ public:
12
+ float get_setup_priority () const override { return setup_priority::LATE; }
13
+ void loop () override ;
14
+ void dump_config () override ;
15
+
16
+ protected:
17
+ void handle_char_ (uint8_t c);
18
+ std::vector<uint8_t > rx_message_;
19
+ };
20
+
21
+ } // namespace serial
22
+ } // namespace esphome
You can’t perform that action at this time.
0 commit comments