From e165cee15969465d514cae46f8af569b5bce66b1 Mon Sep 17 00:00:00 2001 From: White Red Date: Tue, 27 Aug 2024 19:26:59 +0300 Subject: [PATCH] option to override accessory name --- components/homekit/__init__.py | 16 +++++++--------- components/homekit/const.h | 1 + components/homekit/light.hpp | 11 ++++++++--- components/homekit/lock.cpp | 9 +++++++-- components/homekit/lock.h | 2 +- components/homekit/sensor.hpp | 11 ++++++++--- components/homekit/switch.hpp | 11 ++++++++--- test.yaml | 1 + 8 files changed, 41 insertions(+), 21 deletions(-) diff --git a/components/homekit/__init__.py b/components/homekit/__init__.py index 69083f9..c1d0360 100644 --- a/components/homekit/__init__.py +++ b/components/homekit/__init__.py @@ -35,6 +35,7 @@ } ACC_INFO = { + "name": AInfo.NAME, "model": AInfo.MODEL, "manufacturer": AInfo.MANUFACTURER, "serial_number": AInfo.SN, @@ -49,10 +50,7 @@ } ACCESSORY_INFORMATION = { - cv.Optional("model") : cv.string, - cv.Optional("manufacturer") : cv.string, - cv.Optional("serial_number") : cv.string, - cv.Optional("fw_rev") : cv.string + cv.Optional(i): cv.string for i in ACC_INFO } CONFIG_SCHEMA = cv.All(cv.Schema({ @@ -89,7 +87,7 @@ async def to_code(config): info_temp = [] for m in l["meta"]: info_temp.append([ACC_INFO[m], l["meta"][m]]) - cg.add(light_entity.setInfo([info_temp])) + cg.add(light_entity.setInfo(info_temp)) if 'sensor' in config: for l in config["sensor"]: sensor_entity = cg.Pvariable(ID(f"{l['id'].id}_hk_sensor_entity", type=SensorEntity), var.add_sensor(await cg.get_variable(l['id']), l['temp_units'])) @@ -97,7 +95,7 @@ async def to_code(config): info_temp = [] for m in l["meta"]: info_temp.append([ACC_INFO[m], l["meta"][m]]) - cg.add(sensor_entity.setInfo([info_temp])) + cg.add(sensor_entity.setInfo(info_temp)) if 'lock' in config: for l in config["lock"]: lock_entity = cg.Pvariable(ID(f"{l['id'].id}_hk_lock_entity", type=LockEntity), var.add_lock(await cg.get_variable(l['id']))) @@ -125,7 +123,7 @@ async def to_code(config): info_temp = [] for m in l["meta"]: info_temp.append([ACC_INFO[m], l["meta"][m]]) - cg.add(lock_entity.setInfo([info_temp])) + cg.add(lock_entity.setInfo(info_temp)) if "switch" in config: for l in config["switch"]: switch_entity = cg.Pvariable(ID(f"{l['id'].id}_hk_switch_entity", type=SwitchEntity), var.add_switch(await cg.get_variable(l['id']))) @@ -133,7 +131,7 @@ async def to_code(config): info_temp = [] for m in l["meta"]: info_temp.append([ACC_INFO[m], l["meta"][m]]) - cg.add(switch_entity.setInfo([info_temp])) + cg.add(switch_entity.setInfo(info_temp)) if "climate" in config: for l in config["climate"]: climate_entity = cg.Pvariable(ID(f"{l['id'].id}_hk_climate_entity", type=ClimateEntity), var.add_climate(await cg.get_variable(l['id']))) @@ -141,4 +139,4 @@ async def to_code(config): info_temp = [] for m in l["meta"]: info_temp.append([ACC_INFO[m], l["meta"][m]]) - cg.add(climate_entity.setInfo([info_temp])) \ No newline at end of file + cg.add(climate_entity.setInfo(info_temp)) \ No newline at end of file diff --git a/components/homekit/const.h b/components/homekit/const.h index 41c3f14..49fd9b0 100644 --- a/components/homekit/const.h +++ b/components/homekit/const.h @@ -20,6 +20,7 @@ namespace esphome enum AInfo { + NAME, MODEL, SN, MANUFACTURER, diff --git a/components/homekit/light.hpp b/components/homekit/light.hpp index 8a42322..74f117b 100644 --- a/components/homekit/light.hpp +++ b/components/homekit/light.hpp @@ -15,7 +15,7 @@ namespace esphome { private: static constexpr const char* TAG = "LightEntity"; - std::map accessory_info = {{MODEL, "HAP-LIGHT"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}}; + std::map accessory_info = {{NAME, NULL}, {MODEL, "HAP-LIGHT"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}}; light::LightState* lightPtr; static int light_write(hap_write_data_t write_data[], int count, void* serv_priv, void* write_priv) { light::LightState* lightPtr = (light::LightState*)serv_priv; @@ -155,9 +155,14 @@ namespace esphome hap_acc_t* accessory = nullptr; hap_serv_t* service = nullptr; std::string accessory_name = lightPtr->get_name(); - acc_cfg.name = accessory_name.data(); + if (accessory_info[NAME] == NULL) { + acc_cfg.name = strdup(accessory_name.c_str()); + } + else { + acc_cfg.name = strdup(accessory_info[NAME]); + } if (accessory_info[SN] == NULL) { - acc_cfg.serial_num = std::to_string(lightPtr->get_object_id_hash()).data(); + acc_cfg.serial_num = strdup(std::to_string(lightPtr->get_object_id_hash()).c_str()); } else { acc_cfg.serial_num = strdup(accessory_info[SN]); diff --git a/components/homekit/lock.cpp b/components/homekit/lock.cpp index 3b1da3e..180c08f 100644 --- a/components/homekit/lock.cpp +++ b/components/homekit/lock.cpp @@ -328,9 +328,14 @@ namespace esphome hap_acc_t* accessory = nullptr; hap_serv_t* lockMechanism = nullptr; std::string accessory_name = ptrToLock->get_name(); - acc_cfg.name = strdup(accessory_name.c_str()); + if (accessory_info[NAME] == NULL) { + acc_cfg.name = strdup(accessory_name.c_str()); + } + else { + acc_cfg.name = strdup(accessory_info[NAME]); + } if (accessory_info[SN] == NULL) { - acc_cfg.serial_num = std::to_string(ptrToLock->get_object_id_hash()).data(); + acc_cfg.serial_num = strdup(std::to_string(ptrToLock->get_object_id_hash()).c_str()); } else { acc_cfg.serial_num = strdup(accessory_info[SN]); diff --git a/components/homekit/lock.h b/components/homekit/lock.h index 94de50d..4714c66 100644 --- a/components/homekit/lock.h +++ b/components/homekit/lock.h @@ -24,7 +24,7 @@ namespace esphome { private: static constexpr const char* TAG = "LockEntity"; - std::map accessory_info = {{MODEL, "HAP-LOCK"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}}; + std::map accessory_info = {{NAME, NULL}, {MODEL, "HAP-LOCK"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}}; lock::Lock* ptrToLock; static nvs_handle savedHKdata; static readerData_t readerData; diff --git a/components/homekit/sensor.hpp b/components/homekit/sensor.hpp index 6f608f0..926cb5f 100644 --- a/components/homekit/sensor.hpp +++ b/components/homekit/sensor.hpp @@ -15,7 +15,7 @@ namespace esphome { private: static constexpr const char* TAG = "SensorEntity"; - std::map accessory_info = {{MODEL, "HAP-SENSOR"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}}; + std::map accessory_info = {{NAME, NULL}, {MODEL, "HAP-SENSOR"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}}; sensor::Sensor* sensorPtr; void on_sensor_update(sensor::Sensor* obj, float v) { ESP_LOGD(TAG, "%s value: %.2f", obj->get_name().c_str(), v); @@ -97,9 +97,14 @@ namespace esphome }; hap_acc_t* accessory = nullptr; std::string accessory_name = sensorPtr->get_name(); - acc_cfg.name = accessory_name.data(); + if (accessory_info[NAME] == NULL) { + acc_cfg.name = strdup(accessory_name.c_str()); + } + else { + acc_cfg.name = strdup(accessory_info[NAME]); + } if (accessory_info[SN] == NULL) { - acc_cfg.serial_num = std::to_string(sensorPtr->get_object_id_hash()).data(); + acc_cfg.serial_num = strdup(std::to_string(sensorPtr->get_object_id_hash()).c_str()); } else { acc_cfg.serial_num = strdup(accessory_info[SN]); diff --git a/components/homekit/switch.hpp b/components/homekit/switch.hpp index bef741a..9728285 100644 --- a/components/homekit/switch.hpp +++ b/components/homekit/switch.hpp @@ -14,7 +14,7 @@ namespace esphome { private: static constexpr const char* TAG = "SwitchEntity"; - std::map accessory_info = {{MODEL, "HAP-SWITCH"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}}; + std::map accessory_info = {{NAME, NULL}, {MODEL, "HAP-SWITCH"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}}; switch_::Switch* switchPtr; static int switch_write(hap_write_data_t write_data[], int count, void* serv_priv, void* write_priv) { switch_::Switch* switchPtr = (switch_::Switch*)serv_priv; @@ -72,9 +72,14 @@ namespace esphome hap_acc_t* accessory = nullptr; hap_serv_t* service = nullptr; std::string accessory_name = switchPtr->get_name(); - acc_cfg.name = accessory_name.data(); + if (accessory_info[NAME] == NULL) { + acc_cfg.name = strdup(accessory_name.c_str()); + } + else { + acc_cfg.name = strdup(accessory_info[NAME]); + } if (accessory_info[SN] == NULL) { - acc_cfg.serial_num = std::to_string(switchPtr->get_object_id_hash()).data(); + acc_cfg.serial_num = strdup(std::to_string(switchPtr->get_object_id_hash()).c_str()); } else { acc_cfg.serial_num = strdup(accessory_info[SN]); diff --git a/test.yaml b/test.yaml index 800fe0a..c6b5a45 100644 --- a/test.yaml +++ b/test.yaml @@ -136,6 +136,7 @@ homekit: light: - id: test_light meta: + name: "RGB Light" manufacturer: "AMICI&CO" model: "IGNIS" serial_number: "42424242"