ESPHome 2025.5.0
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 {
22namespace bedjet {
23
25
26// Forward declare BedJetClient
27class BedJetClient;
28
29static const espbt::ESPBTUUID BEDJET_SERVICE_UUID = espbt::ESPBTUUID::from_raw("00001000-bed0-0080-aa55-4265644a6574");
30static const espbt::ESPBTUUID BEDJET_STATUS_UUID = espbt::ESPBTUUID::from_raw("00002000-bed0-0080-aa55-4265644a6574");
31static const espbt::ESPBTUUID BEDJET_COMMAND_UUID = espbt::ESPBTUUID::from_raw("00002004-bed0-0080-aa55-4265644a6574");
32static 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
98
103 void set_status_timeout(uint32_t timeout) { this->timeout_ = timeout; }
104
105#ifdef USE_TIME
107 void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
109 void send_local_time();
110#endif
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
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);
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
bool is_ready() const
This class simplifies creating components that periodically check a state.
Definition component.h:301
Hub component connecting to the BedJet device over Bluetooth.
Definition bedjet_hub.h:37
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:155
const BedjetStatusPacket * get_status_packet() const
Definition bedjet_hub.h:94
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:79
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:107
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:164
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:148
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:73
time::RealTimeClock * time_id_
Definition bedjet_hub.h:144
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:103
std::vector< BedJetClient * > children_
Definition bedjet_hub.h:137
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:150
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:149
float get_setup_priority() const override
Definition bedjet_hub.h:120
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::unique_ptr< T > make_unique(Args &&...args)
Definition helpers.h:85
The format of a BedJet V3 status packet.