Add Identify service to Elgato integration (#49990)
parent
13dee0f028
commit
0627b316e3
homeassistant/components/elgato
tests/components/elgato
|
@ -14,3 +14,6 @@ ATTR_ON = "on"
|
|||
ATTR_SOFTWARE_VERSION = "sw_version"
|
||||
|
||||
CONF_SERIAL_NUMBER = "serial_number"
|
||||
|
||||
# Services
|
||||
SERVICE_IDENTIFY = "identify"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue