esphome 2024.6.6__py3-none-any.whl → 2024.7.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. esphome/components/cover/cover.h +2 -2
  2. esphome/components/esp32_camera/__init__.py +6 -3
  3. esphome/components/ethernet/ethernet_component.cpp +3 -2
  4. esphome/components/font/__init__.py +2 -28
  5. esphome/components/haier/binary_sensor/__init__.py +4 -4
  6. esphome/components/haier/button/__init__.py +1 -1
  7. esphome/components/haier/climate.py +43 -9
  8. esphome/components/haier/haier_base.cpp +4 -0
  9. esphome/components/haier/haier_base.h +11 -1
  10. esphome/components/haier/hon_climate.cpp +109 -55
  11. esphome/components/haier/hon_climate.h +7 -1
  12. esphome/components/haier/hon_packet.h +5 -0
  13. esphome/components/haier/sensor/__init__.py +5 -5
  14. esphome/components/haier/smartair2_climate.cpp +1 -0
  15. esphome/components/haier/text_sensor/__init__.py +4 -4
  16. esphome/components/heatpumpir/climate.py +8 -2
  17. esphome/components/heatpumpir/heatpumpir.cpp +6 -0
  18. esphome/components/heatpumpir/heatpumpir.h +6 -0
  19. esphome/components/http_request/update/http_request_update.cpp +6 -7
  20. esphome/components/http_request/update/http_request_update.h +0 -3
  21. esphome/components/image/__init__.py +2 -29
  22. esphome/components/improv_serial/improv_serial_component.cpp +8 -8
  23. esphome/components/mdns/__init__.py +3 -3
  24. esphome/components/mdns/mdns_component.cpp +3 -1
  25. esphome/components/mdns/mdns_component.h +3 -1
  26. esphome/components/mdns/mdns_esp32.cpp +2 -1
  27. esphome/components/mdns/mdns_esp8266.cpp +2 -1
  28. esphome/components/mdns/mdns_host.cpp +2 -1
  29. esphome/components/mdns/mdns_libretiny.cpp +2 -1
  30. esphome/components/mdns/mdns_rp2040.cpp +2 -1
  31. esphome/components/micro_wake_word/__init__.py +203 -56
  32. esphome/components/micro_wake_word/micro_wake_word.cpp +225 -275
  33. esphome/components/micro_wake_word/micro_wake_word.h +77 -107
  34. esphome/components/micro_wake_word/preprocessor_settings.h +20 -0
  35. esphome/components/micro_wake_word/streaming_model.cpp +189 -0
  36. esphome/components/micro_wake_word/streaming_model.h +84 -0
  37. esphome/components/modbus_controller/text_sensor/__init__.py +2 -1
  38. esphome/components/modbus_controller/text_sensor/modbus_textsensor.cpp +4 -1
  39. esphome/components/modbus_controller/text_sensor/modbus_textsensor.h +1 -1
  40. esphome/components/number/__init__.py +2 -0
  41. esphome/components/qspi_amoled/display.py +16 -4
  42. esphome/components/qspi_amoled/qspi_amoled.cpp +16 -0
  43. esphome/components/qspi_amoled/qspi_amoled.h +0 -3
  44. esphome/components/remote_base/dooya_protocol.cpp +4 -4
  45. esphome/components/remote_base/rc_switch_protocol.cpp +1 -1
  46. esphome/components/script/__init__.py +1 -1
  47. esphome/components/sensor/__init__.py +2 -0
  48. esphome/components/tuya/tuya.cpp +8 -2
  49. esphome/components/tuya/tuya.h +3 -1
  50. esphome/components/uart/__init__.py +72 -9
  51. esphome/components/uart/uart_component_esp32_arduino.cpp +18 -4
  52. esphome/components/uart/uart_component_esp_idf.cpp +22 -2
  53. esphome/components/uart/uart_component_host.cpp +295 -0
  54. esphome/components/uart/uart_component_host.h +38 -0
  55. esphome/components/uptime/sensor.py +44 -11
  56. esphome/components/uptime/{uptime_sensor.cpp → uptime_seconds_sensor.cpp} +11 -7
  57. esphome/components/uptime/{uptime_sensor.h → uptime_seconds_sensor.h} +2 -2
  58. esphome/components/uptime/uptime_timestamp_sensor.cpp +39 -0
  59. esphome/components/uptime/uptime_timestamp_sensor.h +30 -0
  60. esphome/components/veml7700/veml7700.cpp +1 -1
  61. esphome/components/veml7700/veml7700.h +5 -5
  62. esphome/components/voice_assistant/voice_assistant.cpp +1 -1
  63. esphome/components/wifi/wifi_component_esp_idf.cpp +1 -1
  64. esphome/components/wifi/wifi_component_pico_w.cpp +18 -2
  65. esphome/components/wireguard/__init__.py +1 -1
  66. esphome/components/x9c/output.py +7 -1
  67. esphome/const.py +2 -1
  68. esphome/core/defines.h +1 -0
  69. esphome/core/helpers.cpp +1 -1
  70. esphome/core/helpers.h +1 -1
  71. esphome/external_files.py +26 -0
  72. {esphome-2024.6.6.dist-info → esphome-2024.7.0b1.dist-info}/METADATA +1 -1
  73. {esphome-2024.6.6.dist-info → esphome-2024.7.0b1.dist-info}/RECORD +77 -71
  74. esphome/components/micro_wake_word/audio_preprocessor_int8_model_data.h +0 -493
  75. {esphome-2024.6.6.dist-info → esphome-2024.7.0b1.dist-info}/LICENSE +0 -0
  76. {esphome-2024.6.6.dist-info → esphome-2024.7.0b1.dist-info}/WHEEL +0 -0
  77. {esphome-2024.6.6.dist-info → esphome-2024.7.0b1.dist-info}/entry_points.txt +0 -0
  78. {esphome-2024.6.6.dist-info → esphome-2024.7.0b1.dist-info}/top_level.txt +0 -0
@@ -65,13 +65,10 @@ class QspiAmoLed : public display::DisplayBuffer,
65
65
 
66
66
  void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
67
67
  void set_enable_pin(GPIOPin *enable_pin) { this->enable_pin_ = enable_pin; }
68
- void set_width(uint16_t width) { this->width_ = width; }
69
68
  void set_dimensions(uint16_t width, uint16_t height) {
70
69
  this->width_ = width;
71
70
  this->height_ = height;
72
71
  }
73
- int get_width() override { return this->width_; }
74
- int get_height() override { return this->height_; }
75
72
  void set_invert_colors(bool invert_colors) {
76
73
  this->invert_colors_ = invert_colors;
77
74
  this->reset_params_();
@@ -8,10 +8,10 @@ static const char *const TAG = "remote.dooya";
8
8
 
9
9
  static const uint32_t HEADER_HIGH_US = 5000;
10
10
  static const uint32_t HEADER_LOW_US = 1500;
11
- static const uint32_t BIT_ZERO_HIGH_US = 750;
12
- static const uint32_t BIT_ZERO_LOW_US = 350;
13
- static const uint32_t BIT_ONE_HIGH_US = 350;
14
- static const uint32_t BIT_ONE_LOW_US = 750;
11
+ static const uint32_t BIT_ZERO_HIGH_US = 350;
12
+ static const uint32_t BIT_ZERO_LOW_US = 750;
13
+ static const uint32_t BIT_ONE_HIGH_US = 750;
14
+ static const uint32_t BIT_ONE_LOW_US = 350;
15
15
 
16
16
  void DooyaProtocol::encode(RemoteTransmitData *dst, const DooyaData &data) {
17
17
  dst->set_carrier_frequency(0);
@@ -54,7 +54,7 @@ void RCSwitchBase::sync(RemoteTransmitData *dst) const {
54
54
  }
55
55
  }
56
56
  void RCSwitchBase::transmit(RemoteTransmitData *dst, uint64_t code, uint8_t len) const {
57
- dst->set_carrier_frequency(0);
57
+ dst->set_carrier_frequency(38000);
58
58
  this->sync(dst);
59
59
  for (int16_t i = len - 1; i >= 0; i--) {
60
60
  if (code & ((uint64_t) 1 << i)) {
@@ -88,7 +88,7 @@ def validate_parameter_name(value):
88
88
  raise cv.Invalid(f"Script's parameter name cannot be {CONF_ID}")
89
89
 
90
90
 
91
- ALLOWED_PARAM_TYPE_CHARSET = set("abcdefghijklmnopqrstuvwxyz0123456789_:*&[]")
91
+ ALLOWED_PARAM_TYPE_CHARSET = set("abcdefghijklmnopqrstuvwxyz0123456789_:*&[]<>")
92
92
 
93
93
 
94
94
  def validate_parameter_type(value):
@@ -43,6 +43,7 @@ from esphome.const import (
43
43
  DEVICE_CLASS_BATTERY,
44
44
  DEVICE_CLASS_CARBON_DIOXIDE,
45
45
  DEVICE_CLASS_CARBON_MONOXIDE,
46
+ DEVICE_CLASS_CONDUCTIVITY,
46
47
  DEVICE_CLASS_CURRENT,
47
48
  DEVICE_CLASS_DATA_RATE,
48
49
  DEVICE_CLASS_DATA_SIZE,
@@ -103,6 +104,7 @@ DEVICE_CLASSES = [
103
104
  DEVICE_CLASS_BATTERY,
104
105
  DEVICE_CLASS_CARBON_DIOXIDE,
105
106
  DEVICE_CLASS_CARBON_MONOXIDE,
107
+ DEVICE_CLASS_CONDUCTIVITY,
106
108
  DEVICE_CLASS_CURRENT,
107
109
  DEVICE_CLASS_DATA_RATE,
108
110
  DEVICE_CLASS_DATA_SIZE,
@@ -223,13 +223,19 @@ void Tuya::handle_command_(uint8_t command, uint8_t version, const uint8_t *buff
223
223
  break;
224
224
  case TuyaCommandType::DATAPOINT_DELIVER:
225
225
  break;
226
- case TuyaCommandType::DATAPOINT_REPORT:
226
+ case TuyaCommandType::DATAPOINT_REPORT_ASYNC:
227
+ case TuyaCommandType::DATAPOINT_REPORT_SYNC:
227
228
  if (this->init_state_ == TuyaInitState::INIT_DATAPOINT) {
228
229
  this->init_state_ = TuyaInitState::INIT_DONE;
229
230
  this->set_timeout("datapoint_dump", 1000, [this] { this->dump_config(); });
230
231
  this->initialized_callback_.call();
231
232
  }
232
233
  this->handle_datapoints_(buffer, len);
234
+
235
+ if (command_type == TuyaCommandType::DATAPOINT_REPORT_SYNC) {
236
+ this->send_command_(
237
+ TuyaCommand{.cmd = TuyaCommandType::DATAPOINT_REPORT_ACK, .payload = std::vector<uint8_t>{0x01}});
238
+ }
233
239
  break;
234
240
  case TuyaCommandType::DATAPOINT_QUERY:
235
241
  break;
@@ -423,7 +429,7 @@ void Tuya::send_raw_command_(TuyaCommand command) {
423
429
  break;
424
430
  case TuyaCommandType::DATAPOINT_DELIVER:
425
431
  case TuyaCommandType::DATAPOINT_QUERY:
426
- this->expected_response_ = TuyaCommandType::DATAPOINT_REPORT;
432
+ this->expected_response_ = TuyaCommandType::DATAPOINT_REPORT_ASYNC;
427
433
  break;
428
434
  default:
429
435
  break;
@@ -53,10 +53,12 @@ enum class TuyaCommandType : uint8_t {
53
53
  WIFI_RESET = 0x04,
54
54
  WIFI_SELECT = 0x05,
55
55
  DATAPOINT_DELIVER = 0x06,
56
- DATAPOINT_REPORT = 0x07,
56
+ DATAPOINT_REPORT_ASYNC = 0x07,
57
57
  DATAPOINT_QUERY = 0x08,
58
58
  WIFI_TEST = 0x0E,
59
59
  LOCAL_TIME_QUERY = 0x1C,
60
+ DATAPOINT_REPORT_SYNC = 0x22,
61
+ DATAPOINT_REPORT_ACK = 0x23,
60
62
  WIFI_RSSI = 0x24,
61
63
  VACUUM_MAP_UPLOAD = 0x28,
62
64
  GET_NETWORK_STATUS = 0x2B,
@@ -1,5 +1,5 @@
1
1
  from typing import Optional
2
-
2
+ import re
3
3
  import esphome.codegen as cg
4
4
  import esphome.config_validation as cv
5
5
  import esphome.final_validate as fv
@@ -11,6 +11,7 @@ from esphome.const import (
11
11
  CONF_NUMBER,
12
12
  CONF_RX_PIN,
13
13
  CONF_TX_PIN,
14
+ CONF_PORT,
14
15
  CONF_UART_ID,
15
16
  CONF_DATA,
16
17
  CONF_RX_BUFFER_SIZE,
@@ -27,6 +28,7 @@ from esphome.const import (
27
28
  CONF_DUMMY_RECEIVER,
28
29
  CONF_DUMMY_RECEIVER_ID,
29
30
  CONF_LAMBDA,
31
+ PLATFORM_HOST,
30
32
  )
31
33
  from esphome.core import CORE
32
34
 
@@ -45,6 +47,7 @@ RP2040UartComponent = uart_ns.class_("RP2040UartComponent", UARTComponent, cg.Co
45
47
  LibreTinyUARTComponent = uart_ns.class_(
46
48
  "LibreTinyUARTComponent", UARTComponent, cg.Component
47
49
  )
50
+ HostUartComponent = uart_ns.class_("HostUartComponent", UARTComponent, cg.Component)
48
51
 
49
52
  NATIVE_UART_CLASSES = (
50
53
  str(IDFUARTComponent),
@@ -54,6 +57,39 @@ NATIVE_UART_CLASSES = (
54
57
  str(LibreTinyUARTComponent),
55
58
  )
56
59
 
60
+ HOST_BAUD_RATES = [
61
+ 50,
62
+ 75,
63
+ 110,
64
+ 134,
65
+ 150,
66
+ 200,
67
+ 300,
68
+ 600,
69
+ 1200,
70
+ 1800,
71
+ 2400,
72
+ 4800,
73
+ 9600,
74
+ 19200,
75
+ 38400,
76
+ 57600,
77
+ 115200,
78
+ 230400,
79
+ 460800,
80
+ 500000,
81
+ 576000,
82
+ 921600,
83
+ 1000000,
84
+ 1152000,
85
+ 1500000,
86
+ 2000000,
87
+ 2500000,
88
+ 3000000,
89
+ 3500000,
90
+ 4000000,
91
+ ]
92
+
57
93
  UARTDevice = uart_ns.class_("UARTDevice")
58
94
  UARTWriteAction = uart_ns.class_("UARTWriteAction", automation.Action)
59
95
  UARTDebugger = uart_ns.class_("UARTDebugger", cg.Component, automation.Action)
@@ -95,6 +131,20 @@ def validate_invert_esp32(config):
95
131
  return config
96
132
 
97
133
 
134
+ def validate_host_config(config):
135
+ if CORE.is_host:
136
+ if CONF_TX_PIN in config or CONF_RX_PIN in config:
137
+ raise cv.Invalid(
138
+ "TX and RX pins are not supported for UART on host platform."
139
+ )
140
+ if config[CONF_BAUD_RATE] not in HOST_BAUD_RATES:
141
+ raise cv.Invalid(
142
+ f"Host platform doesn't support baud rate {config[CONF_BAUD_RATE]}",
143
+ path=[CONF_BAUD_RATE],
144
+ )
145
+ return config
146
+
147
+
98
148
  def _uart_declare_type(value):
99
149
  if CORE.is_esp8266:
100
150
  return cv.declare_id(ESP8266UartComponent)(value)
@@ -107,6 +157,8 @@ def _uart_declare_type(value):
107
157
  return cv.declare_id(RP2040UartComponent)(value)
108
158
  if CORE.is_libretiny:
109
159
  return cv.declare_id(LibreTinyUARTComponent)(value)
160
+ if CORE.is_host:
161
+ return cv.declare_id(HostUartComponent)(value)
110
162
  raise NotImplementedError
111
163
 
112
164
 
@@ -149,6 +201,12 @@ def maybe_empty_debug(value):
149
201
  return DEBUG_SCHEMA(value)
150
202
 
151
203
 
204
+ def validate_port(value):
205
+ if not re.match(r"^/(?:[^/]+/)[^/]+$", value):
206
+ raise cv.Invalid("Port must be a valid device path")
207
+ return value
208
+
209
+
152
210
  DEBUG_SCHEMA = cv.Schema(
153
211
  {
154
212
  cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(UARTDebugger),
@@ -181,6 +239,7 @@ CONFIG_SCHEMA = cv.All(
181
239
  cv.Required(CONF_BAUD_RATE): cv.int_range(min=1),
182
240
  cv.Optional(CONF_TX_PIN): pins.internal_gpio_output_pin_schema,
183
241
  cv.Optional(CONF_RX_PIN): validate_rx_pin,
242
+ cv.Optional(CONF_PORT): cv.All(validate_port, cv.only_on(PLATFORM_HOST)),
184
243
  cv.Optional(CONF_RX_BUFFER_SIZE, default=256): cv.validate_bytes,
185
244
  cv.Optional(CONF_STOP_BITS, default=1): cv.one_of(1, 2, int=True),
186
245
  cv.Optional(CONF_DATA_BITS, default=8): cv.int_range(min=5, max=8),
@@ -193,8 +252,9 @@ CONFIG_SCHEMA = cv.All(
193
252
  cv.Optional(CONF_DEBUG): maybe_empty_debug,
194
253
  }
195
254
  ).extend(cv.COMPONENT_SCHEMA),
196
- cv.has_at_least_one_key(CONF_TX_PIN, CONF_RX_PIN),
255
+ cv.has_at_least_one_key(CONF_TX_PIN, CONF_RX_PIN, CONF_PORT),
197
256
  validate_invert_esp32,
257
+ validate_host_config,
198
258
  )
199
259
 
200
260
 
@@ -236,6 +296,8 @@ async def to_code(config):
236
296
  if CONF_RX_PIN in config:
237
297
  rx_pin = await cg.gpio_pin_expression(config[CONF_RX_PIN])
238
298
  cg.add(var.set_rx_pin(rx_pin))
299
+ if CONF_PORT in config:
300
+ cg.add(var.set_name(config[CONF_PORT]))
239
301
  cg.add(var.set_rx_buffer_size(config[CONF_RX_BUFFER_SIZE]))
240
302
  cg.add(var.set_stop_bits(config[CONF_STOP_BITS]))
241
303
  cg.add(var.set_data_bits(config[CONF_DATA_BITS]))
@@ -258,6 +320,7 @@ KEY_UART_DEVICES = "uart_devices"
258
320
  def final_validate_device_schema(
259
321
  name: str,
260
322
  *,
323
+ uart_bus: str = CONF_UART_ID,
261
324
  baud_rate: Optional[int] = None,
262
325
  require_tx: bool = False,
263
326
  require_rx: bool = False,
@@ -268,7 +331,7 @@ def final_validate_device_schema(
268
331
  def validate_baud_rate(value):
269
332
  if value != baud_rate:
270
333
  raise cv.Invalid(
271
- f"Component {name} requires baud rate {baud_rate} for the uart bus"
334
+ f"Component {name} requires baud rate {baud_rate} for the uart referenced by {uart_bus}"
272
335
  )
273
336
  return value
274
337
 
@@ -287,21 +350,21 @@ def final_validate_device_schema(
287
350
  def validate_data_bits(value):
288
351
  if value != data_bits:
289
352
  raise cv.Invalid(
290
- f"Component {name} requires {data_bits} data bits for the uart bus"
353
+ f"Component {name} requires {data_bits} data bits for the uart referenced by {uart_bus}"
291
354
  )
292
355
  return value
293
356
 
294
357
  def validate_parity(value):
295
358
  if value != parity:
296
359
  raise cv.Invalid(
297
- f"Component {name} requires parity {parity} for the uart bus"
360
+ f"Component {name} requires parity {parity} for the uart referenced by {uart_bus}"
298
361
  )
299
362
  return value
300
363
 
301
364
  def validate_stop_bits(value):
302
365
  if value != stop_bits:
303
366
  raise cv.Invalid(
304
- f"Component {name} requires {stop_bits} stop bits for the uart bus"
367
+ f"Component {name} requires {stop_bits} stop bits for the uart referenced by {uart_bus}"
305
368
  )
306
369
  return value
307
370
 
@@ -316,14 +379,14 @@ def final_validate_device_schema(
316
379
  hub_schema[
317
380
  cv.Required(
318
381
  CONF_TX_PIN,
319
- msg=f"Component {name} requires this uart bus to declare a tx_pin",
382
+ msg=f"Component {name} requires uart referenced by {uart_bus} to declare a tx_pin",
320
383
  )
321
384
  ] = validate_pin(CONF_TX_PIN, device)
322
385
  if require_rx and uart_id_type_str in NATIVE_UART_CLASSES:
323
386
  hub_schema[
324
387
  cv.Required(
325
388
  CONF_RX_PIN,
326
- msg=f"Component {name} requires this uart bus to declare a rx_pin",
389
+ msg=f"Component {name} requires uart referenced by {uart_bus} to declare a rx_pin",
327
390
  )
328
391
  ] = validate_pin(CONF_RX_PIN, device)
329
392
  if baud_rate is not None:
@@ -337,7 +400,7 @@ def final_validate_device_schema(
337
400
  return cv.Schema(hub_schema, extra=cv.ALLOW_EXTRA)(hub_config)
338
401
 
339
402
  return cv.Schema(
340
- {cv.Required(CONF_UART_ID): fv.id_declaration_match_schema(validate_hub)},
403
+ {cv.Required(uart_bus): fv.id_declaration_match_schema(validate_hub)},
341
404
  extra=cv.ALLOW_EXTRA,
342
405
  )
343
406
 
@@ -96,10 +96,24 @@ void ESP32ArduinoUARTComponent::setup() {
96
96
  next_uart_num++;
97
97
  } else {
98
98
  #ifdef USE_LOGGER
99
- // The logger doesn't use this UART component, instead it targets the UARTs
100
- // directly (i.e. Serial/Serial0, Serial1, and Serial2). If the logger is
101
- // enabled, skip the UART that it is configured to use.
102
- if (logger::global_logger->get_baud_rate() > 0 && logger::global_logger->get_uart() == next_uart_num) {
99
+ bool logger_uses_hardware_uart = true;
100
+
101
+ #ifdef USE_LOGGER_USB_CDC
102
+ if (logger::global_logger->get_uart() == logger::UART_SELECTION_USB_CDC) {
103
+ // this is not a hardware UART, ignore it
104
+ logger_uses_hardware_uart = false;
105
+ }
106
+ #endif // USE_LOGGER_USB_CDC
107
+
108
+ #ifdef USE_LOGGER_USB_SERIAL_JTAG
109
+ if (logger::global_logger->get_uart() == logger::UART_SELECTION_USB_SERIAL_JTAG) {
110
+ // this is not a hardware UART, ignore it
111
+ logger_uses_hardware_uart = false;
112
+ }
113
+ #endif // USE_LOGGER_USB_SERIAL_JTAG
114
+
115
+ if (logger_uses_hardware_uart && logger::global_logger->get_baud_rate() > 0 &&
116
+ logger::global_logger->get_uart() == next_uart_num) {
103
117
  next_uart_num++;
104
118
  }
105
119
  #endif // USE_LOGGER
@@ -60,10 +60,30 @@ uart_config_t IDFUARTComponent::get_config_() {
60
60
 
61
61
  void IDFUARTComponent::setup() {
62
62
  static uint8_t next_uart_num = 0;
63
+
63
64
  #ifdef USE_LOGGER
64
- if (logger::global_logger->get_uart_num() == next_uart_num)
65
+ bool logger_uses_hardware_uart = true;
66
+
67
+ #ifdef USE_LOGGER_USB_CDC
68
+ if (logger::global_logger->get_uart() == logger::UART_SELECTION_USB_CDC) {
69
+ // this is not a hardware UART, ignore it
70
+ logger_uses_hardware_uart = false;
71
+ }
72
+ #endif // USE_LOGGER_USB_CDC
73
+
74
+ #ifdef USE_LOGGER_USB_SERIAL_JTAG
75
+ if (logger::global_logger->get_uart() == logger::UART_SELECTION_USB_SERIAL_JTAG) {
76
+ // this is not a hardware UART, ignore it
77
+ logger_uses_hardware_uart = false;
78
+ }
79
+ #endif // USE_LOGGER_USB_SERIAL_JTAG
80
+
81
+ if (logger_uses_hardware_uart && logger::global_logger->get_baud_rate() > 0 &&
82
+ logger::global_logger->get_uart_num() == next_uart_num) {
65
83
  next_uart_num++;
66
- #endif
84
+ }
85
+ #endif // USE_LOGGER
86
+
67
87
  if (next_uart_num >= UART_NUM_MAX) {
68
88
  ESP_LOGW(TAG, "Maximum number of UART components created already.");
69
89
  this->mark_failed();
@@ -0,0 +1,295 @@
1
+ #ifdef USE_HOST
2
+ #include "uart_component_host.h"
3
+ #include "esphome/core/application.h"
4
+ #include "esphome/core/defines.h"
5
+ #include "esphome/core/helpers.h"
6
+ #include "esphome/core/log.h"
7
+
8
+ #ifndef __linux__
9
+ #error This HostUartComponent implementation is only for Linux
10
+ #endif
11
+
12
+ #include <stdio.h>
13
+ #include <string.h>
14
+ #include <unistd.h>
15
+ #include <fcntl.h>
16
+ #include <errno.h>
17
+ #include <termios.h>
18
+ #include <sys/ioctl.h>
19
+
20
+ #ifdef USE_LOGGER
21
+ #include "esphome/components/logger/logger.h"
22
+ #endif
23
+
24
+ namespace {
25
+
26
+ speed_t get_baud(int baud) {
27
+ switch (baud) {
28
+ case 50:
29
+ return B50;
30
+ case 75:
31
+ return B75;
32
+ case 110:
33
+ return B110;
34
+ case 134:
35
+ return B134;
36
+ case 150:
37
+ return B150;
38
+ case 200:
39
+ return B200;
40
+ case 300:
41
+ return B300;
42
+ case 600:
43
+ return B600;
44
+ case 1200:
45
+ return B1200;
46
+ case 1800:
47
+ return B1800;
48
+ case 2400:
49
+ return B2400;
50
+ case 4800:
51
+ return B4800;
52
+ case 9600:
53
+ return B9600;
54
+ case 19200:
55
+ return B19200;
56
+ case 38400:
57
+ return B38400;
58
+ case 57600:
59
+ return B57600;
60
+ case 115200:
61
+ return B115200;
62
+ case 230400:
63
+ return B230400;
64
+ case 460800:
65
+ return B460800;
66
+ case 500000:
67
+ return B500000;
68
+ case 576000:
69
+ return B576000;
70
+ case 921600:
71
+ return B921600;
72
+ case 1000000:
73
+ return B1000000;
74
+ case 1152000:
75
+ return B1152000;
76
+ case 1500000:
77
+ return B1500000;
78
+ case 2000000:
79
+ return B2000000;
80
+ case 2500000:
81
+ return B2500000;
82
+ case 3000000:
83
+ return B3000000;
84
+ case 3500000:
85
+ return B3500000;
86
+ case 4000000:
87
+ return B4000000;
88
+ default:
89
+ return B0;
90
+ }
91
+ }
92
+
93
+ } // namespace
94
+
95
+ namespace esphome {
96
+ namespace uart {
97
+
98
+ static const char *const TAG = "uart.host";
99
+
100
+ HostUartComponent::~HostUartComponent() {
101
+ if (this->file_descriptor_ != -1) {
102
+ close(this->file_descriptor_);
103
+ this->file_descriptor_ = -1;
104
+ }
105
+ }
106
+
107
+ void HostUartComponent::setup() {
108
+ ESP_LOGCONFIG(TAG, "Opening UART port...");
109
+ speed_t baud = get_baud(this->baud_rate_);
110
+ if (baud == B0) {
111
+ ESP_LOGE(TAG, "Unsupported baud rate: %d", this->baud_rate_);
112
+ this->mark_failed();
113
+ return;
114
+ }
115
+ this->file_descriptor_ = ::open(this->port_name_.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
116
+ if (this->file_descriptor_ == -1) {
117
+ this->update_error_(strerror(errno));
118
+ this->mark_failed();
119
+ return;
120
+ }
121
+ fcntl(this->file_descriptor_, F_SETFL, 0);
122
+ struct termios options;
123
+ tcgetattr(this->file_descriptor_, &options);
124
+ options.c_cflag &= ~CRTSCTS;
125
+ options.c_cflag |= CREAD | CLOCAL;
126
+ options.c_lflag &= ~ICANON;
127
+ options.c_lflag &= ~ECHO;
128
+ options.c_lflag &= ~ECHOE;
129
+ options.c_lflag &= ~ECHONL;
130
+ options.c_lflag &= ~ISIG;
131
+ options.c_iflag &= ~(IXON | IXOFF | IXANY);
132
+ options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
133
+ options.c_oflag &= ~OPOST;
134
+ options.c_oflag &= ~ONLCR;
135
+ // Set data bits
136
+ options.c_cflag &= ~CSIZE; // Mask the character size bits
137
+ switch (this->data_bits_) {
138
+ case 5:
139
+ options.c_cflag |= CS5;
140
+ break;
141
+ case 6:
142
+ options.c_cflag |= CS6;
143
+ break;
144
+ case 7:
145
+ options.c_cflag |= CS7;
146
+ break;
147
+ case 8:
148
+ default:
149
+ options.c_cflag |= CS8;
150
+ break;
151
+ }
152
+ // Set parity
153
+ switch (this->parity_) {
154
+ case UART_CONFIG_PARITY_NONE:
155
+ options.c_cflag &= ~PARENB;
156
+ break;
157
+ case UART_CONFIG_PARITY_EVEN:
158
+ options.c_cflag |= PARENB;
159
+ options.c_cflag &= ~PARODD;
160
+ break;
161
+ case UART_CONFIG_PARITY_ODD:
162
+ options.c_cflag |= PARENB;
163
+ options.c_cflag |= PARODD;
164
+ break;
165
+ };
166
+ // Set stop bits
167
+ if (this->stop_bits_ == 2) {
168
+ options.c_cflag |= CSTOPB;
169
+ } else {
170
+ options.c_cflag &= ~CSTOPB;
171
+ }
172
+ cfsetispeed(&options, baud);
173
+ cfsetospeed(&options, baud);
174
+ tcsetattr(this->file_descriptor_, TCSANOW, &options);
175
+ }
176
+
177
+ void HostUartComponent::dump_config() {
178
+ ESP_LOGCONFIG(TAG, "UART:");
179
+ ESP_LOGCONFIG(TAG, " Port: %s", this->port_name_.c_str());
180
+ if (this->file_descriptor_ == -1) {
181
+ ESP_LOGCONFIG(TAG, " Port status: Not opened");
182
+ if (!this->first_error_.empty()) {
183
+ ESP_LOGCONFIG(TAG, " Error: %s", this->first_error_.c_str());
184
+ }
185
+ return;
186
+ }
187
+ ESP_LOGCONFIG(TAG, " Port status: opened");
188
+ ESP_LOGCONFIG(TAG, " Baud Rate: %d", this->baud_rate_);
189
+ ESP_LOGCONFIG(TAG, " Data Bits: %d", this->data_bits_);
190
+ ESP_LOGCONFIG(TAG, " Parity: %s",
191
+ this->parity_ == UART_CONFIG_PARITY_NONE ? "None"
192
+ : this->parity_ == UART_CONFIG_PARITY_EVEN ? "Even"
193
+ : "Odd");
194
+ ESP_LOGCONFIG(TAG, " Stop Bits: %d", this->stop_bits_);
195
+ this->check_logger_conflict();
196
+ }
197
+
198
+ void HostUartComponent::write_array(const uint8_t *data, size_t len) {
199
+ if (this->file_descriptor_ == -1) {
200
+ return;
201
+ }
202
+ size_t written = ::write(this->file_descriptor_, data, len);
203
+ if (written != len) {
204
+ this->update_error_(strerror(errno));
205
+ return;
206
+ }
207
+ #ifdef USE_UART_DEBUGGER
208
+ for (size_t i = 0; i < len; i++) {
209
+ this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
210
+ }
211
+ #endif
212
+ return;
213
+ }
214
+
215
+ bool HostUartComponent::peek_byte(uint8_t *data) {
216
+ if (this->file_descriptor_ == -1) {
217
+ return false;
218
+ }
219
+ if (!this->has_peek_) {
220
+ if (!this->check_read_timeout_()) {
221
+ return false;
222
+ }
223
+ if (::read(this->file_descriptor_, &this->peek_byte_, 1) != 1) {
224
+ this->update_error_(strerror(errno));
225
+ return false;
226
+ }
227
+ this->has_peek_ = true;
228
+ }
229
+ *data = this->peek_byte_;
230
+ return true;
231
+ }
232
+
233
+ bool HostUartComponent::read_array(uint8_t *data, size_t len) {
234
+ if ((this->file_descriptor_ == -1) || (len == 0)) {
235
+ return false;
236
+ }
237
+ if (!this->check_read_timeout_(len))
238
+ return false;
239
+ uint8_t *data_ptr = data;
240
+ size_t length_to_read = len;
241
+ if (this->has_peek_) {
242
+ length_to_read--;
243
+ *data_ptr = this->peek_byte_;
244
+ data_ptr++;
245
+ this->has_peek_ = false;
246
+ }
247
+ if (length_to_read > 0) {
248
+ int sz = ::read(this->file_descriptor_, data_ptr, length_to_read);
249
+ if (sz == -1) {
250
+ this->update_error_(strerror(errno));
251
+ return false;
252
+ }
253
+ }
254
+ #ifdef USE_UART_DEBUGGER
255
+ for (size_t i = 0; i < len; i++) {
256
+ this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
257
+ }
258
+ #endif
259
+ return true;
260
+ }
261
+
262
+ int HostUartComponent::available() {
263
+ if (this->file_descriptor_ == -1) {
264
+ return 0;
265
+ }
266
+ int available;
267
+ int res = ioctl(this->file_descriptor_, FIONREAD, &available);
268
+ if (res == -1) {
269
+ this->update_error_(strerror(errno));
270
+ return 0;
271
+ }
272
+ if (this->has_peek_)
273
+ available++;
274
+ return available;
275
+ };
276
+
277
+ void HostUartComponent::flush() {
278
+ if (this->file_descriptor_ == -1) {
279
+ return;
280
+ }
281
+ tcflush(this->file_descriptor_, TCIOFLUSH);
282
+ ESP_LOGV(TAG, " Flushing...");
283
+ }
284
+
285
+ void HostUartComponent::update_error_(const std::string &error) {
286
+ if (this->first_error_.empty()) {
287
+ this->first_error_ = error;
288
+ }
289
+ ESP_LOGE(TAG, "Port error: %s", error.c_str());
290
+ }
291
+
292
+ } // namespace uart
293
+ } // namespace esphome
294
+
295
+ #endif // USE_HOST