2020-03-05 06:05:39 +00:00
|
|
|
"""Support for the Dynalite channels and presets as switches."""
|
2020-04-02 18:26:36 +00:00
|
|
|
|
2022-08-19 14:10:45 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2020-04-02 18:26:36 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-11-12 21:59:29 +00:00
|
|
|
from homeassistant.const import STATE_ON
|
2020-04-02 18:26:36 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 21:36:48 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-03-05 06:05:39 +00:00
|
|
|
|
|
|
|
from .dynalitebase import DynaliteBase, async_setup_entry_base
|
|
|
|
|
|
|
|
|
2020-04-02 18:26:36 +00:00
|
|
|
async def async_setup_entry(
|
2021-05-04 21:36:48 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-04-02 18:26:36 +00:00
|
|
|
) -> None:
|
2020-03-05 06:05:39 +00:00
|
|
|
"""Record the async_add_entities function to add them later when received from Dynalite."""
|
|
|
|
async_setup_entry_base(
|
|
|
|
hass, config_entry, async_add_entities, "switch", DynaliteSwitch
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class DynaliteSwitch(DynaliteBase, SwitchEntity):
|
2020-03-05 06:05:39 +00:00
|
|
|
"""Representation of a Dynalite Channel as a Home Assistant Switch."""
|
|
|
|
|
|
|
|
@property
|
2020-04-02 18:26:36 +00:00
|
|
|
def is_on(self) -> bool:
|
2020-03-05 06:05:39 +00:00
|
|
|
"""Return true if switch is on."""
|
|
|
|
return self._device.is_on
|
|
|
|
|
2022-08-19 14:10:45 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-03-05 06:05:39 +00:00
|
|
|
"""Turn the switch on."""
|
|
|
|
await self._device.async_turn_on()
|
|
|
|
|
2022-08-19 14:10:45 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-03-05 06:05:39 +00:00
|
|
|
"""Turn the switch off."""
|
|
|
|
await self._device.async_turn_off()
|
2022-11-12 21:59:29 +00:00
|
|
|
|
|
|
|
def initialize_state(self, state):
|
|
|
|
"""Initialize the state from cache."""
|
|
|
|
target_level = 1 if state.state == STATE_ON else 0
|
|
|
|
self._device.init_level(target_level)
|