Home Assistant Unofficial Reference 2024.12.1
cover.py
Go to the documentation of this file.
1 """Cover support for switch entities."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
8  DOMAIN as COVER_DOMAIN,
9  CoverEntity,
10  CoverEntityFeature,
11 )
12 from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import (
15  ATTR_ENTITY_ID,
16  CONF_ENTITY_ID,
17  SERVICE_TURN_OFF,
18  SERVICE_TURN_ON,
19  STATE_ON,
20 )
21 from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
22 from homeassistant.helpers import entity_registry as er
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 
25 from .const import CONF_INVERT
26 from .entity import BaseInvertableEntity
27 
28 
30  hass: HomeAssistant,
31  config_entry: ConfigEntry,
32  async_add_entities: AddEntitiesCallback,
33 ) -> None:
34  """Initialize Cover Switch config entry."""
35  registry = er.async_get(hass)
36  entity_id = er.async_validate_entity_id(
37  registry, config_entry.options[CONF_ENTITY_ID]
38  )
39 
41  [
43  hass,
44  config_entry.title,
45  COVER_DOMAIN,
46  config_entry.options[CONF_INVERT],
47  entity_id,
48  config_entry.entry_id,
49  )
50  ]
51  )
52 
53 
55  """Represents a Switch as a Cover."""
56 
57  _attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
58 
59  async def async_open_cover(self, **kwargs: Any) -> None:
60  """Open the cover."""
61  await self.hasshass.services.async_call(
62  SWITCH_DOMAIN,
63  SERVICE_TURN_OFF if self._invert_state_invert_state else SERVICE_TURN_ON,
64  {ATTR_ENTITY_ID: self._switch_entity_id_switch_entity_id},
65  blocking=True,
66  context=self._context_context,
67  )
68 
69  async def async_close_cover(self, **kwargs: Any) -> None:
70  """Close cover."""
71  await self.hasshass.services.async_call(
72  SWITCH_DOMAIN,
73  SERVICE_TURN_ON if self._invert_state_invert_state else SERVICE_TURN_OFF,
74  {ATTR_ENTITY_ID: self._switch_entity_id_switch_entity_id},
75  blocking=True,
76  context=self._context_context,
77  )
78 
79  @callback
81  self, event: Event[EventStateChangedData] | None = None
82  ) -> None:
83  """Handle child updates."""
84  super().async_state_changed_listener(event)
85  if (
86  not self.availableavailable
87  or (state := self.hasshass.states.get(self._switch_entity_id_switch_entity_id)) is None
88  ):
89  return
90 
91  if self._invert_state_invert_state:
92  self._attr_is_closed_attr_is_closed = state.state == STATE_ON
93  else:
94  self._attr_is_closed_attr_is_closed = state.state != STATE_ON
None async_state_changed_listener(self, Event[EventStateChangedData]|None event=None)
Definition: cover.py:82
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: cover.py:33