Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion widget/source/BaseView.mc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class BaseView extends Ui.View {
}

function onShow() {
if (Hass.client.isLoggedIn()) {
Hass.refreshAllEntities(true);
}
}

// Load your resources here
Expand All @@ -26,5 +29,43 @@ class BaseView extends Ui.View {

function onUpdate(dc) {
View.onUpdate(dc);

var entities = Hass.getEntities();

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;
}

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 => font,
:locX => WatchUi.LAYOUT_HALIGN_CENTER,
:locY => firstLineY + i * lineHeight
});
}

setLayout(texts);
}
}
}
6 changes: 5 additions & 1 deletion widget/source/Entities/EntityListView.mc
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -372,4 +376,4 @@ class EntityListView extends Ui.View {
var WHITE = Graphics.COLOR_WHITE;
var BLACK = Graphics.COLOR_BLACK;
}
}
}
45 changes: 39 additions & 6 deletions widget/source/hass/Entity.mc
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,46 @@ 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_UNLOCKED) {
return true;
}
if (state == STATE_LOCKED) {
return false;
}

return true;
}

hidden var _mId; // Home assistant id
hidden var _mType; // Type of entity
hidden var _mName; // Name
hidden var _mState; // Current State
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];
_mName = entity[:name];
_mState = Entity.stringToState(entity[:state]);
_mExt = entity[:ext] == true;
_mSensorClass = entity[:sensorClass];
_mRefreshing = false;

if (_mId.find("scene.") != null) {
_mType = TYPE_SCENE;
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion widget/source/hass/Hass.mc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ module Hass {

var entity = getEntity(data[:body]["entity_id"]);

entity.setRefreshing(false);

if (name != null) {
entity.setName(name);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -447,4 +453,4 @@ class HassController {

// _refreshPendingEntities();
// }
}
}