Skip to content

Commit

Permalink
1.4.0 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael5r authored May 20, 2020
1 parent 2101feb commit 311b8c5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Option | Type | Default | Description
`displayFilter` | `array` | `['all']` | [Array of strings with names of lights/groups that you wish to **show**](#how-do-i-filter-which-lights-or-groups-to-show)
`hideFilter` | `array` | `[]` | [Array of strings with names of lights/groups that you wish to **hide**](#how-do-i-filter-which-lights-or-groups-to-show)
`hideOff` | `boolean` | `false` | Whether to hide lights that are off
`orderByName` | `boolean` | `false` | Whether to display lights or groups alphabetically
`updateInterval` | `int` | `120000` | How often to load new data, default is 2 minutes
`initialLoadDelay` | `int` | `0` | How long to delay the initial load (in ms)
`motionSleep` | `boolean` | `false` | Suspend module when triggered by [MMM-PIR-Sensor](https://github.com/paviro/MMM-PIR-Sensor)
Expand Down
55 changes: 48 additions & 7 deletions mmm-hue-lights.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ Module.register('mmm-hue-lights', {
minimalList: false,
minimalGrid: false,
minimalGridUltra: false,
orderByName: false,
motionSleep: false,
motionSleepSeconds: 300, // this is in seconds (not ms)
updateInterval: 2 * 60 * 1000,
animationSpeed: 2 * 1000,
initialLoadDelay: 0,
version: '1.3.2'
version: '1.4.0'
},

getScripts: function() {
Expand Down Expand Up @@ -127,6 +128,7 @@ Module.register('mmm-hue-lights', {
var hideOff = this.config.hideOff;
var minimalList = this.config.minimalList;
var alignment = this.config.alignment;
var orderByName = this.config.orderByName;

var lights = this.lights;
var groups = this.groups;
Expand All @@ -144,10 +146,15 @@ Module.register('mmm-hue-lights', {
};

var data = isLights ? lights : groups;
var dataArr = Object.values(data); // convert to array

Object.keys(data).forEach(function(key) {
// sort by name if orderByName is true
if (orderByName) {
dataArr.sort((a, b) => (a.name > b.name) ? 1 : -1);
}

dataArr.forEach(function(item) {

var item = data[key];
var itemColorData = isLights ? item.state : item.action;

var isOn = false;
Expand Down Expand Up @@ -294,6 +301,7 @@ Module.register('mmm-hue-lights', {
var minimalGrid = this.config.minimalGrid;
var minimalGridUltra = isLights ? this.config.minimalGridUltra : false;
var hideOff = this.config.hideOff;
var orderByName = this.config.orderByName;

var lights = this.lights;
var groups = this.groups;
Expand All @@ -312,10 +320,15 @@ Module.register('mmm-hue-lights', {
// create rows

var data = isLights ? lights : groups;
var dataArr = Object.values(data); // convert to array

// sort by name if orderByName is true
if (orderByName) {
dataArr.sort((a, b) => (a.name > b.name) ? 1 : -1);
}

Object.keys(data).forEach(function(key) {
dataArr.forEach(function(item) {

var item = data[key];
var itemColorData = isLights ? item.state : item.action;
var type = item.type.toLowerCase();

Expand Down Expand Up @@ -611,12 +624,11 @@ Module.register('mmm-hue-lights', {

if (notification === 'MMM_HUE_LIGHTS_DATA') {
self.processHueData(payload);
self.scheduleUpdate(self.config.updateInterval);
} else if (notification === 'MMM_HUE_LIGHTS_DATA_ERROR') {
self.errMsg = payload;
self.updateDom(self.config.animationSpeed);
}

self.scheduleUpdate(self.config.updateInterval);
},

suspend: function() {
Expand Down Expand Up @@ -658,6 +670,7 @@ Module.register('mmm-hue-lights', {
var hideFilter = this.config.hideFilter;

var renderUi = true;
var forceRender = false;

var oldLights = this.lights;
var oldGroups = this.groups;
Expand Down Expand Up @@ -764,13 +777,41 @@ Module.register('mmm-hue-lights', {
var numberOfLights = (lights) ? Object.keys(data.lights).length : 0;
var numberOfGroups = (groups) ? Object.keys(data.groups).length : 0;

// check if lights of each group is reachable
// no reachable lights will be "marked" as off
Object.values(data.groups).forEach(function (group, index) {

var numberOfLightsInGroup = group.lights.length;
var any_on = false;

for (var i = 0; i < numberOfLightsInGroup; i++) {
var hueLightID = group.lights[i];

if (data.lights[hueLightID].state.reachable == false) {
// if light is not reachable
data.lights[hueLightID].state.on = false;
group.state.all_on = false;
forceRender = true;
} else {
if (data.lights[hueLightID].state.on == true) {
any_on = true;
}
}
}

group.state.any_on = any_on;

});

// check old data to make sure we're not re-rendering the UI for no reason
if (this.loaded) {

if (numberOfOldLights !== numberOfLights) {
// number of lights changed (update dom)
} else if (numberOfOldGroups !== numberOfGroups) {
// number of groups changed (update dom)
} else if (forceRender) {
// force render (update dom)
} else {
// compare status of lights
if ((this.jsonEqual(oldLights,lights)) && (this.jsonEqual(oldGroups,groups))) {
Expand Down

0 comments on commit 311b8c5

Please sign in to comment.