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
|
2021-06-17 08:10:26 +00:00
|
|
|
from typing import Any, final
|
2014-11-09 01:20:43 +00:00
|
|
|
|
2016-04-12 04:37:42 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "switch"
|
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
|
|
|
|
2017-03-19 21:02:11 +00:00
|
|
|
ATTR_TODAY_ENERGY_KWH = "today_energy_kwh"
|
2017-05-03 06:32:19 +00:00
|
|
|
ATTR_CURRENT_POWER_W = "current_power_w"
|
2014-11-09 01:20:43 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
PROP_TO_ATTR = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"current_power_w": ATTR_CURRENT_POWER_W,
|
|
|
|
"today_energy_kwh": ATTR_TODAY_ENERGY_KWH,
|
2015-06-13 21:56:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_OUTLET = "outlet"
|
|
|
|
DEVICE_CLASS_SWITCH = "switch"
|
2019-04-17 00:07:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASSES = [DEVICE_CLASS_OUTLET, DEVICE_CLASS_SWITCH]
|
2019-04-17 00:07:14 +00:00
|
|
|
|
|
|
|
DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.In(DEVICE_CLASSES))
|
|
|
|
|
2014-11-09 01:20:43 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
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."""
|
2018-07-06 21:05:34 +00:00
|
|
|
component = hass.data[DOMAIN] = EntityComponent(
|
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."""
|
2021-06-17 08:10:26 +00:00
|
|
|
component: EntityComponent = hass.data[DOMAIN]
|
|
|
|
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."""
|
2021-06-17 08:10:26 +00:00
|
|
|
component: EntityComponent = hass.data[DOMAIN]
|
|
|
|
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."""
|
|
|
|
|
|
|
|
|
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
|
2021-06-01 08:23:10 +00:00
|
|
|
_attr_current_power_w: float | None = None
|
|
|
|
_attr_today_energy_kwh: float | None = None
|
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
@property
|
2021-06-01 08:23:10 +00:00
|
|
|
def current_power_w(self) -> float | None:
|
2017-03-19 21:02:11 +00:00
|
|
|
"""Return the current power usage in W."""
|
2021-06-01 08:23:10 +00:00
|
|
|
return self._attr_current_power_w
|
2015-06-13 21:56:20 +00:00
|
|
|
|
|
|
|
@property
|
2021-06-01 08:23:10 +00:00
|
|
|
def today_energy_kwh(self) -> float | None:
|
2017-03-19 21:02:11 +00:00
|
|
|
"""Return the today total energy usage in kWh."""
|
2021-06-01 08:23:10 +00:00
|
|
|
return self._attr_today_energy_kwh
|
2015-06-13 21:56:20 +00:00
|
|
|
|
2021-03-21 09:38:24 +00:00
|
|
|
@final
|
2015-06-13 21:56:20 +00:00
|
|
|
@property
|
2021-06-01 08:23:10 +00:00
|
|
|
def state_attributes(self) -> dict[str, Any] | None:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the optional state attributes."""
|
2015-06-13 21:56:20 +00:00
|
|
|
data = {}
|
|
|
|
|
|
|
|
for prop, attr in PROP_TO_ATTR.items():
|
|
|
|
value = getattr(self, prop)
|
2020-03-10 18:28:11 +00:00
|
|
|
if value is not None:
|
2015-06-13 21:56:20 +00:00
|
|
|
data[attr] = value
|
|
|
|
|
|
|
|
return data
|
2019-04-17 00:07:14 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
|
|
|
|
class SwitchDevice(SwitchEntity):
|
|
|
|
"""Representation of a switch (for backwards compatibility)."""
|
|
|
|
|
2021-06-07 17:36:34 +00:00
|
|
|
def __init_subclass__(cls, **kwargs: Any) -> None:
|
2020-04-26 16:50:37 +00:00
|
|
|
"""Print deprecation warning."""
|
2021-06-07 17:36:34 +00:00
|
|
|
super().__init_subclass__(**kwargs) # type: ignore[call-arg]
|
2020-04-26 16:50:37 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"SwitchDevice is deprecated, modify %s to extend SwitchEntity",
|
|
|
|
cls.__name__,
|
|
|
|
)
|