Repository: home-assistant/core
File: homeassistant/components/cambridge_audio/switch.py (Python)

Code before:

from aiostreammagic import StreamMagicClient

from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from . import CambridgeAudioConfigEntry
from .entity import CambridgeAudioEntity, command

PARALLEL_UPDATES = 0


@dataclass(frozen=True, kw_only=True)
class CambridgeAudioSwitchEntityDescription(SwitchEntityDescription):
    """Describes Cambridge Audio switch entity."""

    load_fn: Callable[[StreamMagicClient], bool] = field(default=lambda _: True)
    value_fn: Callable[[StreamMagicClient], bool]
    set_value_fn: Callable[[StreamMagicClient, bool], Awaitable[None]]


def room_correction_enabled(client: StreamMagicClient) -> bool:
    """Check if room correction is enabled."""
    assert client.audio.tilt_eq is not None
    return client.audio.tilt_eq.enabled


CONTROL_ENTITIES: tuple[CambridgeAudioSwitchEntityDescription, ...] = (
    CambridgeAudioSwitchEntityDescription(
        key="pre_amp",
        translation_key="pre_amp",
        entity_category=EntityCategory.CONFIG,
        value_fn=lambda client: client.state.pre_amp_mode,
        set_value_fn=lambda client, value: client.set_pre_amp_mode(value),
    ),
    CambridgeAudioSwitchEntityDescription(
        key="early_update",
        translation_key="early_update",
        entity_category=EntityCategory.CONFIG,
        value_fn=lambda client: client.update.early_update,
        set_value_fn=lambda client, value: client.set_early_update(value),
    ),
    CambridgeAudioSwitchEntityDescription(
        key="room_correction",
        translation_key="room_correction",
        entity_category=EntityCategory.CONFIG,
        load_fn=lambda client: client.audio.tilt_eq is not None,
        value_fn=room_correction_enabled,
        set_value_fn=lambda client, value: client.set_room_correction_mode(value),

Review comment (suggestion):
```suggestion
    if TYPE_CHECKING:
        assert client.audio.tilt_eq is not None
```
https://developers.home-assistant.io/docs/development_guidelines/#typing

Code after:

from aiostreammagic import StreamMagicClient

from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from . import CambridgeAudioConfigEntry
from .entity import CambridgeAudioEntity, command

PARALLEL_UPDATES = 0


@dataclass(frozen=True, kw_only=True)
class CambridgeAudioSwitchEntityDescription(SwitchEntityDescription):
    """Describes Cambridge Audio switch entity."""

    load_fn: Callable[[StreamMagicClient], bool] = field(default=lambda _: True)
    value_fn: Callable[[StreamMagicClient], bool]
    set_value_fn: Callable[[StreamMagicClient, bool], Awaitable[None]]


def room_correction_enabled(client: StreamMagicClient) -> bool:
    """Check if room correction is enabled."""
    if TYPE_CHECKING:
        assert client.audio.tilt_eq is not None
    return client.audio.tilt_eq.enabled


CONTROL_ENTITIES: tuple[CambridgeAudioSwitchEntityDescription, ...] = (
    CambridgeAudioSwitchEntityDescription(
        key="pre_amp",
        translation_key="pre_amp",
        entity_category=EntityCategory.CONFIG,
        value_fn=lambda client: client.state.pre_amp_mode,
        set_value_fn=lambda client, value: client.set_pre_amp_mode(value),
    ),
    CambridgeAudioSwitchEntityDescription(
        key="early_update",
        translation_key="early_update",
        entity_category=EntityCategory.CONFIG,
        value_fn=lambda client: client.update.early_update,
        set_value_fn=lambda client, value: client.set_early_update(value),
    ),
    CambridgeAudioSwitchEntityDescription(
        key="room_correction",
        translation_key="room_correction",
        entity_category=EntityCategory.CONFIG,
        load_fn=lambda client: client.audio.tilt_eq is not None,
        value_fn=room_correction_enabled,