ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
bedjet_hub.h
Go to the documentation of this file.
1#pragma once
2#ifdef USE_ESP32
3
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
21namespace esphome::bedjet {
22
24
25// Forward declare BedJetClient
26class BedJetClient;
27
28static const espbt::ESPBTUUID BEDJET_SERVICE_UUID = espbt::ESPBTUUID::from_raw("00001000-bed0-0080-aa55-4265644a6574");
29static const espbt::ESPBTUUID BEDJET_STATUS_UUID = espbt::ESPBTUUID::from_raw("00002000-bed0-0080-aa55-4265644a6574");
30static const espbt::ESPBTUUID BEDJET_COMMAND_UUID = espbt::ESPBTUUID::from_raw("00002004-bed0-0080-aa55-4265644a6574");
31static 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
97
102 void set_status_timeout(uint32_t timeout) { this->timeout_ = timeout; }
103
104#ifdef USE_TIME
106 void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
108 void send_local_time();
109#endif
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
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);
154 void set_name_(const std::string &name) { this->name_ = name; }
155
156 std::string name_;
157
159 inline void status_packet_ready_();
160 bool force_refresh_ = false;
161 bool processing_ = false;
162
163 std::unique_ptr<BedjetCodec> codec_;
164
166 uint16_t char_handle_cmd_{0};
167 uint16_t char_handle_name_{0};
170
171 uint8_t write_notify_config_descriptor_(bool enable);
172};
173
174} // namespace esphome::bedjet
175
176#endif
bool is_ready() const
This class simplifies creating components that periodically check a state.
Definition component.h:585
Hub component connecting to the BedJet device over Bluetooth.
Definition bedjet_hub.h:36
bool set_target_temp(float temp_c)
Set the target temperature to temp_c in °C.
void set_name_(const std::string &name)
Definition bedjet_hub.h:154
const BedjetStatusPacket * get_status_packet() const
Definition bedjet_hub.h:93
void dump_config() override
bool button_cool()
Press the COOL button.
uint8_t get_fan_speed()
Return the fan speed as a percent in the range 5%-100%.
Definition bedjet_hub.h:78
void setup_time_()
Initializes time sync callbacks to support syncing current time to the BedJet.
bool button_dry()
Press the DRY button.
void set_time_id(time::RealTimeClock *time_id)
Set the time::RealTimeClock implementation.
Definition bedjet_hub.h:106
uint8_t write_bedjet_packet_(BedjetPacket *pkt)
Send the BedjetPacket to the device.
bool button_ext_heat()
Press the EXT HT button.
uint8_t get_fan_index()
Return the fan speed index, in the range 0-19.
std::unique_ptr< BedjetCodec > codec_
Definition bedjet_hub.h:163
void set_clock(uint8_t hour, uint8_t minute)
Attempt to set the BedJet device's clock to the specified time.
static const uint32_t MIN_NOTIFY_THROTTLE
Definition bedjet_hub.h:147
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
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
time::RealTimeClock * time_id_
Definition bedjet_hub.h:143
bool set_fan_index(uint8_t fan_speed_index)
Set the fan speed to a stepped index in the range 0-19.
uint16_t get_time_remaining()
Return the remaining runtime, in seconds.
bool set_time_remaining(uint8_t hours, uint8_t mins)
Set the operational runtime remaining.
void upgrade_firmware()
Attempts to check for and apply firmware updates.
void dispatch_state_(bool is_ready)
bool button_memory1()
Press the M1 (memory recall) button.
uint8_t set_notify_(bool enable)
Configures the local ESP BLE client to register (true) or unregister (false) for status notifications...
void set_status_timeout(uint32_t timeout)
Set the status timeout.
Definition bedjet_hub.h:102
std::vector< BedJetClient * > children_
Definition bedjet_hub.h:136
bool button_turbo()
Press the TURBO button.
bool button_heat()
Press the HEAT button.
static const uint32_t DEFAULT_STATUS_TIMEOUT
Definition bedjet_hub.h:149
void register_child(BedJetClient *obj)
Register a BedJetClient child component.
bool send_button(BedjetButton button)
Send the button.
uint8_t write_notify_config_descriptor_(bool enable)
Reimplementation of BLEClient.gattc_event_handler() for ESP_GATTC_REG_FOR_NOTIFY_EVT.
bool button_memory3()
Press the M3 (memory recall) button.
bool button_off()
Press the OFF button.
static const uint32_t NOTIFY_WARN_THRESHOLD
Definition bedjet_hub.h:148
float get_setup_priority() const override
Definition bedjet_hub.h:119
void send_local_time()
Attempts to sync the local time (via time_id) to the BedJet device.
bool button_memory2()
Press the M2 (memory recall) button.
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
uint8_t minute
uint8_t hour
constexpr float BLUETOOTH
Definition component.h:46
static void uint32_t
The format of a BedJet V3 status packet.