Skip to content

Commit

Permalink
Merge pull request #11 from maxcanna/cover
Browse files Browse the repository at this point in the history
Add support for cover
  • Loading branch information
smirko-dev authored Feb 16, 2022
2 parents 1530ba3 + 9c7d788 commit f311225
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Version 0.6

### Added
- Support for cover entities
- Italian language
- Bugfixes

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Supported entity types:
- group (on/off)
- automation (execute)
- script (execute)
- cover (open/close)

App icon is from https://icon-icons.com/de/symbol/home-assistant/138491 ([Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)).

Expand Down
12 changes: 12 additions & 0 deletions app/i18n/de-DE.po
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ msgstr "AUS"

msgid "exe"
msgstr "EXE"

msgid "opening"
msgstr "AUF"

msgid "open"
msgstr "AUF"

msgid "closing"
msgstr "ZU"

msgid "closed"
msgstr "ZU"
12 changes: 12 additions & 0 deletions app/i18n/en-US.po
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ msgstr "OFF"

msgid "exe"
msgstr "EXE"

msgid "open"
msgstr "OPE"

msgid "opening"
msgstr "OPE"

msgid "closing"
msgstr "CLO"

msgid "closed"
msgstr "CLO"
26 changes: 14 additions & 12 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { sendData } from "../common/utils";

import document from "document";

const Available = false;
let Available = false;
const EntityList = document.getElementById("entityList");
const AddressText = document.getElementById("addressText");
AddressText.text = gettext("unavailable");
Expand All @@ -21,7 +21,15 @@ let settings = loadSettings();
me.onunload = saveSettings;

// List of {id: "", name: "", state: ""}
const Entities = [];
let Entities = [];
const nextStates = {
on: "turn_off",
off: "turn_on",
open: "close_cover",
opening: "close_cover",
closing: "open_cover",
closed: "open_cover",
}

// Update list data
function setupList(list, data) {
Expand All @@ -35,17 +43,11 @@ function setupList(list, data) {
};
},
configureTile: function(tile, info) {
if (info.type == "item-pool") {
if (info.type === "item-pool") {
tile.getElementById("itemText").text = `${info.name}`;
tile.getElementById("itemState").text = `${gettext(info.state)}`;
let touch = tile.getElementById("itemTouch");
touch.onclick = evt => {
let state = "turn_on";
if (info.state === "on") {
state = "turn_off";
}
sendData({key: "change", entity: Entities[info.index].id, state: `${state}`});
};
touch.onclick = () => sendData({key: "change", entity: Entities[info.index].id, state: nextStates[info.state]});
}
}
};
Expand Down Expand Up @@ -109,7 +111,7 @@ messaging.peerSocket.onopen = () => {
sendData({key: "entities", value: settings.entities});
sendData({key: "force", value: settings.force});
};

// Message socket closes
messaging.peerSocket.onclose = () => {
console.log("Socket closed");
Expand All @@ -121,7 +123,7 @@ function loadSettings() {
return fs.readFileSync(settingsFile, settingsType);
}
catch (ex) {
console.log("Error loading settings");
console.error("Error loading settings");
// Default values
return {
url: "localhost",
Expand Down
2 changes: 1 addition & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as messaging from "messaging";
// Send data
export function sendData(data) {
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
console.log(`Sent: ${JSON.stringify(data)}`);
console.log('Sent', JSON.stringify(data));
messaging.peerSocket.send(data);
}
}
Expand Down
49 changes: 29 additions & 20 deletions companion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ let Port = "8123";
let Token = "";
let Force = false;

const groups = {
switch: "switch",
light: "light",
group: "homeassistant",
script: "script",
automation: "automation",
cover: "cover",
}

const nextStateOverrides = {
script: "turn_on",
automation: "trigger",
}

const forcedStates = {
turn_on: "on",
turn_off: "off",
close_cover: "closed",
open_cover: "open",
}

// Return address as URL + Port
function address() {
return URL + ':' + Port;
Expand Down Expand Up @@ -110,21 +131,9 @@ function changeEntity(url, token, entity, state) {
const json = JSON.stringify({
entity_id: `${entity}`
});
let group = "switch";
if (entity.startsWith("light")) {
group = "light";
}
else if (entity.startsWith("group")) {
group = "homeassistant";
}
else if (entity.startsWith("script")) {
group = "script";
state = "turn_on";
}
else if (entity.startsWith("automation")) {
group = "automation";
state = "trigger";
}
const domain = entity.split('.')[0]
const group = groups[domain]
state = nextStateOverrides[domain] || state
//DEBUG console.log(`SENT ${url}/api/services/${group}/${state} FOR ${entity}`);
fetch(`${url}/api/services/${group}/${state}`, {
method: "POST",
Expand All @@ -142,7 +151,7 @@ function changeEntity(url, token, entity, state) {
let msgData = {
key: "change",
id: entity,
state: state === 'turn_on' ? 'on' : 'off'
state: forcedStates[state] || state,
};
if (!entity.startsWith("script") && !entity.startsWith("automation")) {
//DEBUG console.log('FORCED ' + JSON.stringify(msgData));
Expand Down Expand Up @@ -173,17 +182,17 @@ function changeEntity(url, token, entity, state) {

// Message socket opens
messaging.peerSocket.onopen = () => {
console.log("Socket open");
console.log('Socket open');
};

// Message socket closes
messaging.peerSocket.onclose = () => {
console.log("Socket closed");
console.log('Socket closed');
};

// Received message
messaging.peerSocket.onmessage = evt => {
console.log(`Received: ${JSON.stringify(evt.data)}`);
console.log('Received', JSON.stringify(evt.data));
if (evt.data.key === "change") {
changeEntity(address(), Token, evt.data.entity, evt.data.state);
}
Expand Down

0 comments on commit f311225

Please sign in to comment.