2021-09-30 10:02:56 +00:00
|
|
|
"""Support for Tuya scenes."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
from tuya_iot import TuyaHomeManager, TuyaScene
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
from homeassistant.components.scene import Scene
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-12 14:49:46 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-09-30 10:02:56 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-07-24 08:29:43 +00:00
|
|
|
|
2021-10-12 14:49:46 +00:00
|
|
|
from . import HomeAssistantTuyaData
|
|
|
|
from .const import DOMAIN
|
2018-07-24 08:29:43 +00:00
|
|
|
|
2020-05-11 03:01:00 +00:00
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2021-10-12 14:49:46 +00:00
|
|
|
"""Set up Tuya scenes."""
|
|
|
|
hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
scenes = await hass.async_add_executor_job(hass_data.home_manager.query_scenes)
|
|
|
|
async_add_entities(TuyaHAScene(hass_data.home_manager, scene) for scene in scenes)
|
2020-05-11 03:01:00 +00:00
|
|
|
|
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
class TuyaHAScene(Scene):
|
|
|
|
"""Tuya Scene Remote."""
|
2020-05-11 03:01:00 +00:00
|
|
|
|
2021-10-12 14:49:46 +00:00
|
|
|
_should_poll = False
|
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
def __init__(self, home_manager: TuyaHomeManager, scene: TuyaScene) -> None:
|
|
|
|
"""Init Tuya Scene."""
|
|
|
|
super().__init__()
|
2021-10-12 14:49:46 +00:00
|
|
|
self._attr_unique_id = f"tys{scene.scene_id}"
|
2021-09-30 10:02:56 +00:00
|
|
|
self.home_manager = home_manager
|
|
|
|
self.scene = scene
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str | None:
|
|
|
|
"""Return Tuya scene name."""
|
|
|
|
return self.scene.name
|
2018-07-24 08:29:43 +00:00
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
@property
|
2021-10-12 14:49:46 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2021-09-30 10:02:56 +00:00
|
|
|
"""Return a device description for device registry."""
|
2021-10-12 14:49:46 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, f"{self.unique_id}")},
|
|
|
|
manufacturer="tuya",
|
|
|
|
name=self.scene.name,
|
|
|
|
model="Tuya Scene",
|
|
|
|
)
|
2018-07-24 08:29:43 +00:00
|
|
|
|
2021-09-30 10:02:56 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if the scene is enabled."""
|
|
|
|
return self.scene.enabled
|
2018-07-24 08:29:43 +00:00
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
def activate(self, **kwargs: Any) -> None:
|
2018-07-24 08:29:43 +00:00
|
|
|
"""Activate the scene."""
|
2021-09-30 10:02:56 +00:00
|
|
|
self.home_manager.trigger_scene(self.scene.home_id, self.scene.scene_id)
|