Skip to content

Commit

Permalink
option to override accessory name
Browse files Browse the repository at this point in the history
  • Loading branch information
rednblkx committed Aug 27, 2024
1 parent f5b8e3f commit e165cee
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
16 changes: 7 additions & 9 deletions components/homekit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
}

ACC_INFO = {
"name": AInfo.NAME,
"model": AInfo.MODEL,
"manufacturer": AInfo.MANUFACTURER,
"serial_number": AInfo.SN,
Expand All @@ -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({
Expand Down Expand Up @@ -89,15 +87,15 @@ 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']))
if "meta" in l:
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'])))
Expand Down Expand Up @@ -125,20 +123,20 @@ 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'])))
if "meta" in l:
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'])))
if "meta" in l:
info_temp = []
for m in l["meta"]:
info_temp.append([ACC_INFO[m], l["meta"][m]])
cg.add(climate_entity.setInfo([info_temp]))
cg.add(climate_entity.setInfo(info_temp))
1 change: 1 addition & 0 deletions components/homekit/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace esphome

enum AInfo
{
NAME,
MODEL,
SN,
MANUFACTURER,
Expand Down
11 changes: 8 additions & 3 deletions components/homekit/light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace esphome
{
private:
static constexpr const char* TAG = "LightEntity";
std::map<AInfo, const char*> accessory_info = {{MODEL, "HAP-LIGHT"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}};
std::map<AInfo, const char*> 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;
Expand Down Expand Up @@ -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]);
Expand Down
9 changes: 7 additions & 2 deletions components/homekit/lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion components/homekit/lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace esphome
{
private:
static constexpr const char* TAG = "LockEntity";
std::map<AInfo, const char*> accessory_info = {{MODEL, "HAP-LOCK"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}};
std::map<AInfo, const char*> 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;
Expand Down
11 changes: 8 additions & 3 deletions components/homekit/sensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace esphome
{
private:
static constexpr const char* TAG = "SensorEntity";
std::map<AInfo, const char*> accessory_info = {{MODEL, "HAP-SENSOR"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}};
std::map<AInfo, const char*> 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);
Expand Down Expand Up @@ -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]);
Expand Down
11 changes: 8 additions & 3 deletions components/homekit/switch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace esphome
{
private:
static constexpr const char* TAG = "SwitchEntity";
std::map<AInfo, const char*> accessory_info = {{MODEL, "HAP-SWITCH"}, {SN, NULL}, {MANUFACTURER, "rednblkx"}, {FW_REV, "0.1"}};
std::map<AInfo, const char*> 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;
Expand Down Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ homekit:
light:
- id: test_light
meta:
name: "RGB Light"
manufacturer: "AMICI&CO"
model: "IGNIS"
serial_number: "42424242"
Expand Down

0 comments on commit e165cee

Please sign in to comment.