Skip to content

Commit

Permalink
fix(trigger-state): Handle missing entity errors
Browse files Browse the repository at this point in the history
Fixes #1096
  • Loading branch information
zachowj committed Oct 3, 2023
1 parent 26e6e6f commit 4805cc3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
32 changes: 1 addition & 31 deletions src/nodes/trigger-state/TriggerStateController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HassEntities } from 'home-assistant-js-websocket';
import { cloneDeep } from 'lodash';
import selectn from 'selectn';

Expand Down Expand Up @@ -164,7 +163,7 @@ export default class TriggerStateController extends ExposeAsController {

if (entity === null) {
throw new ConfigError([
'trigger-state.error.entity_not_found',
'trigger-state.error.entity_id_not_found',
{ entity_id: entityId },
]);
}
Expand Down Expand Up @@ -339,35 +338,6 @@ export default class TriggerStateController extends ExposeAsController {
return true;
}

public onDeploy() {
if (this.isEnabled === false) {
return;
}

const entities = this.homeAssistant.websocket.getStates();
this.onStatesLoaded(entities);
}

public onStatesLoaded(entities: HassEntities) {
if (this.isEnabled === false) {
return;
}

for (const entityId in entities) {
const eventMessage = {
event_type: HaEvent.StateChanged,
entity_id: entityId,
event: {
entity_id: entityId,
old_state: entities[entityId],
new_state: entities[entityId],
},
};

this.onEntityStateChanged(eventMessage as HassStateChangedEvent);
}
}

public async onEntityStateChanged(evt: HassStateChangedEvent) {
if (
this.isEnabled === false ||
Expand Down
26 changes: 26 additions & 0 deletions src/nodes/trigger-state/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { HassEntities } from 'home-assistant-js-websocket';

import { HaEvent } from '../../homeAssistant';
import HomeAssistant from '../../homeAssistant/HomeAssistant';

export function createStateChangeEvents(
homeAssistant: HomeAssistant,
entities?: HassEntities
) {
entities ??= homeAssistant.websocket.getStates();

const events = [];
for (const entityId in entities) {
events.push({
event_type: HaEvent.StateChanged,
entity_id: entityId,
event: {
entity_id: entityId,
old_state: entities[entityId],
new_state: entities[entityId],
},
});
}

return events;
}
16 changes: 13 additions & 3 deletions src/nodes/trigger-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getExposeAsConfigNode, getServerConfigNode } from '../../helpers/node';
import { getHomeAssistant } from '../../homeAssistant';
import { BaseNode, BaseNodeProperties } from '../../types/nodes';
import { Constraint, CustomOutput, DISABLE, ENABLE } from './const';
import { createStateChangeEvents } from './helpers';
import TriggerStateController from './TriggerStateController';
import TriggerStateStatus from './TriggerStateStatus';

Expand Down Expand Up @@ -155,14 +156,23 @@ export default function triggerState(
controller.onEntityStateChanged.bind(controller)
);

if (this.config.outputInitially) {
if (controller.isEnabled && this.config.outputInitially) {
const emitEvents = () => {
const events = createStateChangeEvents(homeAssistant);
events.forEach((event) => {
clientEvents.emit(
`ha_events:state_changed:${event.entity_id}`,
event
);
});
};
// Here for when the node is deploy without the server config being deployed
if (homeAssistant.isHomeAssistantRunning) {
controller.onDeploy();
emitEvents();
} else {
clientEvents.addListener(
'ha_client:initial_connection_ready',
controller.onStatesLoaded.bind(controller)
emitEvents
);
}
}
Expand Down

0 comments on commit 4805cc3

Please sign in to comment.