node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
85 lines (84 loc) • 3.78 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _Zone_instances, _Zone_getValidZones, _Zone_getZones;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const ExposeAsMixin_1 = __importDefault(require("../../common/controllers/ExposeAsMixin"));
const OutputController_1 = __importDefault(require("../../common/controllers/OutputController"));
const helpers_1 = require("./helpers");
var ZoneEvent;
(function (ZoneEvent) {
ZoneEvent["Enter"] = "enter";
ZoneEvent["EnterOrLeave"] = "enter_leave";
ZoneEvent["Leave"] = "leave";
})(ZoneEvent || (ZoneEvent = {}));
const ExposeAsController = (0, ExposeAsMixin_1.default)((OutputController_1.default));
class Zone extends ExposeAsController {
constructor() {
super(...arguments);
_Zone_instances.add(this);
}
onStateChanged(evt) {
if (this.isEnabled === false) {
return;
}
const { entity_id: entityId, event } = (0, lodash_1.cloneDeep)(evt);
if (!event.old_state || !event.new_state)
return;
const zones = __classPrivateFieldGet(this, _Zone_instances, "m", _Zone_getValidZones).call(this, event.old_state, event.new_state);
if (!zones.length)
return;
if (event.new_state) {
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.node.config.event} ${zones
.map((z) => z.entity_id)
.join(',')}`;
this.status.setSuccess(statusMessage);
this.node.send(msg);
}
}
_Zone_instances = new WeakSet(), _Zone_getValidZones = function _Zone_getValidZones(fromState, toState) {
const config = this.node.config;
const fromLocationData = (0, helpers_1.getLocationData)(fromState);
const toLocationData = (0, helpers_1.getLocationData)(toState);
if (!fromLocationData || !toLocationData)
return [];
const zones = __classPrivateFieldGet(this, _Zone_instances, "m", _Zone_getZones).call(this);
const validZones = zones.filter((zone) => {
const zoneData = (0, helpers_1.getZoneData)(zone);
if (!zoneData)
return false;
const fromMatch = (0, helpers_1.inZone)(fromLocationData, zoneData);
const toMatch = (0, helpers_1.inZone)(toLocationData, zoneData);
return ((config.event === ZoneEvent.Enter && !fromMatch && toMatch) ||
(config.event === ZoneEvent.Leave && fromMatch && !toMatch) ||
(config.event === ZoneEvent.EnterOrLeave &&
fromMatch !== toMatch));
});
return validZones;
}, _Zone_getZones = function _Zone_getZones() {
const entities = this.homeAssistant.websocket.getStates();
const zones = [];
for (const entityId in entities) {
if (this.node.config.zones.includes(entityId)) {
zones.push(entities[entityId]);
}
}
return zones;
};
exports.default = Zone;
;