"""Cover support for switch entities.""" from __future__ import annotations from typing import Any from homeassistant.components.cover import ( DOMAIN as COVER_DOMAIN, CoverEntity, CoverEntityFeature, ) from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_ENTITY_ID, CONF_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON, ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import EventStateChangedData from homeassistant.helpers.typing import EventType from .entity import BaseEntity async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Initialize Cover Switch config entry.""" registry = er.async_get(hass) entity_id = er.async_validate_entity_id( registry, config_entry.options[CONF_ENTITY_ID] ) async_add_entities( [ CoverSwitch( hass, config_entry.title, COVER_DOMAIN, entity_id, config_entry.entry_id, ) ] ) class CoverSwitch(BaseEntity, CoverEntity): """Represents a Switch as a Cover.""" _attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE async def async_open_cover(self, **kwargs: Any) -> None: """Open the cover.""" await self.hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: self._switch_entity_id}, blocking=True, context=self._context, ) async def async_close_cover(self, **kwargs: Any) -> None: """Close cover.""" await self.hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: self._switch_entity_id}, blocking=True, context=self._context, ) @callback def async_state_changed_listener( self, event: EventType[EventStateChangedData] | None = None ) -> None: """Handle child updates.""" super().async_state_changed_listener(event) if ( not self.available or (state := self.hass.states.get(self._switch_entity_id)) is None ): return self._attr_is_closed = state.state != STATE_ON