ESPHome  2023.5.5
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 #endif
17 
18 #include <esp_gattc_api.h>
19 
20 namespace esphome {
21 namespace bedjet {
22 
24 
25 // Forward declare BedJetClient
26 class BedJetClient;
27 
28 static const espbt::ESPBTUUID BEDJET_SERVICE_UUID = espbt::ESPBTUUID::from_raw("00001000-bed0-0080-aa55-4265644a6574");
29 static const espbt::ESPBTUUID BEDJET_STATUS_UUID = espbt::ESPBTUUID::from_raw("00002000-bed0-0080-aa55-4265644a6574");
30 static const espbt::ESPBTUUID BEDJET_COMMAND_UUID = espbt::ESPBTUUID::from_raw("00002004-bed0-0080-aa55-4265644a6574");
31 static const espbt::ESPBTUUID BEDJET_NAME_UUID = espbt::ESPBTUUID::from_raw("00002001-bed0-0080-aa55-4265644a6574");
32 
37  public:
38  /* BedJet functionality exposed to `BedJetClient` children and/or accessible from action lambdas. */
39 
41  void upgrade_firmware();
42 
44  bool button_off();
46  bool button_heat();
48  bool button_ext_heat();
50  bool button_turbo();
52  bool button_cool();
54  bool button_dry();
56  bool button_memory1();
58  bool button_memory2();
60  bool button_memory3();
61 
63  bool send_button(BedjetButton button);
64 
66  bool set_target_temp(float temp_c);
67 
69  bool set_fan_index(uint8_t fan_speed_index);
70 
72  bool set_fan_speed(uint8_t fan_speed_pct) { return this->set_fan_index(bedjet_fan_speed_to_index(fan_speed_pct)); }
73 
75  uint8_t get_fan_index();
76 
78  uint8_t get_fan_speed() { return bedjet_fan_step_to_speed(this->get_fan_index()); }
79 
84  bool set_time_remaining(uint8_t hours, uint8_t mins);
85 
87  uint16_t get_time_remaining();
88 
90  bool is_connected() { return this->node_state == espbt::ClientState::ESTABLISHED; }
91 
92  bool has_status() { return this->codec_->has_status(); }
93  const BedjetStatusPacket *get_status_packet() const { return this->codec_->get_status_packet(); }
94 
96  void register_child(BedJetClient *obj);
97 
102  void set_status_timeout(uint32_t timeout) { this->timeout_ = timeout; }
103 
104 #ifdef USE_TIME
105 
106  void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
108  void send_local_time();
109 #endif
110 
111  void set_clock(uint8_t hour, uint8_t minute);
112 
113  /* Component overrides */
114 
115  void loop() override;
116  void update() override;
117  void dump_config() override;
118  void setup() override { this->codec_ = make_unique<BedjetCodec>(); }
119  float get_setup_priority() const override { return setup_priority::BLUETOOTH; }
120 
122  std::string get_name() {
123  if (this->name_.empty()) {
124  return this->parent_->address_str();
125  } else {
126  return this->name_;
127  }
128  }
129 
130  /* BLEClient overrides */
131 
132  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
133  esp_ble_gattc_cb_param_t *param) override;
134 
135  protected:
136  std::vector<BedJetClient *> children_;
137  void dispatch_status_();
138  void dispatch_state_(bool is_ready);
139 
140 #ifdef USE_TIME
141 
142  void setup_time_();
144 #endif
145 
147  static const uint32_t MIN_NOTIFY_THROTTLE = 15000;
148  static const uint32_t NOTIFY_WARN_THRESHOLD = 300000;
149  static const uint32_t DEFAULT_STATUS_TIMEOUT = 900000;
150 
151  uint8_t set_notify_(bool enable);
153  uint8_t write_bedjet_packet_(BedjetPacket *pkt);
154  void set_name_(const std::string &name) { this->name_ = name; }
155 
156  std::string name_;
157 
158  uint32_t last_notify_ = 0;
159  inline void status_packet_ready_();
160  bool force_refresh_ = false;
161  bool processing_ = false;
162 
163  std::unique_ptr<BedjetCodec> codec_;
164 
170 
171  uint8_t write_notify_config_descriptor_(bool enable);
172 };
173 
174 } // namespace bedjet
175 } // namespace esphome
176 
177 #endif
void set_name_(const std::string &name)
Definition: bedjet_hub.h:154
bool button_dry()
Press the DRY button.
Definition: bedjet_hub.cpp:50
const char * name
Definition: stm32flash.h:78
void dispatch_state_(bool is_ready)
Definition: bedjet_hub.cpp:497
void set_time_id(time::RealTimeClock *time_id)
Set the time::RealTimeClock implementation.
Definition: bedjet_hub.h:106
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:124
std::unique_ptr< BedjetCodec > codec_
Definition: bedjet_hub.h:163
uint8_t get_fan_index()
Return the fan speed index, in the range 0-19.
Definition: bedjet_hub.cpp:71
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:47
const BedjetStatusPacket * get_status_packet() const
Definition: bedjet_hub.h:93
void send_local_time()
Attempts to sync the local time (via time_id) to the BedJet device.
Definition: bedjet_hub.cpp:442
static const uint32_t MIN_NOTIFY_THROTTLE
Definition: bedjet_hub.h:147
optional< time::RealTimeClock * > time_id_
Definition: bedjet_hub.h:143
void upgrade_firmware()
Attempts to check for and apply firmware updates.
Definition: bedjet_hub.cpp:37
This class simplifies creating components that periodically check a state.
Definition: component.h:282
bool button_memory1()
Press the M1 (memory recall) button.
Definition: bedjet_hub.cpp:52
The format of a BedJet V3 status packet.
Definition: bedjet_codec.h:39
void dump_config() override
Definition: bedjet_hub.cpp:486
Hub component connecting to the BedJet device over Bluetooth.
Definition: bedjet_hub.h:36
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:140
bool button_heat()
Press the HEAT button.
Definition: bedjet_hub.cpp:46
float get_setup_priority() const override
Definition: bedjet_hub.h:119
static const uint32_t DEFAULT_STATUS_TIMEOUT
Definition: bedjet_hub.h:149
bool button_memory2()
Press the M2 (memory recall) button.
Definition: bedjet_hub.cpp:53
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:226
bool button_memory3()
Press the M3 (memory recall) button.
Definition: bedjet_hub.cpp:54
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:72
bool button_turbo()
Press the TURBO button.
Definition: bedjet_hub.cpp:48
std::vector< BedJetClient * > children_
Definition: bedjet_hub.h:136
bool set_time_remaining(uint8_t hours, uint8_t mins)
Set the operational runtime remaining.
Definition: bedjet_hub.cpp:89
bool set_target_temp(float temp_c)
Set the target temperature to temp_c in °C.
Definition: bedjet_hub.cpp:79
uint8_t get_fan_speed()
Return the fan speed as a percent in the range 5%-100%.
Definition: bedjet_hub.h:78
bool send_button(BedjetButton button)
Send the button.
Definition: bedjet_hub.cpp:100
uint16_t get_time_remaining()
Return the remaining runtime, in seconds.
Definition: bedjet_hub.cpp:114
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:466
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:56
Definition: a4988.cpp:4
static ESPBTUUID from_raw(const uint8_t *data)
Definition: ble_uuid.cpp:27
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:418
void set_status_timeout(uint32_t timeout)
Set the status timeout.
Definition: bedjet_hub.h:102
void setup() override
Definition: bedjet_hub.h:118
bool button_cool()
Press the COOL button.
Definition: bedjet_hub.cpp:49
void register_child(BedJetClient *obj)
Register a BedJetClient child component.
Definition: bedjet_hub.cpp:539
bool button_off()
Press the OFF button.
Definition: bedjet_hub.cpp:51
static const uint32_t NOTIFY_WARN_THRESHOLD
Definition: bedjet_hub.h:148
espbt::ClientState node_state
Definition: ble_client.h:38