ESPHome  2024.11.0
uponor_smatrix.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 
7 #include "esphome/core/defines.h"
8 
9 #ifdef USE_TIME
11 #include "esphome/core/time.h"
12 #endif
13 
14 #include <queue>
15 #include <set>
16 #include <vector>
17 
18 namespace esphome {
19 namespace uponor_smatrix {
20 
22 static const uint8_t UPONOR_ID_DATETIME1 = 0x08;
24 static const uint8_t UPONOR_ID_DATETIME2 = 0x09;
26 static const uint8_t UPONOR_ID_DATETIME3 = 0x0A;
28 static const uint8_t UPONOR_ID_UNKNOWN1 = 0x0C;
30 static const uint8_t UPONOR_ID_OUTDOOR_TEMP = 0x2D;
32 static const uint8_t UPONOR_ID_UNKNOWN2 = 0x35;
34 static const uint8_t UPONOR_ID_TARGET_TEMP_MIN = 0x37;
36 static const uint8_t UPONOR_ID_TARGET_TEMP_MAX = 0x38;
38 static const uint8_t UPONOR_ID_TARGET_TEMP = 0x3B;
40 static const uint8_t UPONOR_ID_ECO_SETBACK = 0x3C;
42 static const uint8_t UPONOR_ID_DEMAND = 0x3D;
44 static const uint8_t UPONOR_ID_MODE1 = 0x3E;
46 static const uint8_t UPONOR_ID_MODE2 = 0x3F;
48 static const uint8_t UPONOR_ID_ROOM_TEMP = 0x40;
50 static const uint8_t UPONOR_ID_EXTERNAL_TEMP = 0x41;
52 static const uint8_t UPONOR_ID_HUMIDITY = 0x42;
54 static const uint8_t UPONOR_ID_REQUEST = 0xFF;
55 
57 static const uint16_t UPONOR_INVALID_VALUE = 0x7FFF;
58 
60  uint8_t id;
61  uint16_t value;
62 };
63 
65 
67  public:
68  UponorSmatrixComponent() = default;
69 
70  void setup() override;
71  void dump_config() override;
72  void loop() override;
73 
74  void set_system_address(uint16_t address) { this->address_ = address; }
75  void register_device(UponorSmatrixDevice *device) { this->devices_.push_back(device); }
76 
77  bool send(uint16_t device_address, const UponorSmatrixData *data, size_t data_len);
78 
79 #ifdef USE_TIME
80  void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
81  void set_time_device_address(uint16_t address) { this->time_device_address_ = address; }
82  void send_time() { this->send_time_requested_ = true; }
83 #endif
84 
85  protected:
86  bool parse_byte_(uint8_t byte);
87 
88  uint16_t address_;
89  std::vector<UponorSmatrixDevice *> devices_;
90  std::set<uint16_t> unknown_devices_;
91 
92  std::vector<uint8_t> rx_buffer_;
93  std::queue<std::vector<uint8_t>> tx_queue_;
94  uint32_t last_rx_;
95  uint32_t last_tx_;
96 
97 #ifdef USE_TIME
98  time::RealTimeClock *time_id_{nullptr};
101  bool do_send_time_();
102 #endif
103 };
104 
105 class UponorSmatrixDevice : public Parented<UponorSmatrixComponent> {
106  public:
107  void set_device_address(uint16_t address) { this->address_ = address; }
108 
109  virtual void on_device_data(const UponorSmatrixData *data, size_t data_len) = 0;
110  bool send(const UponorSmatrixData *data, size_t data_len) {
111  return this->parent_->send(this->address_, data, data_len);
112  }
113 
114  protected:
116  uint16_t address_;
117 };
118 
119 inline float raw_to_celsius(uint16_t raw) {
120  return (raw == UPONOR_INVALID_VALUE) ? NAN : fahrenheit_to_celsius(raw / 10.0f);
121 }
122 
123 inline uint16_t celsius_to_raw(float celsius) {
124  return std::isnan(celsius) ? UPONOR_INVALID_VALUE
125  : static_cast<uint16_t>(lroundf(celsius_to_fahrenheit(celsius) * 10.0f));
126 }
127 
128 } // namespace uponor_smatrix
129 } // namespace esphome
void setup()
void loop()
float raw_to_celsius(uint16_t raw)
uint8_t raw[35]
Definition: bl0939.h:19
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
void register_device(UponorSmatrixDevice *device)
void set_time_id(time::RealTimeClock *time_id)
std::vector< UponorSmatrixDevice * > devices_
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition: helpers.h:466
bool send(const UponorSmatrixData *data, size_t data_len)
std::queue< std::vector< uint8_t > > tx_queue_
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition: helpers.h:468
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522
uint16_t celsius_to_raw(float celsius)