2020-05-15 21:48:17 +00:00
|
|
|
"""BleBox switch implementation."""
|
2022-06-29 09:57:55 +00:00
|
|
|
from datetime import timedelta
|
2022-08-19 07:54:13 +00:00
|
|
|
from typing import Any
|
2022-06-29 09:57:55 +00:00
|
|
|
|
2022-10-19 15:49:40 +00:00
|
|
|
from blebox_uniapi.box import Box
|
|
|
|
import blebox_uniapi.switch
|
|
|
|
|
2022-07-11 10:24:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
2022-01-03 15:31:24 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-05-15 21:48:17 +00:00
|
|
|
|
2022-10-19 15:49:40 +00:00
|
|
|
from . import BleBoxEntity
|
|
|
|
from .const import DOMAIN, PRODUCT
|
2020-05-15 21:48:17 +00:00
|
|
|
|
2022-06-29 09:57:55 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=5)
|
|
|
|
|
2020-05-15 21:48:17 +00:00
|
|
|
|
2022-01-03 15:31:24 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-05-15 21:48:17 +00:00
|
|
|
"""Set up a BleBox switch entity."""
|
2022-10-19 15:49:40 +00:00
|
|
|
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
|
|
|
|
entities = [
|
|
|
|
BleBoxSwitchEntity(feature) for feature in product.features.get("switches", [])
|
|
|
|
]
|
|
|
|
async_add_entities(entities, True)
|
2020-05-15 21:48:17 +00:00
|
|
|
|
|
|
|
|
2022-10-19 15:49:40 +00:00
|
|
|
class BleBoxSwitchEntity(BleBoxEntity[blebox_uniapi.switch.Switch], SwitchEntity):
|
2020-05-15 21:48:17 +00:00
|
|
|
"""Representation of a BleBox switch feature."""
|
|
|
|
|
2023-08-28 10:49:20 +00:00
|
|
|
_attr_device_class = SwitchDeviceClass.SWITCH
|
2020-05-15 21:48:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return whether switch is on."""
|
|
|
|
return self._feature.is_on
|
|
|
|
|
2022-08-19 07:54:13 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-05-15 21:48:17 +00:00
|
|
|
"""Turn on the switch."""
|
2020-05-17 23:56:49 +00:00
|
|
|
await self._feature.async_turn_on()
|
2020-05-15 21:48:17 +00:00
|
|
|
|
2022-08-19 07:54:13 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-05-15 21:48:17 +00:00
|
|
|
"""Turn off the switch."""
|
2020-05-17 23:56:49 +00:00
|
|
|
await self._feature.async_turn_off()
|