core/homeassistant/components/hunterdouglas_powerview/scene.py

98 lines
2.9 KiB
Python
Raw Normal View History

"""Support for Powerview scenes from a Powerview hub."""
from __future__ import annotations
from typing import Any
2016-03-06 14:42:11 +00:00
from aiopvapi.resources.scene import Scene as PvScene
import voluptuous as vol
from homeassistant.components.scene import Scene
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PLATFORM
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .const import (
COORDINATOR,
DEVICE_INFO,
DOMAIN,
HUB_ADDRESS,
PV_API,
PV_ROOM_DATA,
PV_SCENE_DATA,
ROOM_NAME_UNICODE,
STATE_ATTRIBUTE_ROOM_NAME,
)
from .entity import HDEntity
2016-03-06 14:42:11 +00:00
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = vol.Schema(
{vol.Required(CONF_PLATFORM): DOMAIN, vol.Required(HUB_ADDRESS): cv.string}
2019-07-31 19:25:30 +00:00
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Import platform from yaml."""
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_HOST: config[HUB_ADDRESS]},
)
)
2016-03-06 14:42:11 +00:00
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up powerview scene entries."""
pv_data = hass.data[DOMAIN][entry.entry_id]
room_data = pv_data[PV_ROOM_DATA]
scene_data = pv_data[PV_SCENE_DATA]
pv_request = pv_data[PV_API]
coordinator = pv_data[COORDINATOR]
device_info = pv_data[DEVICE_INFO]
2016-03-06 14:42:11 +00:00
pvscenes = []
for raw_scene in scene_data.values():
scene = PvScene(raw_scene, pv_request)
room_name = room_data.get(scene.room_id, {}).get(ROOM_NAME_UNICODE, "")
pvscenes.append(PowerViewScene(coordinator, device_info, room_name, scene))
async_add_entities(pvscenes)
2016-03-06 14:42:11 +00:00
class PowerViewScene(HDEntity, Scene):
2016-03-09 10:15:04 +00:00
"""Representation of a Powerview scene."""
2016-03-06 14:42:11 +00:00
def __init__(self, coordinator, device_info, room_name, scene):
2016-03-09 10:15:04 +00:00
"""Initialize the scene."""
super().__init__(coordinator, device_info, room_name, scene.id)
self._scene = scene
2016-03-06 14:42:11 +00:00
@property
def name(self):
2016-03-09 10:15:04 +00:00
"""Return the name of the scene."""
return self._scene.name
2016-03-06 14:42:11 +00:00
@property
def extra_state_attributes(self):
2016-03-09 10:15:04 +00:00
"""Return the state attributes."""
return {STATE_ATTRIBUTE_ROOM_NAME: self._room_name}
2016-03-06 14:42:11 +00:00
@property
def icon(self):
"""Icon to use in the frontend."""
2019-07-31 19:25:30 +00:00
return "mdi:blinds"
async def async_activate(self, **kwargs: Any) -> None:
"""Activate scene. Try to get entities into requested state."""
await self._scene.activate()