-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from korapp/dev
Dev
- Loading branch information
Showing
10 changed files
with
243 additions
and
141 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
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 |
---|---|---|
@@ -1,75 +1,73 @@ | ||
const activeStates = ['on', 'open']; | ||
const activeStates = ['on', 'open', 'idle']; | ||
|
||
function getDisplayValue({ state, attribute, attributes, unit }) { | ||
if (attribute) { | ||
return attributes[attribute] || '' | ||
} | ||
if (state && state !== 'unknown') { | ||
return state + (unit === '%' ? unit : ' ' + unit) | ||
} | ||
if (attribute && attributes[attribute]) return attributes[attribute].toString() | ||
if (state && state !== 'unknown') return state + (unit === '%' ? unit : ' ' + unit) | ||
return '' | ||
} | ||
|
||
export function Entity({ entity_id = '', name, icon, attribute = '', unit, default_action = {} } = {}, data = {}) { | ||
const { s: state = '', a: attributes = {} } = data | ||
export function Entity({ entity_id = '', name, icon, state, attribute = '', attributes, unit, default_action = {}, scroll_action = {} } = {}, data = {}) { | ||
this.entity_id = entity_id | ||
this.name = name || attributes.friendly_name || '' | ||
this.icon = icon || attributes.icon || '' | ||
this.unit = unit || attributes.unit_of_measurement || '' | ||
this.attributes = Object.assign({}, attributes, data.a) | ||
this.state = data.s || state || '' | ||
this.name = name || this.attributes.friendly_name || '' | ||
this.icon = icon || this.attributes.icon || '' | ||
this.unit = unit || this.attributes.unit_of_measurement || '' | ||
this.attribute = attribute | ||
this.default_action = default_action | ||
this.active = activeStates.includes(state) | ||
this.scroll_action = scroll_action | ||
this.active = activeStates.includes(this.state) | ||
this.domain = entity_id.substring(0, entity_id.indexOf('.')) | ||
this.state = state | ||
this.value = getDisplayValue({ state, attribute, attributes, unit: this.unit }) | ||
this.value = getDisplayValue(this) | ||
} | ||
|
||
export function ConfigEntity({ entity_id = '', name, icon, attribute, default_action, notify } = {}) { | ||
export function ConfigEntity({ entity_id = '', name, icon, attribute, default_action, scroll_action, notify } = {}) { | ||
Object.defineProperties(this, { | ||
entity_id: { | ||
get: function() { return this._entity_id }, | ||
enumerable: true, | ||
get: function() { return this[Symbol.for('entity_id')] }, | ||
set: function(id) { | ||
this._entity_id = id | ||
this._domain = id.substring(0, id.indexOf('.')) | ||
if (!this._default_action) return | ||
if (this._domain === this._default_action.domain) { | ||
this._default_action.target.entity_id = id | ||
} else { | ||
delete this._default_action | ||
} | ||
this[Symbol.for('entity_id')] = id | ||
this[Symbol.for('domain')] = id.substring(0, id.indexOf('.')) | ||
updateAction(this, 'default_action') | ||
updateAction(this, 'scroll_action') | ||
} | ||
}, | ||
domain: { | ||
get: function() { return this._domain } | ||
}, | ||
default_action: { | ||
get: function() { return this._default_action }, | ||
set: function({ domain = this.domain, service, target = { entity_id: this.entity_id }} = {}) { | ||
if (!service) return delete this._default_action | ||
this._default_action = { | ||
domain, | ||
service, | ||
target | ||
} | ||
} | ||
}, | ||
enumerable: true, | ||
get: function() { return this[Symbol.for('domain')] } | ||
} | ||
}); | ||
|
||
addActionProperty(this, 'default_action') | ||
addActionProperty(this, 'scroll_action') | ||
|
||
this.entity_id = entity_id | ||
this.name = name | ||
this.icon = icon | ||
this.attribute = attribute | ||
this.default_action = default_action | ||
this.scroll_action = scroll_action | ||
this.notify = notify | ||
} | ||
|
||
ConfigEntity.prototype.toJSON = function() { | ||
return { | ||
attribute: this.attribute, | ||
entity_id: this.entity_id, | ||
icon: this.icon, | ||
name: this.name, | ||
default_action: this.default_action, | ||
notify: this.notify | ||
} | ||
function addActionProperty(o, name) { | ||
Object.defineProperty(o, name, { | ||
enumerable: true, | ||
get: function() { return o[Symbol.for(name)] }, | ||
set: function(action) { | ||
o[Symbol.for(name)] = !action?.service ? null : { | ||
service: action.service, | ||
domain: action.domain || o.domain, | ||
target: action.target || { entity_id: o.entity_id }, | ||
data_field: action.data_field | ||
} | ||
} | ||
}) | ||
} | ||
|
||
function updateAction(o, name) { | ||
if (!o[name]) return | ||
if (o.domain !== o[name].domain) return o[name] = null | ||
o[name].target.entity_id = o.entity_id | ||
} |
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
Oops, something went wrong.