77#include < hap_apple_servs.h>
88#include < hap_apple_chars.h>
99#include " const.h"
10+ #include " hap_entity.h"
11+
1012namespace esphome
1113{
1214 namespace homekit
1315 {
14- class ClimateEntity
16+ class ClimateEntity : public HAPEntity
1517 {
1618 private:
1719 static constexpr const char * TAG = " ClimateEntity" ;
18- void on_climate_update (climate::Climate& obj) {
20+ climate::Climate* climatePtr;
21+ static void on_climate_update (climate::Climate& obj) {
1922 ESP_LOGI (TAG, " %s Mode: %d Action: %d CTemp: %.2f TTemp: %.2f CHum: %.2f THum: %.2f" , obj.get_name ().c_str (), obj.mode , obj.action , obj.current_temperature , obj.target_temperature , obj.current_humidity , obj.target_humidity );
2023 hap_acc_t * acc = hap_acc_get_by_aid (hap_get_unique_aid (std::to_string (obj.get_object_id_hash ()).c_str ()));
2124 if (acc) {
@@ -122,8 +125,8 @@ namespace esphome
122125 return HAP_SUCCESS;
123126 }
124127 public:
125- ClimateEntity () {}
126- void setup (climate::Climate* climatePtr, TemperatureUnits units = CELSIUS) {
128+ ClimateEntity (climate::Climate* climatePtr) : HAPEntity({{MODEL, " HAP-CLIMATE " }}), climatePtr(climatePtr ) {}
129+ void setup (TemperatureUnits units = CELSIUS) {
127130 hap_acc_cfg_t acc_cfg = {
128131 .model = " ESP-CLIMATE" ,
129132 .manufacturer = " rednblkx" ,
@@ -136,12 +139,12 @@ namespace esphome
136139 hap_acc_t * accessory = nullptr ;
137140 hap_serv_t * service = nullptr ;
138141 hap_serv_t * service_fan = nullptr ;
139- std::string accessory_name = climatePtr->get_name ();
142+ std::string accessory_name = this -> climatePtr ->get_name ();
140143 acc_cfg.name = accessory_name.data ();
141- acc_cfg.serial_num = std::to_string (climatePtr->get_object_id_hash ()).data ();
142- climate::ClimateTraits climateTraits = climatePtr->get_traits ();
143- climate::ClimateMode climateMode = climatePtr->mode ;
144- climate::ClimateAction climateAction = climatePtr->action ;
144+ acc_cfg.serial_num = std::to_string (this -> climatePtr ->get_object_id_hash ()).data ();
145+ climate::ClimateTraits climateTraits = this -> climatePtr ->get_traits ();
146+ climate::ClimateMode climateMode = this -> climatePtr ->mode ;
147+ climate::ClimateAction climateAction = this -> climatePtr ->action ;
145148 uint8_t current_mode = 0 ;
146149 uint8_t target_mode = 0 ;
147150 switch (climateAction) {
@@ -181,22 +184,22 @@ namespace esphome
181184 default :
182185 break ;
183186 }
184- ESP_LOGI (TAG, " CTemp: %.2f TTemp: %.2f CHum: %.2f THum: %.2f" , climatePtr->current_temperature , climatePtr->target_temperature , climatePtr->current_humidity , climatePtr->target_humidity );
185- service = hap_serv_thermostat_create (current_mode, target_mode, climatePtr->current_temperature , climatePtr->target_temperature , units);
187+ ESP_LOGI (TAG, " CTemp: %.2f TTemp: %.2f CHum: %.2f THum: %.2f" , this -> climatePtr ->current_temperature , this -> climatePtr ->target_temperature , this -> climatePtr ->current_humidity , this -> climatePtr ->target_humidity );
188+ service = hap_serv_thermostat_create (current_mode, target_mode, this -> climatePtr ->current_temperature , this -> climatePtr ->target_temperature , units);
186189 if (climateTraits.get_supports_current_humidity ()) {
187- hap_serv_add_char (service, hap_char_current_relative_humidity_create (climatePtr->current_humidity ));
190+ hap_serv_add_char (service, hap_char_current_relative_humidity_create (this -> climatePtr ->current_humidity ));
188191 }
189192 if (climateTraits.get_supports_target_humidity ()) {
190- hap_serv_add_char (service, hap_char_target_relative_humidity_create (climatePtr->target_humidity ));
193+ hap_serv_add_char (service, hap_char_target_relative_humidity_create (this -> climatePtr ->target_humidity ));
191194 }
192- // service_fan = hap_serv_fan_v2_create(!climatePtr->fan_mode ? 1 : 0);
195+ // service_fan = hap_serv_fan_v2_create(!this-> climatePtr->fan_mode ? 1 : 0);
193196 // hap_char_swing_mode_create();
194197 // hap_serv_link_serv()
195198 if (service) {
196199 /* Create accessory object */
197200 accessory = hap_acc_create (&acc_cfg);
198- ESP_LOGI (TAG, " ID HASH: %lu" , climatePtr->get_object_id_hash ());
199- hap_serv_set_priv (service, strdup (std::to_string (climatePtr->get_object_id_hash ()).c_str ()));
201+ ESP_LOGI (TAG, " ID HASH: %lu" , this -> climatePtr ->get_object_id_hash ());
202+ hap_serv_set_priv (service, strdup (std::to_string (this -> climatePtr ->get_object_id_hash ()).c_str ()));
200203
201204 /* Set the write callback for the service */
202205 hap_serv_set_write_cb (service, climate_write);
@@ -207,7 +210,7 @@ namespace esphome
207210
208211
209212 /* Add the Accessory to the HomeKit Database */
210- hap_add_bridged_accessory (accessory, hap_get_unique_aid (std::to_string (climatePtr->get_object_id_hash ()).c_str ()));
213+ hap_add_bridged_accessory (accessory, hap_get_unique_aid (std::to_string (this -> climatePtr ->get_object_id_hash ()).c_str ()));
211214 }
212215 }
213216 };
0 commit comments