From bf63d381b2d4daf6e923d2b738bc9b57c2f03b2a Mon Sep 17 00:00:00 2001 From: Jeef Date: Wed, 18 May 2022 14:54:52 -0600 Subject: [PATCH] IntelliFire On/Off Switches (#70377) Co-authored-by: J. Nick Koston --- .coveragerc | 1 + .../components/intellifire/__init__.py | 2 +- .../components/intellifire/switch.py | 93 +++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/intellifire/switch.py diff --git a/.coveragerc b/.coveragerc index fbe98e4594f..5fabf2851fb 100644 --- a/.coveragerc +++ b/.coveragerc @@ -550,6 +550,7 @@ omit = homeassistant/components/intellifire/coordinator.py homeassistant/components/intellifire/binary_sensor.py homeassistant/components/intellifire/sensor.py + homeassistant/components/intellifire/switch.py homeassistant/components/intellifire/entity.py homeassistant/components/incomfort/* homeassistant/components/intesishome/* diff --git a/homeassistant/components/intellifire/__init__.py b/homeassistant/components/intellifire/__init__.py index e139626b88c..83c6e05f572 100644 --- a/homeassistant/components/intellifire/__init__.py +++ b/homeassistant/components/intellifire/__init__.py @@ -13,7 +13,7 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from .const import DOMAIN, LOGGER from .coordinator import IntellifireDataUpdateCoordinator -PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] +PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/intellifire/switch.py b/homeassistant/components/intellifire/switch.py new file mode 100644 index 00000000000..9c196a59fd4 --- /dev/null +++ b/homeassistant/components/intellifire/switch.py @@ -0,0 +1,93 @@ +"""Define switch func.""" +from __future__ import annotations + +from collections.abc import Awaitable, Callable +from dataclasses import dataclass +from typing import Any + +from intellifire4py import IntellifireControlAsync, IntellifirePollData + +from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .coordinator import IntellifireDataUpdateCoordinator +from .entity import IntellifireEntity + + +@dataclass() +class IntellifireSwitchRequiredKeysMixin: + """Mixin for required keys.""" + + on_fn: Callable[[IntellifireControlAsync], Awaitable] + off_fn: Callable[[IntellifireControlAsync], Awaitable] + value_fn: Callable[[IntellifirePollData], bool] + + +@dataclass +class IntellifireSwitchEntityDescription( + SwitchEntityDescription, IntellifireSwitchRequiredKeysMixin +): + """Describes a switch entity.""" + + +INTELLIFIRE_SWITCHES: tuple[IntellifireSwitchEntityDescription, ...] = ( + IntellifireSwitchEntityDescription( + key="on_off", + name="Flame", + on_fn=lambda control_api: control_api.flame_on( + fireplace=control_api.default_fireplace + ), + off_fn=lambda control_api: control_api.flame_off( + fireplace=control_api.default_fireplace + ), + value_fn=lambda data: data.is_on, + ), + IntellifireSwitchEntityDescription( + key="pilot", + name="Pilot Light", + icon="mdi:fire-alert", + on_fn=lambda control_api: control_api.pilot_on( + fireplace=control_api.default_fireplace + ), + off_fn=lambda control_api: control_api.pilot_off( + fireplace=control_api.default_fireplace + ), + value_fn=lambda data: data.pilot_on, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Configure switch entities.""" + coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + + async_add_entities( + IntellifireSwitch(coordinator=coordinator, description=description) + for description in INTELLIFIRE_SWITCHES + ) + + +class IntellifireSwitch(IntellifireEntity, SwitchEntity): + """Define an Intellifire Switch.""" + + entity_description: IntellifireSwitchEntityDescription + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn on the switch.""" + await self.entity_description.on_fn(self.coordinator.control_api) + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn off the switch.""" + await self.entity_description.off_fn(self.coordinator.control_api) + + @property + def is_on(self) -> bool | None: + """Return the on state.""" + return self.entity_description.value_fn(self.coordinator.read_api.data)