From 3d12c7409d3a5d934e4e3ee204b96dc78ff1a70b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 22 Jun 2023 21:22:29 +0200 Subject: [PATCH] Add basic light tests to esphome (#95029) --- homeassistant/components/esphome/light.py | 7 +- tests/components/esphome/test_light.py | 139 ++++++++++++++++++++++ 2 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 tests/components/esphome/test_light.py diff --git a/homeassistant/components/esphome/light.py b/homeassistant/components/esphome/light.py index 9ba7e474e8c..b44bac0b933 100644 --- a/homeassistant/components/esphome/light.py +++ b/homeassistant/components/esphome/light.py @@ -135,11 +135,7 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity): """A light implementation for ESPHome.""" _native_supported_color_modes: list[int] - - @property - def _supports_color_mode(self) -> bool: - """Return whether the client supports the new color mode system natively.""" - return self._api_version >= APIVersion(1, 6) + _supports_color_mode = False @property @esphome_state_property @@ -364,6 +360,7 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity): """Set attrs from static info.""" super()._on_static_info_update(static_info) static_info = self._static_info + self._supports_color_mode = self._api_version >= APIVersion(1, 6) self._native_supported_color_modes = static_info.supported_color_modes_compat( self._api_version ) diff --git a/tests/components/esphome/test_light.py b/tests/components/esphome/test_light.py new file mode 100644 index 00000000000..df307259e53 --- /dev/null +++ b/tests/components/esphome/test_light.py @@ -0,0 +1,139 @@ +"""Test ESPHome lights.""" + + +from unittest.mock import call + +from aioesphomeapi import ( + APIClient, + APIVersion, + LightColorCapability, + LightInfo, + LightState, +) + +from homeassistant.components.light import ( + ATTR_MAX_COLOR_TEMP_KELVIN, + ATTR_MAX_MIREDS, + ATTR_MIN_COLOR_TEMP_KELVIN, + ATTR_MIN_MIREDS, + DOMAIN as LIGHT_DOMAIN, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_ON, +) +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant + + +async def test_light_no_color_temp( + hass: HomeAssistant, mock_client: APIClient, mock_generic_device_entry +) -> None: + """Test a generic light entity that does not support color temp.""" + mock_client.api_version = APIVersion(1, 7) + entity_info = [ + LightInfo( + object_id="mylight", + key=1, + name="my light", + unique_id="my_light", + min_mireds=153, + max_mireds=400, + supported_color_modes=[LightColorCapability.BRIGHTNESS], + ) + ] + states = [LightState(key=1, state=True, brightness=100)] + user_service = [] + await mock_generic_device_entry( + mock_client=mock_client, + entity_info=entity_info, + user_service=user_service, + states=states, + ) + state = hass.states.get("light.test_my_light") + assert state is not None + assert state.state == STATE_ON + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.test_my_light"}, + blocking=True, + ) + mock_client.light_command.assert_has_calls( + [call(key=1, state=True, color_mode=LightColorCapability.BRIGHTNESS)] + ) + mock_client.light_command.reset_mock() + + +async def test_light_color_temp( + hass: HomeAssistant, mock_client: APIClient, mock_generic_device_entry +) -> None: + """Test a generic light entity that does supports color temp.""" + mock_client.api_version = APIVersion(1, 7) + entity_info = [ + LightInfo( + object_id="mylight", + key=1, + name="my light", + unique_id="my_light", + min_mireds=153, + max_mireds=400, + supported_color_modes=[ + LightColorCapability.COLOR_TEMPERATURE + | LightColorCapability.ON_OFF + | LightColorCapability.BRIGHTNESS + ], + ) + ] + states = [ + LightState( + key=1, + state=True, + brightness=100, + color_temperature=153, + color_mode=LightColorCapability.COLOR_TEMPERATURE, + ) + ] + user_service = [] + await mock_generic_device_entry( + mock_client=mock_client, + entity_info=entity_info, + user_service=user_service, + states=states, + ) + state = hass.states.get("light.test_my_light") + assert state is not None + assert state.state == STATE_ON + attributes = state.attributes + + assert attributes[ATTR_MAX_MIREDS] == 400 + assert attributes[ATTR_MIN_MIREDS] == 153 + assert attributes[ATTR_MIN_COLOR_TEMP_KELVIN] == 2500 + assert attributes[ATTR_MAX_COLOR_TEMP_KELVIN] == 6535 + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.test_my_light"}, + blocking=True, + ) + mock_client.light_command.assert_has_calls( + [ + call( + key=1, + state=True, + color_mode=LightColorCapability.COLOR_TEMPERATURE + | LightColorCapability.ON_OFF + | LightColorCapability.BRIGHTNESS, + ) + ] + ) + mock_client.light_command.reset_mock() + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "light.test_my_light"}, + blocking=True, + ) + mock_client.light_command.assert_has_calls([call(key=1, state=False)]) + mock_client.light_command.reset_mock()