2019-02-13 20:21:14 +00:00
|
|
|
"""Support for RFXtrx switches."""
|
2021-12-09 21:35:53 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-09-27 09:13:49 +00:00
|
|
|
import logging
|
2019-10-14 15:38:34 +00:00
|
|
|
|
2019-10-12 19:37:59 +00:00
|
|
|
import RFXtrx as rfxtrxmod
|
2018-01-12 19:52:53 +00:00
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2022-01-13 06:00:14 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-22 21:38:55 +00:00
|
|
|
from homeassistant.const import STATE_ON
|
2022-01-13 06:00:14 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_SIGNAL_REPETITIONS,
|
2020-07-12 20:03:22 +00:00
|
|
|
DOMAIN,
|
2021-12-09 21:35:53 +00:00
|
|
|
DeviceTuple,
|
2020-07-18 11:43:38 +00:00
|
|
|
RfxtrxCommandEntity,
|
2021-12-22 21:38:55 +00:00
|
|
|
async_setup_platform_entry,
|
2021-09-09 05:17:02 +00:00
|
|
|
)
|
2021-12-22 21:38:55 +00:00
|
|
|
from .const import COMMAND_OFF_LIST, COMMAND_ON_LIST, CONF_SIGNAL_REPETITIONS
|
2015-11-26 06:52:37 +00:00
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
DATA_SWITCH = f"{DOMAIN}_switch"
|
2015-09-29 06:20:25 +00:00
|
|
|
|
2020-07-12 20:03:22 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-04-09 03:55:31 +00:00
|
|
|
|
2015-09-27 09:13:49 +00:00
|
|
|
|
2020-10-01 06:55:57 +00:00
|
|
|
def supported(event):
|
|
|
|
"""Return whether an event supports switch."""
|
|
|
|
return (
|
|
|
|
isinstance(event.device, rfxtrxmod.LightingDevice)
|
|
|
|
and not event.device.known_to_be_dimmable
|
|
|
|
and not event.device.known_to_be_rollershutter
|
|
|
|
or isinstance(event.device, rfxtrxmod.RfyDevice)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-13 21:24:28 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-13 06:00:14 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-07-13 21:24:28 +00:00
|
|
|
"""Set up config entry."""
|
2021-12-22 21:38:55 +00:00
|
|
|
|
|
|
|
def _constructor(
|
|
|
|
event: rfxtrxmod.RFXtrxEvent,
|
2022-01-13 06:00:14 +00:00
|
|
|
auto: rfxtrxmod.RFXtrxEvent | None,
|
2021-12-22 21:38:55 +00:00
|
|
|
device_id: DeviceTuple,
|
|
|
|
entity_info: dict,
|
|
|
|
):
|
|
|
|
return [
|
|
|
|
RfxtrxSwitch(
|
|
|
|
event.device,
|
|
|
|
device_id,
|
|
|
|
entity_info.get(CONF_SIGNAL_REPETITIONS, DEFAULT_SIGNAL_REPETITIONS),
|
|
|
|
event=event if auto else None,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
await async_setup_platform_entry(
|
|
|
|
hass, config_entry, async_add_entities, supported, _constructor
|
|
|
|
)
|
2015-09-27 09:13:49 +00:00
|
|
|
|
|
|
|
|
2020-07-18 11:43:38 +00:00
|
|
|
class RfxtrxSwitch(RfxtrxCommandEntity, SwitchEntity):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a RFXtrx switch."""
|
|
|
|
|
2020-07-21 22:01:31 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Restore device state."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
if self._event is None:
|
|
|
|
old_state = await self.async_get_last_state()
|
|
|
|
if old_state is not None:
|
|
|
|
self._state = old_state.state == STATE_ON
|
|
|
|
|
2021-12-09 21:35:53 +00:00
|
|
|
def _apply_event(self, event: rfxtrxmod.RFXtrxEvent) -> None:
|
2020-07-05 22:10:26 +00:00
|
|
|
"""Apply command from rfxtrx."""
|
2021-12-09 21:35:53 +00:00
|
|
|
assert isinstance(event, rfxtrxmod.ControlEvent)
|
2020-07-17 08:27:07 +00:00
|
|
|
super()._apply_event(event)
|
2020-07-05 22:10:26 +00:00
|
|
|
if event.values["Command"] in COMMAND_ON_LIST:
|
|
|
|
self._state = True
|
|
|
|
elif event.values["Command"] in COMMAND_OFF_LIST:
|
|
|
|
self._state = False
|
|
|
|
|
2020-07-13 21:24:28 +00:00
|
|
|
@callback
|
2021-12-09 21:35:53 +00:00
|
|
|
def _handle_event(
|
|
|
|
self, event: rfxtrxmod.RFXtrxEvent, device_id: DeviceTuple
|
|
|
|
) -> None:
|
2020-07-09 09:40:37 +00:00
|
|
|
"""Check if event applies to me and update."""
|
2021-07-23 16:45:31 +00:00
|
|
|
if self._event_applies(event, device_id):
|
|
|
|
self._apply_event(event)
|
2020-07-09 09:40:37 +00:00
|
|
|
|
2021-07-23 16:45:31 +00:00
|
|
|
self.async_write_ha_state()
|
2020-07-05 22:10:26 +00:00
|
|
|
|
2020-07-18 11:43:38 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._state
|
|
|
|
|
2020-07-27 22:44:30 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the device on."""
|
2020-07-27 22:44:30 +00:00
|
|
|
await self._async_send(self._device.send_on)
|
|
|
|
self._state = True
|
|
|
|
self.async_write_ha_state()
|
2020-07-05 22:10:26 +00:00
|
|
|
|
2020-07-27 22:44:30 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2020-07-05 22:10:26 +00:00
|
|
|
"""Turn the device off."""
|
2020-07-27 22:44:30 +00:00
|
|
|
await self._async_send(self._device.send_off)
|
|
|
|
self._state = False
|
|
|
|
self.async_write_ha_state()
|