ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ufm01.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_BINARY_SENSOR
8#endif
9#ifdef USE_SENSOR
11#endif
12#ifdef USE_TEXT_SENSOR
14#endif
16
17#include <array>
18#include <cstdint>
19
20// component API definition at https://www.sciosense.com/wp-content/uploads/2025/06/UFM-01-Datasheet-1.pdf
21
22namespace esphome::ufm01 {
23
24namespace testing {
25class TestableUFM01;
26} // namespace testing
27
28static constexpr size_t FRAME_SIZE = 32;
29static constexpr uint8_t FRAME_START_BYTE_1 = 0x3C;
30static constexpr uint8_t FRAME_START_BYTE_2 = 0x32;
31static constexpr size_t PASSIVE_FRAME_SIZE = 23;
32static constexpr size_t PASSIVE_FRAME_WITH_ID_SIZE = 39;
33#ifdef USE_UFM01_METER_ID
34static constexpr size_t PASSIVE_FRAME_MAX_SIZE = PASSIVE_FRAME_WITH_ID_SIZE;
35#else
36static constexpr size_t PASSIVE_FRAME_MAX_SIZE = PASSIVE_FRAME_SIZE;
37#endif
38static constexpr size_t SOFTWARE_VERSION_RESPONSE_SIZE = 7;
39static constexpr size_t DEVICE_ID_LENGTH = 5;
40static constexpr size_t DEVICE_ID_STRING_LENGTH = 10;
41static constexpr size_t SOFTWARE_VERSION_LENGTH = 4;
42static constexpr size_t SOFTWARE_VERSION_STRING_LENGTH = 8;
43
44enum class OperatingMode : uint8_t {
45 STARTUP = 0,
46 ACTIVE_STREAM = 1,
47 PASSIVE_POLL = 2,
48};
49
50enum class StartupPhase : uint8_t {
51 WAIT,
55#ifdef USE_UFM01_SOFTWARE_VERSION
57#endif
61};
62
63enum class ReadResult : uint8_t {
67};
68
70#ifdef USE_SENSOR
71 SUB_SENSOR(accumulated_flow)
72 SUB_SENSOR(flow)
73 SUB_SENSOR(temperature)
74#endif
75
76#ifdef USE_BINARY_SENSOR
77 SUB_BINARY_SENSOR(ufc_chip_error)
78 SUB_BINARY_SENSOR(flow_direction_wrong)
79 SUB_BINARY_SENSOR(empty_tube)
80 SUB_BINARY_SENSOR(flow_rate_out_of_range)
81#endif
82
83#ifdef USE_UFM01_METER_ID
84 SUB_TEXT_SENSOR(meter_id)
85#endif
86#ifdef USE_UFM01_SOFTWARE_VERSION
87 SUB_TEXT_SENSOR(software_version)
88#endif
89
90 public:
91 void setup() override;
92
93 void dump_config() override;
94
95 void loop() override;
96
97#ifdef USE_UFM01_CLEAR_ACCUMULATED_FLOW_ACTION
99 void request_clear_accumulated_flow();
100#endif
101
102 float get_setup_priority() const override;
103
104 private:
105#ifdef USE_UFM01_CLEAR_ACCUMULATED_FLOW_ACTION
106 bool can_start_clear_action_() const;
107 void loop_pending_clear_action_(uint32_t now);
108 void finish_pending_clear_();
109#endif
110 void send_command_no_wait_(const std::array<uint8_t, 7> &command);
111 bool consume_ack_();
112 void flush_rx_();
113 bool process_active_stream_(uint32_t now);
114 void on_active_frame_(const uint8_t data[FRAME_SIZE], uint32_t now);
115#ifdef USE_UFM01_METER_ID
116 void publish_meter_id_from_frame_(const uint8_t data[FRAME_SIZE]);
117#endif
118 void publish_stale_flow_and_temperature_();
119#ifdef USE_UFM01_SOFTWARE_VERSION
120 void start_software_version_read_(uint32_t now);
121 ReadResult continue_software_version_read_(uint32_t now);
122 void finish_software_version_read_(ReadResult result, uint32_t now);
123 bool service_software_version_(uint32_t now);
124#endif
125
126 void loop_startup_(uint32_t now);
127 void loop_active_stream_(uint32_t now);
128 void loop_passive_poll_(uint32_t now);
129 void set_mode_(OperatingMode mode, uint32_t now);
130 void set_startup_phase_(StartupPhase phase, uint32_t now);
131 void send_reset_(const LogString *status, uint32_t now);
132 void enter_active_stream_(const LogString *reason, uint32_t now);
133 void enter_passive_from_stale_(uint32_t now);
134 void restart_startup_(const LogString *reason, uint32_t now);
135 void start_passive_read_(uint32_t now);
136 ReadResult continue_passive_read_(uint32_t now);
137 void note_passive_poll_result_(bool success, uint32_t now);
138
139 uint32_t phase_start_ms_{0};
140 uint32_t startup_wait_ms_{0};
141 uint32_t last_valid_frame_ms_{0};
142 uint32_t last_poll_ms_{0};
143 // Shared by the passive read and the software version read, which never overlap
144 uint32_t passive_start_ms_{0};
145#ifdef USE_UFM01_SOFTWARE_VERSION
146 uint32_t last_software_version_attempt_ms_{0};
147#endif
148#ifdef USE_UFM01_CLEAR_ACCUMULATED_FLOW_ACTION
149 uint32_t pending_clear_start_ms_{0};
150#endif
151
152 OperatingMode operating_mode_{OperatingMode::STARTUP};
153 StartupPhase startup_phase_{StartupPhase::WAIT};
154 bool reset_retried_{false};
155 bool passive_read_pending_{false};
156 uint8_t consecutive_passive_failures_{0};
157 uint8_t passive_index_{0};
158 uint8_t passive_expected_size_{PASSIVE_FRAME_SIZE};
159 uint8_t passive_start_byte_2_{0};
160#ifdef USE_UFM01_SOFTWARE_VERSION
161 bool software_version_published_{false};
162 bool software_version_read_pending_{false};
163 uint8_t software_version_failures_{0};
164#endif
165#ifdef USE_UFM01_METER_ID
166 bool meter_id_published_{false};
167 bool meter_id_invalid_logged_{false};
168#endif
169#ifdef USE_UFM01_CLEAR_ACCUMULATED_FLOW_ACTION
170 bool clear_pending_{false};
171 bool pending_clear_sent_{false};
172#endif
173
174 uint8_t passive_frame_[PASSIVE_FRAME_MAX_SIZE];
176
177 friend class testing::TestableUFM01;
178};
179
180} // namespace esphome::ufm01
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
Collects fixed length binary frames of N bytes that start with the header bytes HEADER....
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12