2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Powerview scenes from a Powerview hub."""
|
2022-01-03 16:17:44 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
from typing import Any
|
2016-03-06 14:42:11 +00:00
|
|
|
|
2019-12-04 13:15:39 +00:00
|
|
|
from aiopvapi.resources.scene import Scene as PvScene
|
2017-05-26 20:19:19 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-29 21:24:57 +00:00
|
|
|
from homeassistant.components.scene import Scene
|
2022-01-03 16:17:44 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2020-04-29 21:24:57 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PLATFORM
|
2022-01-03 16:17:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-04 13:15:39 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 16:17:44 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2020-04-29 21:24:57 +00:00
|
|
|
|
|
|
|
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(
|
2020-04-29 21:24:57 +00:00
|
|
|
{vol.Required(CONF_PLATFORM): DOMAIN, vol.Required(HUB_ADDRESS): cv.string}
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-05-26 20:19:19 +00:00
|
|
|
|
2019-01-04 21:19:06 +00:00
|
|
|
|
2022-01-03 16:17:44 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2020-04-29 21:24:57 +00:00
|
|
|
"""Import platform from yaml."""
|
2017-05-26 20:19:19 +00:00
|
|
|
|
2020-04-29 21:24:57 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2022-01-03 16:17:44 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2020-04-29 21:24:57 +00:00
|
|
|
"""Set up powerview scene entries."""
|
2019-01-04 21:19:06 +00:00
|
|
|
|
2020-04-29 21:24:57 +00:00
|
|
|
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
|
|
|
|
2021-02-20 06:17:00 +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))
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(pvscenes)
|
2016-03-06 14:42:11 +00:00
|
|
|
|
|
|
|
|
2020-04-29 21:24:57 +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
|
|
|
|
2021-02-20 06:17:00 +00:00
|
|
|
def __init__(self, coordinator, device_info, room_name, scene):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Initialize the scene."""
|
2021-02-20 06:17:00 +00:00
|
|
|
super().__init__(coordinator, device_info, room_name, scene.id)
|
2017-10-23 06:34:50 +00:00
|
|
|
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."""
|
2017-10-23 06:34:50 +00:00
|
|
|
return self._scene.name
|
2016-03-06 14:42:11 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Return the state attributes."""
|
2017-05-26 20:19:19 +00:00
|
|
|
return {STATE_ATTRIBUTE_ROOM_NAME: self._room_name}
|
2016-03-06 14:42:11 +00:00
|
|
|
|
2017-01-10 12:21:15 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:blinds"
|
2017-01-10 12:21:15 +00:00
|
|
|
|
2020-04-21 01:07:50 +00:00
|
|
|
async def async_activate(self, **kwargs: Any) -> None:
|
2017-05-26 20:19:19 +00:00
|
|
|
"""Activate scene. Try to get entities into requested state."""
|
2019-01-04 21:19:06 +00:00
|
|
|
await self._scene.activate()
|