1 """Component to interface with switches that can be controlled remotely."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from enum
import StrEnum
7 from functools
import partial
10 from propcache
import cached_property
11 import voluptuous
as vol
23 DeprecatedConstantEnum,
24 all_with_deprecated_constants,
25 check_if_deprecated_constant,
26 dir_with_deprecated_constants,
34 from .const
import DOMAIN
36 _LOGGER = logging.getLogger(__name__)
38 DATA_COMPONENT: HassKey[EntityComponent[SwitchEntity]] =
HassKey(DOMAIN)
39 ENTITY_ID_FORMAT = DOMAIN +
".{}"
40 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
41 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
48 """Device class for switches."""
54 DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(SwitchDeviceClass))
58 DEVICE_CLASSES = [cls.value
for cls
in SwitchDeviceClass]
60 SwitchDeviceClass.OUTLET,
"2025.1"
63 SwitchDeviceClass.SWITCH,
"2025.1"
70 def is_on(hass: HomeAssistant, entity_id: str) -> bool:
71 """Return if the switch is on based on the statemachine.
75 return hass.states.is_state(entity_id, STATE_ON)
78 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
79 """Track states and offer events for switches."""
80 component = hass.data[DATA_COMPONENT] = EntityComponent[SwitchEntity](
81 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
83 await component.async_setup(config)
85 component.async_register_entity_service(SERVICE_TURN_OFF,
None,
"async_turn_off")
86 component.async_register_entity_service(SERVICE_TURN_ON,
None,
"async_turn_on")
87 component.async_register_entity_service(SERVICE_TOGGLE,
None,
"async_toggle")
93 """Set up a config entry."""
98 """Unload a config entry."""
103 """A class that describes switch entities."""
105 device_class: SwitchDeviceClass |
None =
None
108 CACHED_PROPERTIES_WITH_ATTR_ = {
114 """Base class for switch entities."""
116 entity_description: SwitchEntityDescription
117 _attr_device_class: SwitchDeviceClass |
None
121 """Return the class of this entity."""
122 if hasattr(self,
"_attr_device_class"):
123 return self._attr_device_class
124 if hasattr(self,
"entity_description"):
125 return self.entity_description.device_class
130 __getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
132 dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
SwitchDeviceClass|None device_class(self)
bool is_on(HomeAssistant hass, str entity_id)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
list[str] all_with_deprecated_constants(dict[str, Any] module_globals)