diff --git a/homeassistant/components/elgato/const.py b/homeassistant/components/elgato/const.py index b2535ce0e4f..8b931fc6328 100644 --- a/homeassistant/components/elgato/const.py +++ b/homeassistant/components/elgato/const.py @@ -14,3 +14,6 @@ ATTR_ON = "on" ATTR_SOFTWARE_VERSION = "sw_version" CONF_SERIAL_NUMBER = "serial_number" + +# Services +SERVICE_IDENTIFY = "identify" diff --git a/homeassistant/components/elgato/light.py b/homeassistant/components/elgato/light.py index 89e077f8f49..7f4987b2620 100644 --- a/homeassistant/components/elgato/light.py +++ b/homeassistant/components/elgato/light.py @@ -17,8 +17,9 @@ from homeassistant.components.light import ( from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import DeviceInfo, Entity +from homeassistant.helpers.entity_platform import async_get_current_platform -from .const import DATA_ELGATO_CLIENT, DOMAIN +from .const import DATA_ELGATO_CLIENT, DOMAIN, SERVICE_IDENTIFY _LOGGER = logging.getLogger(__name__) @@ -36,6 +37,13 @@ async def async_setup_entry( info = await elgato.info() async_add_entities([ElgatoLight(elgato, info)], True) + platform = async_get_current_platform() + platform.async_register_entity_service( + SERVICE_IDENTIFY, + {}, + ElgatoLight.async_identify.__name__, + ) + class ElgatoLight(LightEntity): """Defines a Elgato Key Light.""" @@ -144,3 +152,11 @@ class ElgatoLight(LightEntity): "model": self._info.product_name, "sw_version": f"{self._info.firmware_version} ({self._info.firmware_build_number})", } + + async def async_identify(self) -> None: + """Identify the light, will make it blink.""" + try: + await self.elgato.identify() + except ElgatoError: + _LOGGER.exception("An error occurred while identifying the Elgato Light") + self._state = None diff --git a/homeassistant/components/elgato/services.yaml b/homeassistant/components/elgato/services.yaml new file mode 100644 index 00000000000..05d341a7041 --- /dev/null +++ b/homeassistant/components/elgato/services.yaml @@ -0,0 +1,9 @@ +identify: + name: Identify + description: >- + Identify an Elgato Light. Blinks the light, which can be useful + for, e.g., a visual notification. + target: + entity: + integration: elgato + domain: light diff --git a/tests/components/elgato/test_light.py b/tests/components/elgato/test_light.py index 6c4de76719f..38da5856f75 100644 --- a/tests/components/elgato/test_light.py +++ b/tests/components/elgato/test_light.py @@ -3,6 +3,7 @@ from unittest.mock import patch from elgato import ElgatoError +from homeassistant.components.elgato.const import DOMAIN, SERVICE_IDENTIFY from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, @@ -106,3 +107,50 @@ async def test_light_unavailable( await hass.async_block_till_done() state = hass.states.get("light.frenck") assert state.state == STATE_UNAVAILABLE + + +async def test_light_identify( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker +) -> None: + """Test identifying an Elgato Light.""" + await init_integration(hass, aioclient_mock) + + with patch( + "homeassistant.components.elgato.light.Elgato.identify", + return_value=mock_coro(), + ) as mock_identify: + await hass.services.async_call( + DOMAIN, + SERVICE_IDENTIFY, + { + ATTR_ENTITY_ID: "light.frenck", + }, + blocking=True, + ) + await hass.async_block_till_done() + assert len(mock_identify.mock_calls) == 1 + mock_identify.assert_called_with() + + +async def test_light_identify_error( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog +) -> None: + """Test error occurred during identifying an Elgato Light.""" + await init_integration(hass, aioclient_mock) + + with patch( + "homeassistant.components.elgato.light.Elgato.identify", + side_effect=ElgatoError, + ) as mock_identify: + await hass.services.async_call( + DOMAIN, + SERVICE_IDENTIFY, + { + ATTR_ENTITY_ID: "light.frenck", + }, + blocking=True, + ) + await hass.async_block_till_done() + assert len(mock_identify.mock_calls) == 1 + + assert "An error occurred while identifying the Elgato Light" in caplog.text