Skip to content

Commit dfc285f

Browse files
committed
Preserve HA metadata and pass override options
1 parent 5f6341a commit dfc285f

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

lib/extension/homeassistant.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,9 @@ export class HomeAssistant extends Extension {
13581358
optimistic: !allowsState,
13591359
}),
13601360
};
1361+
if (feature.category === "config" || feature.category === "diagnostic") {
1362+
discoveryPayload.entity_category = feature.category;
1363+
}
13611364

13621365
if (isNumericExpose(feature)) {
13631366
if (allowsSet) discoveryPayload.command_template = compositePathCommandTemplate(path, "{{ value }}");
@@ -1468,7 +1471,7 @@ export class HomeAssistant extends Extension {
14681471
// This takes precedence over definitions in this file.
14691472
if (firstExpose.category === "config" || firstExpose.category === "diagnostic") {
14701473
for (const entry of discoveryEntries) {
1471-
entry.discovery_payload.entity_category = firstExpose.category;
1474+
entry.discovery_payload.entity_category ??= firstExpose.category;
14721475
}
14731476
}
14741477

@@ -1985,7 +1988,10 @@ export class HomeAssistant extends Extension {
19851988

19861989
if (entity.isDevice()) {
19871990
try {
1988-
entity.definition?.meta?.overrideHaDiscoveryPayload?.(payload);
1991+
const overrideHaDiscoveryPayload = entity.definition?.meta?.overrideHaDiscoveryPayload as
1992+
| ((payload: KeyValue, options: KeyValue) => void)
1993+
| undefined;
1994+
overrideHaDiscoveryPayload?.(payload, entity.options);
19891995
} catch (error) {
19901996
logger.error(`Failed to override HA discovery payload (${(error as Error).stack})`);
19911997
}

test/extensions/homeassistant.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,13 +1008,15 @@ describe("Extension: HomeAssistant", () => {
10081008
property: "network",
10091009
label: "Network",
10101010
access: 3,
1011+
category: "config",
10111012
features: [
10121013
{
10131014
type: "numeric",
10141015
name: "timeout",
10151016
property: "timeout",
10161017
label: "Timeout",
10171018
access: 3,
1019+
category: "config",
10181020
unit: "min",
10191021
value_min: 1,
10201022
value_max: 60,
@@ -1026,6 +1028,7 @@ describe("Extension: HomeAssistant", () => {
10261028
property: "temperature",
10271029
label: "Temperature",
10281030
access: 1,
1031+
category: "diagnostic",
10291032
unit: "°C",
10301033
},
10311034
{
@@ -1091,24 +1094,28 @@ describe("Extension: HomeAssistant", () => {
10911094
value_template:
10921095
'{% if value_json["network"] is defined and value_json["network"]["timeout"] is defined %}{{ value_json["network"]["timeout"] }}{% endif %}',
10931096
command_template: '{"network": {"timeout": {{ value }}}}',
1097+
entity_category: "config",
10941098
unit_of_measurement: "min",
10951099
min: 1,
10961100
max: 60,
10971101
step: 1,
10981102
});
10991103
expect(configs.find((config) => config.object_id === "network_temperature")?.discovery_payload).toMatchObject({
1104+
entity_category: "diagnostic",
11001105
unit_of_measurement: "°C",
11011106
device_class: "temperature",
11021107
state_class: "measurement",
11031108
});
11041109
expect(configs.find((config) => config.object_id === "network_mode")?.discovery_payload).toMatchObject({
11051110
command_template: '{"network": {"mode": {{ value | tojson }}}}',
1111+
entity_category: "config",
11061112
options: ["auto", "manual"],
11071113
});
11081114
expect(configs.find((config) => config.object_id === "network_advanced_enabled")?.discovery_payload).toMatchObject({
11091115
value_template:
11101116
'{% if value_json["network"] is defined and value_json["network"]["advanced"] is defined and value_json["network"]["advanced"]["enabled"] is defined %}{{ value_json["network"]["advanced"]["enabled"] | string | lower }}{% endif %}',
11111117
command_template: '{"network": {"advanced": {"enabled": {{ value }}}}}',
1118+
entity_category: "config",
11121119
payload_on: "true",
11131120
payload_off: "false",
11141121
});
@@ -1375,6 +1382,18 @@ describe("Extension: HomeAssistant", () => {
13751382
overrideSpy.mockRestore();
13761383
});
13771384

1385+
it("passes device options to discovery payload overrides", async () => {
1386+
const bosch = getZ2MEntity(devices.BTH_RM230Z) as Device;
1387+
assert(typeof bosch.definition?.meta?.overrideHaDiscoveryPayload === "function");
1388+
const overrideSpy = vi.spyOn(bosch.definition.meta, "overrideHaDiscoveryPayload") as MockInstance;
1389+
settings.set(["devices", "0x18fc2600000d7ae3", "expose_cooling"], true);
1390+
1391+
await resetExtension();
1392+
1393+
expect(overrideSpy).toHaveBeenCalledWith(expect.any(Object), expect.objectContaining({expose_cooling: true}));
1394+
overrideSpy.mockRestore();
1395+
});
1396+
13781397
it("Should discover Bosch BTH-RM230Z with a current_humidity attribute", () => {
13791398
const payload = {
13801399
action_template:

0 commit comments

Comments
 (0)