ESPHome  2024.9.2
bedjet_hub.h
Go to the documentation of this file.
1 #pragma once
2 #ifdef USE_ESP32
3 
7 #include "esphome/core/defines.h"
8 #include "esphome/core/hal.h"
9 #include "bedjet_child.h"
10 #include "bedjet_codec.h"
11 
12 #include <vector>
13 
14 #ifdef USE_TIME
16 #include "esphome/core/time.h"
17 #endif
18 
19 #include <esp_gattc_api.h>
20 
21 namespace esphome {
22 namespace bedjet {
23 
25 
26 // Forward declare BedJetClient
27 class BedJetClient;
28 
29 static const espbt::ESPBTUUID BEDJET_SERVICE_UUID = espbt::ESPBTUUID::from_raw("00001000-bed0-0080-aa55-4265644a6574");
30 static const espbt::ESPBTUUID BEDJET_STATUS_UUID = espbt::ESPBTUUID::from_raw("00002000-bed0-0080-aa55-4265644a6574");
31 static const espbt::ESPBTUUID BEDJET_COMMAND_UUID = espbt::ESPBTUUID::from_raw("00002004-bed0-0080-aa55-4265644a6574");
32 static const espbt::ESPBTUUID BEDJET_NAME_UUID = espbt::ESPBTUUID::from_raw("00002001-bed0-0080-aa55-4265644a6574");
33 
38  public:
39  /* BedJet functionality exposed to `BedJetClient` children and/or accessible from action lambdas. */
40 
42  void upgrade_firmware();
43 
45  bool button_off();
47  bool button_heat();
49  bool button_ext_heat();
51  bool button_turbo();
53  bool button_cool();
55  bool button_dry();
57  bool button_memory1();
59  bool button_memory2();
61  bool button_memory3();
62 
64  bool send_button(BedjetButton button);
65 
67  bool set_target_temp(float temp_c);
68 
70  bool set_fan_index(uint8_t fan_speed_index);
71 
73  bool set_fan_speed(uint8_t fan_speed_pct) { return this->set_fan_index(bedjet_fan_speed_to_index(fan_speed_pct)); }
74 
76  uint8_t get_fan_index();
77 
79  uint8_t get_fan_speed() { return bedjet_fan_step_to_speed(this->get_fan_index()); }
80 
85  bool set_time_remaining(uint8_t hours, uint8_t mins);
86 
88  uint16_t get_time_remaining();
89 
91  bool is_connected() { return this->node_state == espbt::ClientState::ESTABLISHED; }
92 
93  bool has_status() { return this->codec_->has_status(); }
94  const BedjetStatusPacket *get_status_packet() const { return this->codec_->get_status_packet(); }
95 
97  void register_child(BedJetClient *obj);
98 
103  void set_status_timeout(uint32_t timeout) { this->timeout_ = timeout; }
104 
105 #ifdef USE_TIME
106 
107  void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
109  void send_local_time();
110 #endif
111 
112  void set_clock(uint8_t hour, uint8_t minute);
113 
114  /* Component overrides */
115 
116  void loop() override;
117  void update() override;
118  void dump_config() override;
119  void setup() override { this->codec_ = make_unique<BedjetCodec>(); }
120  float get_setup_priority() const override { return setup_priority::BLUETOOTH; }
121 
123  std::string get_name() {
124  if (this->name_.empty()) {
125  return this->parent_->address_str();
126  } else {
127  return this->name_;
128  }
129  }
130 
131  /* BLEClient overrides */
132 
133  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
134  esp_ble_gattc_cb_param_t *param) override;
135 
136  protected:
137  std::vector<BedJetClient *> children_;
138  void dispatch_status_();
139  void dispatch_state_(bool is_ready);
140 
141 #ifdef USE_TIME
142 
143  void setup_time_();
145 #endif
146 
148  static const uint32_t MIN_NOTIFY_THROTTLE = 15000;
149  static const uint32_t NOTIFY_WARN_THRESHOLD = 300000;
150  static const uint32_t DEFAULT_STATUS_TIMEOUT = 900000;
151 
152  uint8_t set_notify_(bool enable);
154  uint8_t write_bedjet_packet_(BedjetPacket *pkt);
155  void set_name_(const std::string &name) { this->name_ = name; }
156 
157  std::string name_;
158 
159  uint32_t last_notify_ = 0;
160  inline void status_packet_ready_();
161  bool force_refresh_ = false;
162  bool processing_ = false;
163 
164  std::unique_ptr<BedjetCodec> codec_;
165 
171 
172  uint8_t write_notify_config_descriptor_(bool enable);
173 };
174 
175 } // namespace bedjet
176 } // namespace esphome
177 
178 #endif
void set_name_(const std::string &name)
Definition: bedjet_hub.h:155
bool button_dry()
Press the DRY button.
Definition: bedjet_hub.cpp:51
const char * name
Definition: stm32flash.h:78
void dispatch_state_(bool is_ready)
Definition: bedjet_hub.cpp:496
void set_time_id(time::RealTimeClock *time_id)
Set the time::RealTimeClock implementation.
Definition: bedjet_hub.h:107
void setup_time_()
Initializes time sync callbacks to support syncing current time to the BedJet.
Definition: bedjet_hub.cpp:455
uint8_t write_bedjet_packet_(BedjetPacket *pkt)
Send the BedjetPacket to the device.
Definition: bedjet_hub.cpp:125
std::unique_ptr< BedjetCodec > codec_
Definition: bedjet_hub.h:164
uint8_t get_fan_index()
Return the fan speed index, in the range 0-19.
Definition: bedjet_hub.cpp:72
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
bool button_ext_heat()
Press the EXT HT button.
Definition: bedjet_hub.cpp:48
const BedjetStatusPacket * get_status_packet() const
Definition: bedjet_hub.h:94
void send_local_time()
Attempts to sync the local time (via time_id) to the BedJet device.
Definition: bedjet_hub.cpp:443
static const uint32_t MIN_NOTIFY_THROTTLE
Definition: bedjet_hub.h:148
void upgrade_firmware()
Attempts to check for and apply firmware updates.
Definition: bedjet_hub.cpp:38
This class simplifies creating components that periodically check a state.
Definition: component.h:283
bool button_memory1()
Press the M1 (memory recall) button.
Definition: bedjet_hub.cpp:53
The format of a BedJet V3 status packet.
Definition: bedjet_codec.h:39
void dump_config() override
Definition: bedjet_hub.cpp:485
uint8_t minute
bool is_ready() const
Definition: component.cpp:144
Hub component connecting to the BedJet device over Bluetooth.
Definition: bedjet_hub.h:37
uint8_t set_notify_(bool enable)
Configures the local ESP BLE client to register (true) or unregister (false) for status notifications...
Definition: bedjet_hub.cpp:141
bool button_heat()
Press the HEAT button.
Definition: bedjet_hub.cpp:47
float get_setup_priority() const override
Definition: bedjet_hub.h:120
static const uint32_t DEFAULT_STATUS_TIMEOUT
Definition: bedjet_hub.h:150
uint8_t hour
bool button_memory2()
Press the M2 (memory recall) button.
Definition: bedjet_hub.cpp:54
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition: bedjet_hub.cpp:227
bool button_memory3()
Press the M3 (memory recall) button.
Definition: bedjet_hub.cpp:55
bool set_fan_speed(uint8_t fan_speed_pct)
Set the fan speed to a percent in the range 5% - 100%, at 5% increments.
Definition: bedjet_hub.h:73
bool button_turbo()
Press the TURBO button.
Definition: bedjet_hub.cpp:49
std::vector< BedJetClient * > children_
Definition: bedjet_hub.h:137
bool set_time_remaining(uint8_t hours, uint8_t mins)
Set the operational runtime remaining.
Definition: bedjet_hub.cpp:90
bool set_target_temp(float temp_c)
Set the target temperature to temp_c in °C.
Definition: bedjet_hub.cpp:80
uint8_t get_fan_speed()
Return the fan speed as a percent in the range 5%-100%.
Definition: bedjet_hub.h:79
bool send_button(BedjetButton button)
Send the button.
Definition: bedjet_hub.cpp:101
uint16_t get_time_remaining()
Return the remaining runtime, in seconds.
Definition: bedjet_hub.cpp:115
void set_clock(uint8_t hour, uint8_t minute)
Attempt to set the BedJet device&#39;s clock to the specified time.
Definition: bedjet_hub.cpp:465
bool set_fan_index(uint8_t fan_speed_index)
Set the fan speed to a stepped index in the range 0-19.
Definition: bedjet_hub.cpp:57
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
static ESPBTUUID from_raw(const uint8_t *data)
Definition: ble_uuid.cpp:28
time::RealTimeClock * time_id_
Definition: bedjet_hub.h:144
uint8_t write_notify_config_descriptor_(bool enable)
Reimplementation of BLEClient.gattc_event_handler() for ESP_GATTC_REG_FOR_NOTIFY_EVT.
Definition: bedjet_hub.cpp:419
void set_status_timeout(uint32_t timeout)
Set the status timeout.
Definition: bedjet_hub.h:103
void setup() override
Definition: bedjet_hub.h:119
bool button_cool()
Press the COOL button.
Definition: bedjet_hub.cpp:50
void register_child(BedJetClient *obj)
Register a BedJetClient child component.
Definition: bedjet_hub.cpp:538
bool button_off()
Press the OFF button.
Definition: bedjet_hub.cpp:52
static const uint32_t NOTIFY_WARN_THRESHOLD
Definition: bedjet_hub.h:149
espbt::ClientState node_state
Definition: ble_client.h:38