Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event node to fire when device/person enter/leaves a selected zone
- Loading branch information
Showing
11 changed files
with
376 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,7 @@ module.exports = { | |
'trigger-state', | ||
'wait-until', | ||
'webhook', | ||
'zone', | ||
], | ||
}, | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Zone | ||
|
||
Outputs when one of the configured entities enter or leaves one of the defined zones. | ||
|
||
## Configuration | ||
|
||
### Entities | ||
|
||
- Type: `array` | ||
|
||
An array of entity ids to monitor for zone changes. | ||
|
||
### Event | ||
|
||
- Type: `string` | ||
|
||
Set when to check an entity. Either entering or leaving a zone. | ||
|
||
### Zones | ||
|
||
- Type: `array` | ||
|
||
An array of zone ids to check when a configured entity updates. | ||
|
||
## Outputs | ||
|
||
### topic | ||
|
||
- Type: `string` | ||
|
||
The entity id of the device/person that triggered the update. | ||
|
||
### payload | ||
|
||
- Type: `string` | ||
|
||
The state of the device/person entity that triggered the update. | ||
|
||
### data | ||
|
||
- Type: `object` | ||
|
||
The entity object of the device/person that triggered the update. | ||
|
||
### zones | ||
|
||
- Type: `array` | ||
|
||
An array of zone entities where the device/person entity entered/left after an update of location. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<div class="form-row"> | ||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> | ||
<input type="text" id="node-input-name" placeholder="Name" /> | ||
</div> | ||
<div class="form-row"> | ||
<label for="node-input-server"><i class="fa fa-server"></i> Server</label> | ||
<input type="text" id="node-input-server" /> | ||
</div> | ||
<div class="form-row"><ol id="entities"></ol></div> | ||
<div class="form-row"> | ||
<label for="node-input-event"><i class="fa fa-sign-in"></i> Event</label> | ||
<select id="node-input-event"> | ||
<option value="enter">Enter</option> | ||
<option value="leave">Leave</option> | ||
</select> | ||
</div> | ||
<div class="form-row"><ol id="zones"></ol></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
RED.nodes.registerType('ha-zone', { | ||
category: 'home_assistant', | ||
color: '#038FC7', | ||
outputs: 1, | ||
icon: 'font-awesome/fa-map-marker', | ||
paletteLabel: 'zone', | ||
label: function () { | ||
return this.name || 'zone'; | ||
}, | ||
labelStyle: nodeVersion.labelStyle, | ||
defaults: { | ||
server: { value: '', type: 'server', required: true }, | ||
name: { value: '' }, | ||
entities: { | ||
value: [], | ||
required: true, | ||
validate: function (v) { | ||
return v.length; | ||
}, | ||
}, | ||
event: { value: 'enter' }, | ||
zones: { | ||
value: [], | ||
required: true, | ||
validate: function (v) { | ||
return v.length; | ||
}, | ||
}, | ||
}, | ||
oneditprepare: function () { | ||
const $entities = $('#entities'); | ||
const $zones = $('#zones'); | ||
let zones = []; | ||
let devices = []; | ||
|
||
haServer.init(this, '#node-input-server'); | ||
haServer.autocomplete('entities', (entities) => { | ||
devices = entities.filter( | ||
(entities) => | ||
entities.startsWith('person.') || | ||
entities.startsWith('device_tracker.') | ||
); | ||
zones = entities.filter((entities) => entities.startsWith('zone.')); | ||
}); | ||
$entities.editableList({ | ||
addButton: true, | ||
removable: true, | ||
height: 'auto', | ||
header: $('<div>').append('Entities'), | ||
addItem: function (container, _, data) { | ||
const $row = $('<div />').appendTo(container); | ||
$('<span />', { | ||
text: 'Entity', | ||
style: 'padding-right: 35px;', | ||
}).appendTo($row); | ||
const value = $.isEmptyObject(data) ? '' : data; | ||
$('<input />', { type: 'text', class: 'input-entity' }) | ||
.appendTo($row) | ||
.val(value) | ||
.autocomplete({ source: devices, minLength: 0 }); | ||
}, | ||
}); | ||
$entities.editableList('addItems', this.entities); | ||
$zones.editableList({ | ||
addButton: true, | ||
removable: true, | ||
height: 'auto', | ||
header: $('<div>').append('zones'), | ||
addItem: function (container, _, data) { | ||
const $row = $('<div />').appendTo(container); | ||
$('<span />', { | ||
text: 'Zone', | ||
style: 'padding-right: 35px;', | ||
}).appendTo($row); | ||
const value = $.isEmptyObject(data) ? '' : data; | ||
$('<input />', { type: 'text', class: 'input-zone' }) | ||
.appendTo($row) | ||
.val(value) | ||
.autocomplete({ source: zones, minLength: 0 }); | ||
}, | ||
}); | ||
$zones.editableList('addItems', this.zones); | ||
}, | ||
oneditsave: function () { | ||
const entityList = $('#entities').editableList('items'); | ||
const zoneList = $('#zones').editableList('items'); | ||
const entities = new Set(); | ||
const zones = new Set(); | ||
entityList.each(function () { | ||
const val = $(this).find('.input-entity').val().replace(/\s/g, ''); | ||
if (val.length) { | ||
entities.add(val); | ||
} | ||
}); | ||
zoneList.each(function () { | ||
const val = $(this).find('.input-zone').val().replace(/\s/g, ''); | ||
if (val.length) { | ||
zones.add(val); | ||
} | ||
}); | ||
this.entities = Array.from(entities); | ||
this.zones = Array.from(zones); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const cloneDeep = require('lodash.clonedeep'); | ||
|
||
const EventsHaNode = require('../../lib/events-ha-node'); | ||
const { ZONE_ENTER, ZONE_LEAVE } = require('../../lib/const'); | ||
const { getLocationData, getZoneData, inZone } = require('../../lib/utils'); | ||
|
||
module.exports = function (RED) { | ||
const nodeOptions = { | ||
config: { | ||
entities: {}, | ||
event: {}, | ||
zones: {}, | ||
}, | ||
}; | ||
|
||
class Zone extends EventsHaNode { | ||
constructor(nodeDefinition) { | ||
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) | ||
); | ||
} | ||
} | ||
|
||
async onStateChanged(evt) { | ||
if (this.isEnabled === false || !this.isHomeAssistantRunning) { | ||
return; | ||
} | ||
const { entity_id: entityId, event } = cloneDeep(evt); | ||
|
||
const zones = await this.getValidZones( | ||
event.old_state, | ||
event.new_state | ||
); | ||
|
||
if (!zones.length) { | ||
this.setStatusFailed(entityId); | ||
return; | ||
} | ||
|
||
event.new_state.timeSinceChangedMs = | ||
Date.now() - new Date(event.new_state.last_changed).getTime(); | ||
|
||
const msg = { | ||
topic: entityId, | ||
payload: event.new_state.state, | ||
data: event, | ||
zones, | ||
}; | ||
const statusMessage = `${entityId} ${ | ||
this.nodeConfig.event | ||
} ${zones.map((z) => z.entity_id).join(',')}`; | ||
this.setStatusSuccess(statusMessage); | ||
this.send(msg); | ||
} | ||
|
||
async getValidZones(fromState, toState) { | ||
const config = this.nodeConfig; | ||
const fromLocationData = getLocationData(fromState); | ||
const toLocationData = getLocationData(toState); | ||
if (!fromLocationData || !toLocationData) return []; | ||
const zones = await this.getZones(); | ||
const validZones = zones.filter((zone) => { | ||
const zoneData = getZoneData(zone); | ||
if (!zoneData) return false; | ||
const fromMatch = inZone(fromLocationData, zoneData); | ||
const toMatch = inZone(toLocationData, zoneData); | ||
|
||
return ( | ||
(config.event === ZONE_ENTER && !fromMatch && toMatch) || | ||
(config.event === ZONE_LEAVE && fromMatch && !toMatch) | ||
); | ||
}); | ||
|
||
return validZones; | ||
} | ||
|
||
async getZones() { | ||
const node = this; | ||
const entities = await this.nodeConfig.server.homeAssistant.getStates(); | ||
const zones = []; | ||
Object.keys(entities).forEach((entityId) => { | ||
if (node.nodeConfig.zones.includes(entityId)) { | ||
zones.push(entities[entityId]); | ||
} | ||
}); | ||
|
||
return zones; | ||
} | ||
} | ||
RED.nodes.registerType('ha-zone', Zone); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.