"""Switch entities for Boiler Auxiliary Heater Control.""" from __future__ import annotations from typing import Any from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity from .const import DOMAIN, SWITCH_DEFINITIONS async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up switch entities.""" coordinator = hass.data[DOMAIN][entry.entry_id] entities = [] for key, defn in SWITCH_DEFINITIONS.items(): entity = BoilerAuxHeaterSwitch(coordinator, entry, key, defn) entities.append(entity) async_add_entities(entities) class BoilerAuxHeaterSwitch(RestoreEntity, SwitchEntity): """A mode toggle switch for the boiler aux heater.""" _attr_has_entity_name = True _attr_entity_category = EntityCategory.CONFIG def __init__( self, coordinator, entry: ConfigEntry, key: str, defn: dict, ) -> None: """Initialize the switch entity.""" self.coordinator = coordinator self._key = key self._defn = defn self._attr_unique_id = f"{entry.entry_id}_{key}" self._attr_translation_key = key self._attr_icon = defn["icon"] self._attr_is_on = defn["default"] self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, entry.entry_id)}, name="Boiler Auxiliary Heater", manufacturer="Custom", ) async def async_added_to_hass(self) -> None: """Restore state on startup.""" await super().async_added_to_hass() last_state = await self.async_get_last_state() if last_state and last_state.state not in ("unknown", "unavailable"): self._attr_is_on = last_state.state == "on" # Register with coordinator self.coordinator.register_switch(self._key, self) async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the switch.""" self._attr_is_on = True self.async_write_ha_state() self.coordinator.async_request_refresh() async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the switch.""" self._attr_is_on = False self.async_write_ha_state() self.coordinator.async_request_refresh()