2019-04-03 15:40:03 +00:00
|
|
|
"""Support for switches which integrates with other components."""
|
2022-01-03 15:23:12 +00:00
|
|
|
from __future__ import annotations
|
2016-09-08 14:26:54 +00:00
|
|
|
|
2016-08-22 22:05:45 +00:00
|
|
|
import voluptuous as vol
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2016-08-22 22:05:45 +00:00
|
|
|
from homeassistant.components.switch import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_FORMAT,
|
|
|
|
PLATFORM_SCHEMA,
|
2020-04-26 16:50:37 +00:00
|
|
|
SwitchEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-02-02 19:25:17 +00:00
|
|
|
from homeassistant.const import (
|
2019-12-08 20:05:08 +00:00
|
|
|
ATTR_ENTITY_ID,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FRIENDLY_NAME,
|
|
|
|
CONF_SWITCHES,
|
2020-08-01 22:45:55 +00:00
|
|
|
CONF_UNIQUE_ID,
|
2019-12-08 20:05:08 +00:00
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-03 15:23:12 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2016-02-02 19:25:17 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2017-03-02 13:09:53 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-10-17 05:00:55 +00:00
|
|
|
from homeassistant.helpers.entity import async_generate_entity_id
|
2022-01-03 15:23:12 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-07-06 23:32:33 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2016-09-08 14:26:54 +00:00
|
|
|
from homeassistant.helpers.script import Script
|
2022-01-03 15:23:12 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-12-08 20:05:08 +00:00
|
|
|
|
2021-12-16 16:11:47 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
from .template_entity import (
|
|
|
|
TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY,
|
|
|
|
TemplateEntity,
|
|
|
|
rewrite_common_legacy_to_modern_conf,
|
|
|
|
)
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ON_ACTION = "turn_on"
|
|
|
|
OFF_ACTION = "turn_off"
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2020-09-01 13:53:50 +00:00
|
|
|
SWITCH_SCHEMA = vol.All(
|
|
|
|
cv.deprecated(ATTR_ENTITY_ID),
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Required(ON_ACTION): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Required(OFF_ACTION): cv.SCRIPT_SCHEMA,
|
|
|
|
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
|
|
|
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
|
|
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
|
|
|
}
|
2021-12-16 16:11:47 +00:00
|
|
|
).extend(TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY.schema),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-08-22 22:05:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_SWITCHES): cv.schema_with_slug_keys(SWITCH_SCHEMA)}
|
|
|
|
)
|
2016-08-22 22:05:45 +00:00
|
|
|
|
2016-02-03 14:30:58 +00:00
|
|
|
|
2020-08-25 22:25:15 +00:00
|
|
|
async def _async_create_entities(hass, config):
|
2020-08-21 23:31:48 +00:00
|
|
|
"""Create the Template switches."""
|
2016-02-02 19:25:17 +00:00
|
|
|
switches = []
|
|
|
|
|
2021-12-16 16:11:47 +00:00
|
|
|
for object_id, entity_config in config[CONF_SWITCHES].items():
|
|
|
|
entity_config = rewrite_common_legacy_to_modern_conf(entity_config)
|
|
|
|
unique_id = entity_config.get(CONF_UNIQUE_ID)
|
2019-09-25 04:30:48 +00:00
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
switches.append(
|
|
|
|
SwitchTemplate(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
2021-12-16 16:11:47 +00:00
|
|
|
object_id,
|
|
|
|
entity_config,
|
2020-08-01 22:45:55 +00:00
|
|
|
unique_id,
|
2016-02-02 19:25:17 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-11-26 00:30:49 +00:00
|
|
|
|
2020-08-21 23:31:48 +00:00
|
|
|
return switches
|
|
|
|
|
|
|
|
|
2022-01-03 15:23:12 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2020-08-21 23:31:48 +00:00
|
|
|
"""Set up the template switches."""
|
2020-08-25 22:25:15 +00:00
|
|
|
async_add_entities(await _async_create_entities(hass, config))
|
2016-02-02 19:25:17 +00:00
|
|
|
|
|
|
|
|
2020-08-21 12:33:53 +00:00
|
|
|
class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a Template switch."""
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass,
|
2021-12-16 16:11:47 +00:00
|
|
|
object_id,
|
|
|
|
config,
|
2020-08-01 22:45:55 +00:00
|
|
|
unique_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the Template switch."""
|
2022-01-24 22:16:31 +00:00
|
|
|
super().__init__(
|
|
|
|
hass, config=config, fallback_name=object_id, unique_id=unique_id
|
|
|
|
)
|
2017-05-02 20:47:20 +00:00
|
|
|
self.entity_id = async_generate_entity_id(
|
2021-12-16 16:11:47 +00:00
|
|
|
ENTITY_ID_FORMAT, object_id, hass=hass
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-24 14:44:05 +00:00
|
|
|
friendly_name = self._attr_name
|
2021-12-16 16:11:47 +00:00
|
|
|
self._template = config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
self._on_script = Script(hass, config[ON_ACTION], friendly_name, DOMAIN)
|
|
|
|
self._off_script = Script(hass, config[OFF_ACTION], friendly_name, DOMAIN)
|
2016-03-25 06:07:19 +00:00
|
|
|
self._state = False
|
2017-03-02 13:09:53 +00:00
|
|
|
|
2020-08-20 13:51:27 +00:00
|
|
|
@callback
|
|
|
|
def _update_state(self, result):
|
|
|
|
super()._update_state(result)
|
|
|
|
if isinstance(result, TemplateError):
|
|
|
|
self._state = None
|
|
|
|
return
|
2020-10-06 22:05:52 +00:00
|
|
|
|
|
|
|
if isinstance(result, bool):
|
|
|
|
self._state = result
|
|
|
|
return
|
|
|
|
|
|
|
|
if isinstance(result, str):
|
|
|
|
self._state = result.lower() in ("true", STATE_ON)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._state = False
|
2020-08-20 13:51:27 +00:00
|
|
|
|
2018-10-01 06:55:00 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-03-02 13:09:53 +00:00
|
|
|
"""Register callbacks."""
|
2020-07-06 23:32:33 +00:00
|
|
|
if self._template is None:
|
|
|
|
|
|
|
|
# restore state after startup
|
|
|
|
await super().async_added_to_hass()
|
2021-10-30 14:29:07 +00:00
|
|
|
if state := await self.async_get_last_state():
|
2020-07-06 23:32:33 +00:00
|
|
|
self._state = state.state == STATE_ON
|
|
|
|
|
|
|
|
# no need to listen for events
|
2020-08-20 13:51:27 +00:00
|
|
|
else:
|
|
|
|
self.add_template_attribute(
|
|
|
|
"_state", self._template, None, self._update_state
|
|
|
|
)
|
2017-03-02 13:09:53 +00:00
|
|
|
|
2020-08-20 13:51:27 +00:00
|
|
|
await super().async_added_to_hass()
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2016-03-25 06:07:19 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._state
|
|
|
|
|
2016-02-02 19:25:17 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return the polling state."""
|
2016-02-02 19:25:17 +00:00
|
|
|
return False
|
|
|
|
|
2018-10-01 06:55:00 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Fire the on action."""
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._on_script.async_run(context=self._context)
|
2020-07-06 23:32:33 +00:00
|
|
|
if self._template is None:
|
|
|
|
self._state = True
|
|
|
|
self.async_write_ha_state()
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2018-10-01 06:55:00 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Fire the off action."""
|
2018-10-15 09:38:49 +00:00
|
|
|
await self._off_script.async_run(context=self._context)
|
2020-07-06 23:32:33 +00:00
|
|
|
if self._template is None:
|
|
|
|
self._state = False
|
|
|
|
self.async_write_ha_state()
|
2016-02-02 19:25:17 +00:00
|
|
|
|
2020-07-06 23:32:33 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""State is assumed, if no template given."""
|
|
|
|
return self._template is None
|