From 94642f0257caead86c8a60823e6c53308c9f406b Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Thu, 27 Jul 2023 14:11:20 +0200 Subject: [PATCH 1/3] Added entities in the default view --- widget/source/BaseView.mc | 31 +++++++++++++++- widget/source/Entities/EntityListView.mc | 6 +++- widget/source/hass/Entity.mc | 45 ++++++++++++++++++++---- widget/source/hass/Hass.mc | 8 ++++- 4 files changed, 81 insertions(+), 9 deletions(-) diff --git a/widget/source/BaseView.mc b/widget/source/BaseView.mc index f1cbb16..2895069 100644 --- a/widget/source/BaseView.mc +++ b/widget/source/BaseView.mc @@ -10,6 +10,9 @@ class BaseView extends Ui.View { } function onShow() { + if (Hass.client.isLoggedIn()) { + Hass.refreshAllEntities(true); + } } // Load your resources here @@ -26,5 +29,31 @@ class BaseView extends Ui.View { function onUpdate(dc) { View.onUpdate(dc); + + var entities = Hass.getEntities(); + + var texts = new [entities.size()]; + for(var i = 0; i < entities.size(); i++) { + var color = Graphics.COLOR_DK_GRAY; + if (!entities[i].getRefreshing()) { + color = Hass.Entity.stateToActive(entities[i].getState()) ? Graphics.COLOR_WHITE : Graphics.COLOR_LT_GRAY; + } + + var text = entities[i].getName(); + var sensorValue = entities[i].getSensorValue(); + if (sensorValue) { + text += ": " + entities[i].getSensorValue(); + } + + texts[i] = new Ui.Text({ + :text => text, + :color => color, + :font => Graphics.FONT_SMALL, + :locX => WatchUi.LAYOUT_HALIGN_CENTER, + :locY => (i + 1) * Graphics.getFontHeight(Graphics.FONT_SMALL) + }); + } + + setLayout(texts); } -} \ No newline at end of file +} diff --git a/widget/source/Entities/EntityListView.mc b/widget/source/Entities/EntityListView.mc index 80755bd..d014dac 100644 --- a/widget/source/Entities/EntityListView.mc +++ b/widget/source/Entities/EntityListView.mc @@ -178,6 +178,10 @@ class EntityListView extends Ui.View { var fontWidth = vw * 0.80; var text = entity.getName(); + var sensorValue = entity.getSensorValue(); + if (sensorValue) { + text += "\n" + entity.getSensorValue(); + } var fonts = [Graphics.FONT_MEDIUM, Graphics.FONT_TINY, Graphics.FONT_XTINY]; var font = fonts[0]; @@ -372,4 +376,4 @@ class EntityListView extends Ui.View { var WHITE = Graphics.COLOR_WHITE; var BLACK = Graphics.COLOR_BLACK; } -} \ No newline at end of file +} diff --git a/widget/source/hass/Entity.mc b/widget/source/hass/Entity.mc index fd0c5f3..7eb7e83 100644 --- a/widget/source/hass/Entity.mc +++ b/widget/source/hass/Entity.mc @@ -64,6 +64,30 @@ module Hass { return HASS_STATE_UNKNOWN; } + // Converts any state to a boolean value. For example, if a light is on, it is considered true, while off is false. + static function stateToActive(state) { + if (state == STATE_ON) { + return true; + } + if (state == STATE_OFF) { + return false; + } + if (state == STATE_OPEN) { + return true; + } + if (state == STATE_CLOSED) { + return false; + } + if (state == STATE_LOCKED) { + return true; + } + if (state == STATE_UNLOCKED) { + return false; + } + + return true; + } + hidden var _mId; // Home assistant id hidden var _mType; // Type of entity hidden var _mName; // Name @@ -71,6 +95,7 @@ module Hass { hidden var _mExt; // Is this entity loaded from settings? hidden var _mSensorValue; // Custom state info text hidden var _mSensorClass; // Device class for sensor + hidden var _mRefreshing; // If device state is currently refreshing function initialize(entity) { _mId = entity[:id]; @@ -78,6 +103,7 @@ module Hass { _mState = Entity.stringToState(entity[:state]); _mExt = entity[:ext] == true; _mSensorClass = entity[:sensorClass]; + _mRefreshing = false; if (_mId.find("scene.") != null) { _mType = TYPE_SCENE; @@ -114,19 +140,26 @@ module Hass { return _mId; } + function getRefreshing() { + return _mRefreshing; + } + + function setRefreshing(newRefreshingState) { + _mRefreshing = newRefreshingState; + } + function getName() { - if (_mState == STATE_SENSOR) { - return _mName + "\n" + _mSensorValue; - } - else { - return _mName; - } + return _mName; } function setName(newName) { _mName = newName; } + function getSensorValue() { + return _mSensorValue; + } + function getType() { return _mType; } diff --git a/widget/source/hass/Hass.mc b/widget/source/hass/Hass.mc index e83003a..611cfb2 100644 --- a/widget/source/hass/Hass.mc +++ b/widget/source/hass/Hass.mc @@ -173,6 +173,8 @@ module Hass { var entity = getEntity(data[:body]["entity_id"]); + entity.setRefreshing(false); + if (name != null) { entity.setName(name); } @@ -187,6 +189,9 @@ module Hass { entity.setSensorClass(sensorClass); } + System.println("Updating after" + name); + Ui.requestUpdate(); + if (data[:context][:callback] != null) { data[:context][:callback].invoke(null, entity); } @@ -243,6 +248,7 @@ module Hass { for (var i = 0; i < _entities.size(); i++) { _entitiesToRefresh.add(_entities[i]); + _entities[i].setRefreshing(true); } _refreshPendingEntities(null, null); @@ -447,4 +453,4 @@ class HassController { // _refreshPendingEntities(); // } -} \ No newline at end of file +} From e362c418b98dfd7e9f1b4507667df57c5b4682f3 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Fri, 28 Jul 2023 21:58:25 +0200 Subject: [PATCH 2/3] Always center entities in the base view --- widget/source/BaseView.mc | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/widget/source/BaseView.mc b/widget/source/BaseView.mc index 2895069..1ae7b70 100644 --- a/widget/source/BaseView.mc +++ b/widget/source/BaseView.mc @@ -32,8 +32,20 @@ class BaseView extends Ui.View { var entities = Hass.getEntities(); - var texts = new [entities.size()]; - for(var i = 0; i < entities.size(); i++) { + var font = Graphics.FONT_TINY; + var lineHeight = Graphics.getFontHeight(font); + var verticalMargin = lineHeight; + var drawableHeight = dc.getHeight() - 2 * verticalMargin; + var maxDrawableEntities = Math.floor(drawableHeight / lineHeight); + var entitiesToDraw = entities.size(); + if (maxDrawableEntities < entitiesToDraw) { + entitiesToDraw = maxDrawableEntities; + } + var firstLineY = (dc.getHeight() - entitiesToDraw * lineHeight) / 2; + + var texts = new [entitiesToDraw]; + + for(var i = 0; i < entitiesToDraw; i++) { var color = Graphics.COLOR_DK_GRAY; if (!entities[i].getRefreshing()) { color = Hass.Entity.stateToActive(entities[i].getState()) ? Graphics.COLOR_WHITE : Graphics.COLOR_LT_GRAY; @@ -48,9 +60,9 @@ class BaseView extends Ui.View { texts[i] = new Ui.Text({ :text => text, :color => color, - :font => Graphics.FONT_SMALL, + :font => font, :locX => WatchUi.LAYOUT_HALIGN_CENTER, - :locY => (i + 1) * Graphics.getFontHeight(Graphics.FONT_SMALL) + :locY => firstLineY + i * lineHeight }); } From 5855badec88dd3613ef8944c53ee4d9a221da550 Mon Sep 17 00:00:00 2001 From: Johan Lindell Date: Sat, 29 Jul 2023 12:22:49 +0200 Subject: [PATCH 3/3] Fixed locked state to active entity conversion --- widget/source/hass/Entity.mc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/widget/source/hass/Entity.mc b/widget/source/hass/Entity.mc index 7eb7e83..ed9d21b 100644 --- a/widget/source/hass/Entity.mc +++ b/widget/source/hass/Entity.mc @@ -78,10 +78,10 @@ module Hass { if (state == STATE_CLOSED) { return false; } - if (state == STATE_LOCKED) { + if (state == STATE_UNLOCKED) { return true; } - if (state == STATE_UNLOCKED) { + if (state == STATE_LOCKED) { return false; }