Add studio mode switch to Elgato (#87691)

This commit is contained in:
Franck Nijhof 2023-02-08 11:42:53 +01:00 committed by GitHub
parent fc7e4b7664
commit 1c3713d08f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 224 additions and 1 deletions

View file

@ -6,7 +6,7 @@ from homeassistant.core import HomeAssistant
from .const import DOMAIN
from .coordinator import ElgatoDataUpdateCoordinator
PLATFORMS = [Platform.BUTTON, Platform.LIGHT, Platform.SENSOR]
PLATFORMS = [Platform.BUTTON, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View file

@ -0,0 +1,113 @@
"""Support for Elgato switches."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any
from elgato import Elgato, ElgatoError
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .coordinator import ElgatoData, ElgatoDataUpdateCoordinator
from .entity import ElgatoEntity
@dataclass
class ElgatoEntityDescriptionMixin:
"""Mixin values for Elgato entities."""
is_on_fn: Callable[[ElgatoData], bool | None]
set_fn: Callable[[Elgato, bool], Awaitable[Any]]
@dataclass
class ElgatoSwitchEntityDescription(
SwitchEntityDescription, ElgatoEntityDescriptionMixin
):
"""Class describing Elgato switch entities."""
has_fn: Callable[[ElgatoData], bool] = lambda _: True
SWITCHES = [
ElgatoSwitchEntityDescription(
key="bypass",
name="Studio mode",
icon="mdi:battery-off-outline",
entity_category=EntityCategory.CONFIG,
has_fn=lambda x: x.battery is not None,
is_on_fn=lambda x: x.settings.battery.bypass if x.settings.battery else None,
set_fn=lambda client, on: client.battery_bypass(on=on),
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Elgato switches based on a config entry."""
coordinator: ElgatoDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
ElgatoSwitchEntity(
coordinator=coordinator,
description=description,
)
for description in SWITCHES
if description.has_fn(coordinator.data)
)
class ElgatoSwitchEntity(ElgatoEntity, SwitchEntity):
"""Representation of an Elgato switch."""
entity_description: ElgatoSwitchEntityDescription
def __init__(
self,
coordinator: ElgatoDataUpdateCoordinator,
description: ElgatoSwitchEntityDescription,
) -> None:
"""Initiate Elgato switch."""
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = (
f"{coordinator.data.info.serial_number}_{description.key}"
)
@property
def is_on(self) -> bool | None:
"""Return state of the switch."""
return self.entity_description.is_on_fn(self.coordinator.data)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
try:
await self.entity_description.set_fn(self.coordinator.client, True)
except ElgatoError as error:
raise HomeAssistantError(
"An error occurred while updating the Elgato Light"
) from error
finally:
await self.coordinator.async_refresh()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
try:
await self.entity_description.set_fn(self.coordinator.client, False)
except ElgatoError as error:
raise HomeAssistantError(
"An error occurred while updating the Elgato Light"
) from error
finally:
await self.coordinator.async_refresh()

View file

@ -0,0 +1,110 @@
"""Tests for the Elgato switch platform."""
from unittest.mock import MagicMock
from elgato import ElgatoError
import pytest
from homeassistant.components.elgato.const import DOMAIN
from homeassistant.components.switch import (
DOMAIN as SWITCH_DOMAIN,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_FRIENDLY_NAME,
ATTR_ICON,
STATE_OFF,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity import EntityCategory
@pytest.mark.parametrize("device_fixtures", ["key-light-mini"])
@pytest.mark.usefixtures(
"device_fixtures",
"entity_registry_enabled_by_default",
"init_integration",
)
async def test_battery_bypass(hass: HomeAssistant, mock_elgato: MagicMock) -> None:
"""Test the Elgato battery bypass switch."""
device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)
state = hass.states.get("switch.frenck_studio_mode")
assert state
assert state.state == STATE_OFF
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Frenck Studio mode"
assert state.attributes.get(ATTR_ICON) == "mdi:battery-off-outline"
assert not state.attributes.get(ATTR_DEVICE_CLASS)
entry = entity_registry.async_get("switch.frenck_studio_mode")
assert entry
assert entry.unique_id == "GW24L1A02987_bypass"
assert entry.entity_category == EntityCategory.CONFIG
assert entry.device_id
device_entry = device_registry.async_get(entry.device_id)
assert device_entry
assert device_entry.configuration_url is None
assert device_entry.connections == {
(dr.CONNECTION_NETWORK_MAC, "aa:bb:cc:dd:ee:ff")
}
assert device_entry.entry_type is None
assert device_entry.identifiers == {(DOMAIN, "GW24L1A02987")}
assert device_entry.manufacturer == "Elgato"
assert device_entry.model == "Elgato Key Light Mini"
assert device_entry.name == "Frenck"
assert device_entry.sw_version == "1.0.4 (229)"
assert device_entry.hw_version == "202"
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.frenck_studio_mode"},
blocking=True,
)
assert len(mock_elgato.battery_bypass.mock_calls) == 1
mock_elgato.battery_bypass.assert_called_once_with(on=True)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.frenck_studio_mode"},
blocking=True,
)
assert len(mock_elgato.battery_bypass.mock_calls) == 2
mock_elgato.battery_bypass.assert_called_with(on=False)
mock_elgato.battery_bypass.side_effect = ElgatoError
with pytest.raises(
HomeAssistantError, match="An error occurred while updating the Elgato Light"
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.frenck_studio_mode"},
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_elgato.battery_bypass.mock_calls) == 3
with pytest.raises(
HomeAssistantError, match="An error occurred while updating the Elgato Light"
):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.frenck_studio_mode"},
blocking=True,
)
await hass.async_block_till_done()
assert len(mock_elgato.battery_bypass.mock_calls) == 4