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