Skip to content

Commit

Permalink
Expose new event entity for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Oct 19, 2024
1 parent b2d3bbc commit a367754
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
29 changes: 26 additions & 3 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,26 @@ export default class HomeAssistant extends Extension {
});
}

/**
* If enum attribute does not have SET access and is named 'action', then expose
* as EVENT entity. Wildcard actions like `recall_*` are currently not supported.
*/
if (firstExpose.access & ACCESS_STATE && !(firstExpose.access & ACCESS_SET) && firstExpose.property == 'action') {
discoveryEntries.push({
type: 'event',
object_id: firstExpose.property,
mockProperties: [{property: firstExpose.property, value: null}],
discovery_payload: {
name: endpoint ? /* istanbul ignore next */ `${firstExpose.label} ${endpoint}` : firstExpose.label,
state_topic: true,
state_topic_postfix: 'action',
event_types: firstExpose.values.map((v) => v.toString()).filter((v) => !v.includes('*')),
value_template: `{ "event_type": "{{value}}" }`,
...ENUM_DISCOVERY_LOOKUP[firstExpose.name],
},
});
}

/**
* If enum attribute has SET access then expose as SELECT entity too.
* Note: currently both sensor and select are discovered, this is to avoid
Expand Down Expand Up @@ -1248,9 +1268,12 @@ export default class HomeAssistant extends Extension {
if (['binary_sensor', 'sensor'].includes(d.type) && d.discovery_payload.entity_category === 'config') {
d.discovery_payload.entity_category = 'diagnostic';
}
});

discoveryEntries.forEach((d) => {
// Event entities cannot have an entity_category set.
if (d.type === 'event' && d.discovery_payload.entity_category) {
delete d.discovery_payload.entity_category;
}

// Let Home Assistant generate entity name when device_class is present
if (d.discovery_payload.device_class) {
delete d.discovery_payload.name;
Expand Down Expand Up @@ -1532,7 +1555,7 @@ export default class HomeAssistant extends Extension {
}

if (!this.legacyTrigger) {
configs = configs.filter((c) => c.object_id !== 'action' && c.object_id !== 'click');
configs = configs.filter((c) => (c.object_id !== 'action' && c.object_id !== 'click') || c.type == 'event');
}

// deep clone of the config objects
Expand Down
9 changes: 6 additions & 3 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1673,9 +1673,10 @@ describe('HomeAssistant extension', () => {

it('Should discover trigger when click is published', async () => {
const discovered = MQTT.publish.mock.calls.filter((c) => c[0].includes('0x0017880104e45520')).map((c) => c[0]);
expect(discovered.length).toBe(7);
expect(discovered.length).toBe(8);
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/click/config');
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/action/config');
expect(discovered).toContain('homeassistant/event/0x0017880104e45520/action/config');

MQTT.publish.mockClear();

Expand Down Expand Up @@ -1856,8 +1857,9 @@ describe('HomeAssistant extension', () => {
await resetExtension();

const discovered = MQTT.publish.mock.calls.filter((c) => c[0].includes('0x0017880104e45520')).map((c) => c[0]);
expect(discovered.length).toBe(6);
expect(discovered.length).toBe(7);
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/action/config');
expect(discovered).toContain('homeassistant/event/0x0017880104e45520/action/config');
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/battery/config');
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/linkquality/config');
});
Expand All @@ -1867,9 +1869,10 @@ describe('HomeAssistant extension', () => {
await resetExtension();

const discovered = MQTT.publish.mock.calls.filter((c) => c[0].includes('0x0017880104e45520')).map((c) => c[0]);
expect(discovered.length).toBe(5);
expect(discovered.length).toBe(6);
expect(discovered).not.toContain('homeassistant/sensor/0x0017880104e45520/click/config');
expect(discovered).not.toContain('homeassistant/sensor/0x0017880104e45520/action/config');
expect(discovered).toContain('homeassistant/event/0x0017880104e45520/action/config');

MQTT.publish.mockClear();

Expand Down

0 comments on commit a367754

Please sign in to comment.