2019-04-03 15:40:03 +00:00
|
|
|
"""Component to interface with switches that can be controlled remotely."""
|
2021-06-01 08:23:10 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-07-26 22:22:21 +00:00
|
|
|
from dataclasses import dataclass
|
2014-12-05 05:06:45 +00:00
|
|
|
from datetime import timedelta
|
2015-09-27 06:17:04 +00:00
|
|
|
import logging
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2016-04-12 04:37:42 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-12-03 08:31:17 +00:00
|
|
|
from homeassistant.backports.enum import StrEnum
|
2021-06-01 08:23:10 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-12-08 17:50:17 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
SERVICE_TOGGLE,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
2021-06-01 08:23:10 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-11-16 09:22:07 +00:00
|
|
|
from homeassistant.helpers.config_validation import ( # noqa: F401
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
PLATFORM_SCHEMA_BASE,
|
|
|
|
)
|
2021-07-26 22:22:21 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity, ToggleEntityDescription
|
2019-12-08 17:50:17 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
2021-06-01 08:23:10 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-12-08 17:50:17 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2019-08-12 03:38:18 +00:00
|
|
|
|
2022-03-04 19:02:17 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
2017-01-05 23:16:12 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
2021-11-30 23:38:45 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-04-17 00:07:14 +00:00
|
|
|
|
|
|
|
|
2021-11-30 23:38:45 +00:00
|
|
|
class SwitchDeviceClass(StrEnum):
|
|
|
|
"""Device class for switches."""
|
2019-04-17 00:07:14 +00:00
|
|
|
|
2021-11-30 23:38:45 +00:00
|
|
|
OUTLET = "outlet"
|
|
|
|
SWITCH = "switch"
|
|
|
|
|
|
|
|
|
|
|
|
DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(SwitchDeviceClass))
|
|
|
|
|
|
|
|
# DEVICE_CLASS* below are deprecated as of 2021.12
|
|
|
|
# use the SwitchDeviceClass enum instead.
|
|
|
|
DEVICE_CLASSES = [cls.value for cls in SwitchDeviceClass]
|
|
|
|
DEVICE_CLASS_OUTLET = SwitchDeviceClass.OUTLET.value
|
|
|
|
DEVICE_CLASS_SWITCH = SwitchDeviceClass.SWITCH.value
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2022-09-17 16:18:53 +00:00
|
|
|
# mypy: disallow-any-generics
|
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2021-06-07 17:36:34 +00:00
|
|
|
def is_on(hass: HomeAssistant, entity_id: str) -> bool:
|
2017-02-02 05:44:05 +00:00
|
|
|
"""Return if the switch is on based on the statemachine.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2014-11-09 01:20:43 +00:00
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
|
|
|
|
|
|
|
|
2021-06-01 08:23:10 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Track states and offer events for switches."""
|
2022-09-17 16:18:53 +00:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent[SwitchEntity](
|
2020-01-07 16:30:53 +00:00
|
|
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-02-24 18:24:33 +00:00
|
|
|
await component.async_setup(config)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2019-09-03 07:50:24 +00:00
|
|
|
component.async_register_entity_service(SERVICE_TURN_OFF, {}, "async_turn_off")
|
|
|
|
component.async_register_entity_service(SERVICE_TURN_ON, {}, "async_turn_on")
|
|
|
|
component.async_register_entity_service(SERVICE_TOGGLE, {}, "async_toggle")
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
return True
|
2015-06-13 21:56:20 +00:00
|
|
|
|
|
|
|
|
2021-06-01 08:23:10 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up a config entry."""
|
2022-09-17 16:18:53 +00:00
|
|
|
component: EntityComponent[SwitchEntity] = hass.data[DOMAIN]
|
2021-06-17 08:10:26 +00:00
|
|
|
return await component.async_setup_entry(entry)
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
|
2021-06-01 08:23:10 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-07-06 21:05:34 +00:00
|
|
|
"""Unload a config entry."""
|
2022-09-17 16:18:53 +00:00
|
|
|
component: EntityComponent[SwitchEntity] = hass.data[DOMAIN]
|
2021-06-17 08:10:26 +00:00
|
|
|
return await component.async_unload_entry(entry)
|
2018-07-06 21:05:34 +00:00
|
|
|
|
|
|
|
|
2021-07-26 22:22:21 +00:00
|
|
|
@dataclass
|
|
|
|
class SwitchEntityDescription(ToggleEntityDescription):
|
|
|
|
"""A class that describes switch entities."""
|
|
|
|
|
2022-12-06 13:50:44 +00:00
|
|
|
device_class: SwitchDeviceClass | None = None
|
2021-11-30 23:38:45 +00:00
|
|
|
|
2021-07-26 22:22:21 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class SwitchEntity(ToggleEntity):
|
2021-03-21 09:38:24 +00:00
|
|
|
"""Base class for switch entities."""
|
2015-06-13 21:56:20 +00:00
|
|
|
|
2021-07-26 22:22:21 +00:00
|
|
|
entity_description: SwitchEntityDescription
|
2022-12-06 13:50:44 +00:00
|
|
|
_attr_device_class: SwitchDeviceClass | None
|
2015-06-13 21:56:20 +00:00
|
|
|
|
2021-11-30 23:38:45 +00:00
|
|
|
@property
|
2022-12-06 13:50:44 +00:00
|
|
|
def device_class(self) -> SwitchDeviceClass | None:
|
2021-11-30 23:38:45 +00:00
|
|
|
"""Return the class of this entity."""
|
|
|
|
if hasattr(self, "_attr_device_class"):
|
|
|
|
return self._attr_device_class
|
|
|
|
if hasattr(self, "entity_description"):
|
|
|
|
return self.entity_description.device_class
|
|
|
|
return None
|