mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2024-11-17 02:48:31 -07:00
d83085ea7f
* Update zigbee-herdsman and zigbee-shepherd-converters. * Force Aqara S2 Lock endvices (#1764) * Start on zigbee-herdsman controller refactor. * More updates. * Cleanup zapp. * updates. * Propagate adapter disconnected event. * Updates. * Initial refactor to zigbee-herdsman. * Refactor deviceReceive to zigbee-herdsman. * Rename * Refactor deviceConfigure. * Finish bridge config. * Refactor availability. * Active homeassistant extension and more refactors. * Refactor groups. * Enable soft reset. * Activate group membership * Start on tests. * Enable reporting. * Add more controller tests. * Add more tests * Fix linting error. * Data en deviceReceive tests. * Move to zigbee-herdsman-converters. * More device publish tests. * Cleanup dependencies. * Bring device publish coverage to 100. * Bring home assistant test coverage to 100. * Device configure tests. * Attempt to fix tests. * Another attempt. * Another one. * Another one. * Another. * Add wait. * Longer wait. * Debug. * Update dependencies. * Another. * Begin on availability tests. * Improve availability tests. * Complete deviceAvailability tests. * Device bind tests. * More tests. * Begin networkmap refactors. * start on networkmap tests. * Network map tests. * Add utils tests. * Logger tests. * Settings and logger tests. * Ignore some stuff for coverage and add todos. * Add remaining missing tests. * Enforce 100% test coverage. * Start on groups test and refactor entityPublish to resolveEntity * Remove joinPathStorage, not used anymore as group information is stored into zigbee-herdsman database. * Fix linting issues. * Improve tests. * Add groups. * fix group membership. * Group: log names. * Convert MQTT message to string by default. * Fix group name. * Updates. * Revert configuration.yaml. * Add new line. * Fixes. * Updates. * Fix tests. * Ignore soft reset extension.
112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
const logger = require('./util/logger');
|
|
const data = require('./util/data');
|
|
const fs = require('fs');
|
|
const settings = require('./util/settings');
|
|
const objectAssignDeep = require('object-assign-deep');
|
|
|
|
const saveInterval = 1000 * 60 * 5; // 5 minutes
|
|
|
|
const dontCacheProperties = [
|
|
'action', 'button', 'button_left', 'button_right', 'click', 'forgotten', 'keyerror',
|
|
'step_size', 'transition_time', 'action_color_temperature', 'action_color',
|
|
'action_group', 'group_list', 'group_capacity', 'no_occupancy_since',
|
|
'step_mode', 'transition_time', 'duration', 'elapsed',
|
|
];
|
|
|
|
class State {
|
|
constructor() {
|
|
this.state = {};
|
|
this.file = data.joinPath('state.json');
|
|
this.timer = null;
|
|
|
|
this.handleSettingsChanged = this.handleSettingsChanged.bind(this);
|
|
}
|
|
|
|
start() {
|
|
this._load();
|
|
|
|
// Save the state on every interval
|
|
this.clearTimer();
|
|
this.timer = setInterval(() => this.save(), saveInterval);
|
|
|
|
// Listen for on settings changed events.
|
|
settings.addOnChangeHandler(this.handleSettingsChanged);
|
|
|
|
this.checkLastSeen();
|
|
}
|
|
|
|
handleSettingsChanged() {
|
|
this.checkLastSeen();
|
|
}
|
|
|
|
checkLastSeen() {
|
|
if (settings.get().advanced.last_seen === 'disable') {
|
|
Object.values(this.state).forEach((s) => {
|
|
if (s.hasOwnProperty('last_seen')) {
|
|
delete s.last_seen;
|
|
}
|
|
});
|
|
|
|
this.save();
|
|
}
|
|
}
|
|
|
|
clearTimer() {
|
|
if (this.timer) {
|
|
clearTimeout(this.timer);
|
|
this.timer = null;
|
|
}
|
|
}
|
|
|
|
stop() {
|
|
this.clearTimer();
|
|
this.save();
|
|
}
|
|
|
|
_load() {
|
|
if (fs.existsSync(this.file)) {
|
|
try {
|
|
this.state = JSON.parse(fs.readFileSync(this.file, 'utf8'));
|
|
logger.debug(`Loaded state from file ${this.file}`);
|
|
} catch (e) {
|
|
logger.debug(`Failed to load state from file ${this.file} (corrupt file?)`);
|
|
}
|
|
} else {
|
|
logger.debug(`Can't load state from file ${this.file} (doesn't exist)`);
|
|
}
|
|
}
|
|
|
|
save() {
|
|
logger.debug(`Saving state to file ${this.file}`);
|
|
const json = JSON.stringify(this.state, null, 4);
|
|
fs.writeFileSync(this.file, json, 'utf8');
|
|
}
|
|
|
|
exists(ieeeAddr) {
|
|
return this.state.hasOwnProperty(ieeeAddr);
|
|
}
|
|
|
|
get(ieeeAddr) {
|
|
return this.state[ieeeAddr];
|
|
}
|
|
|
|
set(ieeeAddr, state) {
|
|
const toState = objectAssignDeep.noMutate(state);
|
|
dontCacheProperties.forEach((property) => {
|
|
if (toState.hasOwnProperty(property)) {
|
|
delete toState[property];
|
|
}
|
|
});
|
|
|
|
this.state[ieeeAddr] = toState;
|
|
}
|
|
|
|
remove(ieeeAddr) {
|
|
if (this.exists(ieeeAddr)) {
|
|
delete this.state[ieeeAddr];
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = State;
|