From 78442d82d184636fd12c4d26f4a9fc61d3655afc Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 31 Dec 2021 11:43:59 +0100 Subject: [PATCH] Add color mode support to switch light (#63124) --- homeassistant/components/switch/light.py | 8 +++++++- tests/components/switch/test_light.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/switch/light.py b/homeassistant/components/switch/light.py index 12fb847f86b..9ca5df135bd 100644 --- a/homeassistant/components/switch/light.py +++ b/homeassistant/components/switch/light.py @@ -6,7 +6,11 @@ from typing import Any, cast import voluptuous as vol from homeassistant.components import switch -from homeassistant.components.light import PLATFORM_SCHEMA, LightEntity +from homeassistant.components.light import ( + COLOR_MODE_ONOFF, + PLATFORM_SCHEMA, + LightEntity, +) from homeassistant.const import ( ATTR_ENTITY_ID, CONF_ENTITY_ID, @@ -64,6 +68,8 @@ class LightSwitch(LightEntity): self._switch_entity_id = switch_entity_id self._unique_id = unique_id self._switch_state: State | None = None + self._attr_color_mode = COLOR_MODE_ONOFF + self._attr_supported_color_modes = {COLOR_MODE_ONOFF} @property def name(self) -> str: diff --git a/tests/components/switch/test_light.py b/tests/components/switch/test_light.py index e260ce83dbf..62fef242e9f 100644 --- a/tests/components/switch/test_light.py +++ b/tests/components/switch/test_light.py @@ -1,5 +1,10 @@ """The tests for the Light Switch platform.""" +from homeassistant.components.light import ( + ATTR_COLOR_MODE, + ATTR_SUPPORTED_COLOR_MODES, + COLOR_MODE_ONOFF, +) from homeassistant.setup import async_setup_component from tests.components.light import common @@ -31,6 +36,8 @@ async def test_default_state(hass): assert state.attributes.get("white_value") is None assert state.attributes.get("effect_list") is None assert state.attributes.get("effect") is None + assert state.attributes.get(ATTR_SUPPORTED_COLOR_MODES) == [COLOR_MODE_ONOFF] + assert state.attributes.get(ATTR_COLOR_MODE) is None async def test_light_service_calls(hass): @@ -54,6 +61,10 @@ async def test_light_service_calls(hass): assert hass.states.get("switch.decorative_lights").state == "on" assert hass.states.get("light.light_switch").state == "on" + assert ( + hass.states.get("light.light_switch").attributes.get(ATTR_COLOR_MODE) + == COLOR_MODE_ONOFF + ) await common.async_turn_off(hass, "light.light_switch") await hass.async_block_till_done()