Skip to content

Commit

Permalink
feat(zone): Add enter or leave event type
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Oct 4, 2020
1 parent deeaf85 commit 735437b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/const.js
Expand Up @@ -6,5 +6,6 @@ module.exports = Object.freeze({
INTEGRATION_LOADED: 'loaded',
INTEGRATION_UNLOADED: 'unloaded',
ZONE_ENTER: 'enter',
ZONE_ENTER_OR_LEAVE: 'enter_leave',
ZONE_LEAVE: 'leave',
});
1 change: 1 addition & 0 deletions nodes/zone/ui-zone.html
Expand Up @@ -12,6 +12,7 @@
<select id="node-input-event">
<option value="enter">Enter</option>
<option value="leave">Leave</option>
<option value="enter_leave">Enter or Leave</option>
</select>
</div>
<div class="form-row"><ol id="zones"></ol></div>
15 changes: 10 additions & 5 deletions nodes/zone/zone.js
@@ -1,8 +1,12 @@
const cloneDeep = require('lodash.clonedeep');

const EventsHaNode = require('../../lib/events-ha-node');
const {
ZONE_ENTER,
ZONE_LEAVE,
ZONE_ENTER_OR_LEAVE,
} = require('../../lib/const');
const { getLocationData, getZoneData, inZone } = require('../../lib/utils');
const { ZONE_ENTER, ZONE_LEAVE } = require('../../lib/const');

module.exports = function (RED) {
const nodeOptions = {
Expand All @@ -18,7 +22,6 @@ module.exports = function (RED) {
super(nodeDefinition, RED, nodeOptions);

for (const entity of this.nodeConfig.entities) {
console.log(entity);
this.addEventClientListener(
`ha_events:state_changed:${entity}`,
this.onStateChanged.bind(this)
Expand Down Expand Up @@ -72,7 +75,9 @@ module.exports = function (RED) {

return (
(config.event === ZONE_ENTER && !fromMatch && toMatch) ||
(config.event === ZONE_LEAVE && fromMatch && !toMatch)
(config.event === ZONE_LEAVE && fromMatch && !toMatch) ||
(config.event === ZONE_ENTER_OR_LEAVE &&
fromMatch !== toMatch)
);
});

Expand All @@ -83,11 +88,11 @@ module.exports = function (RED) {
const node = this;
const entities = await this.nodeConfig.server.homeAssistant.getStates();
const zones = [];
Object.keys(entities).forEach((entityId) => {
for (const entityId in entities) {
if (node.nodeConfig.zones.includes(entityId)) {
zones.push(entities[entityId]);
}
});
}

return zones;
}
Expand Down

0 comments on commit 735437b

Please sign in to comment.