diff --git a/src/common/entity/can_toggle_domain.ts b/src/common/entity/can_toggle_domain.ts index d1c4a08b7c..df87bfcf7a 100644 --- a/src/common/entity/can_toggle_domain.ts +++ b/src/common/entity/can_toggle_domain.ts @@ -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]; diff --git a/src/common/entity/can_toggle_state.ts b/src/common/entity/can_toggle_state.ts index f0480b60ff..afc8dd9f9c 100644 --- a/src/common/entity/can_toggle_state.ts +++ b/src/common/entity/can_toggle_state.ts @@ -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); } diff --git a/test/common/entity/can_toggle_state_test.ts b/test/common/entity/can_toggle_state_test.ts index b35bb04c63..93bd3849a6 100644 --- a/test/common/entity/can_toggle_state_test.ts +++ b/test/common/entity/can_toggle_state_test.ts @@ -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", () => { @@ -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)); });