Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control HA light entity control example #101

Merged
merged 1 commit into from
May 29, 2024
Merged
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
79 changes: 79 additions & 0 deletions control-ha-light-entity-with-boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const CONFIG = {
ha_ip: "", // Home Assistant IP
ha_port: 8123, // Home Assistant Port
ha_token: "", // Home assistant Long-Lived Access Token
ha_entity_id: "light.shelly1pmminig3_84fce6xxxxxx_switch_0", // Home Assistant Light Entity ID
boolean_id: 200, // Boolean Component ID to toggle the light entity
};

const HEADERS = {
Authorization: "Bearer " + CONFIG.ha_token,
"Content-Type": "application/json",
};

function entityStateResponseHandler(response, err_no, err_msg) {
if (err_no !== 0) {
console.log(err_msg);
return;
}

const result = JSON.parse(response.body);
Shelly.call("boolean.set", {
id: CONFIG.boolean_id,
value: result.state === "on",
});
}

function checkEntityState(entity_id) {
Shelly.call(
"http.request",
{
method: "GET",
headers: HEADERS,
url:
"http://" +
CONFIG.ha_ip +
":" +
JSON.stringify(CONFIG.ha_port) +
"/api/states/" +
entity_id,
},
entityStateResponseHandler
);
}

/**
* Sets the light entity state based on the method
* @param {*} entity_id entity id
* @param {*} method turn_on | turn_off | toggle
*/
function setLightEntityState(entity_id, method) {
Shelly.call("http.request", {
method: "POST",
headers: HEADERS,
url:
"http://" +
CONFIG.ha_ip +
":" +
JSON.stringify(CONFIG.ha_port) +
"/api/services/light/" +
method,
body: JSON.stringify({
entity_id: entity_id,
}),
});

checkEntityState(entity_id);
}

Shelly.addStatusHandler(function (status_data) {
if (status_data.name !== "boolean" || status_data.id !== CONFIG.boolean_id) {
return;
}

const invoke_state = status_data.delta.value ? "turn_on" : "turn_off";
setLightEntityState(CONFIG.ha_entity_id, invoke_state);
});

// Initial sync with the entity state
checkEntityState(CONFIG.ha_entity_id);
5 changes: 5 additions & 0 deletions examples-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"fname": "control-ha-light-entity-with-boolean.js",
"title": "Control Light entity from HA via virtual boolean component",
"description": "This script will control a light entity in Home Assistant via a virtual boolean component in Shelly. The script will listen for changes in the boolean component and will turn on or off the light entity in Home Assistant accordingly."
},
{
"fname": "load-shedding.js",
"title": "Load shedding with Shelly Pro4PM and Pro3EM",
Expand Down
Loading