Skip to content
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
2 changes: 1 addition & 1 deletion src/common/entity/can_toggle_domain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HomeAssistant } from "../../types";
import type { HomeAssistant } from "../../types";

export const canToggleDomain = (hass: HomeAssistant, domain: string) => {
const services = hass.services[domain];
Expand Down
22 changes: 19 additions & 3 deletions src/common/entity/can_toggle_state.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { HassEntity } from "home-assistant-js-websocket";
import { HomeAssistant } from "../../types";
import type { HassEntity } from "home-assistant-js-websocket";
import type { HomeAssistant } from "../../types";
import { canToggleDomain } from "./can_toggle_domain";
import { computeStateDomain } from "./compute_state_domain";
import { supportsFeature } from "./supports-feature";

export const canToggleState = (hass: HomeAssistant, stateObj: HassEntity) => {
const domain = computeStateDomain(stateObj);

if (domain === "group") {
return stateObj.state === "on" || stateObj.state === "off";
if (
stateObj.attributes?.entity_id?.some((entity) => {
const entityStateObj = hass.states[entity];
if (!entityStateObj) {
return false;
}

const entityDomain = computeStateDomain(entityStateObj);
return canToggleDomain(hass, entityDomain);
})
) {
return stateObj.state === "on" || stateObj.state === "off";
}

return false;
}

if (domain === "climate") {
return supportsFeature(stateObj, 4096);
}
Expand Down
8 changes: 8 additions & 0 deletions test/common/entity/can_toggle_state_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe("canToggleState", () => {
turn_off: null,
},
},
states: {
"light.bla": { entity_id: "light.bla" },
"light.test": { entity_id: "light.test" },
},
};

it("Detects lights toggle", () => {
Expand All @@ -24,7 +28,11 @@ describe("canToggleState", () => {
const stateObj: any = {
entity_id: "group.bla",
state: "on",
attributes: {
entity_id: ["light.bla", "light.test"],
},
};

assert.isTrue(canToggleState(hass, stateObj));
});

Expand Down