2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Powerview scenes from a Powerview hub."""
|
2016-03-06 14:42:11 +00:00
|
|
|
import logging
|
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
|
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_PLATFORM
|
2019-12-04 13:15:39 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
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
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
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
|
|
|
|
2020-04-30 00:32:05 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=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
|
|
|
|
|
|
|
|
2020-04-29 21:24:57 +00:00
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
pvscenes = (
|
2020-04-29 21:24:57 +00:00
|
|
|
PowerViewScene(
|
|
|
|
PvScene(raw_scene, pv_request), room_data, coordinator, device_info
|
|
|
|
)
|
|
|
|
for scene_id, raw_scene in scene_data.items()
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
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
|
|
|
|
2020-04-29 21:24:57 +00:00
|
|
|
def __init__(self, scene, room_data, coordinator, device_info):
|
2016-03-09 10:15:04 +00:00
|
|
|
"""Initialize the scene."""
|
2020-04-29 21:24:57 +00:00
|
|
|
super().__init__(coordinator, device_info, scene.id)
|
2017-10-23 06:34:50 +00:00
|
|
|
self._scene = scene
|
2020-04-29 21:24:57 +00:00
|
|
|
self._room_name = room_data.get(scene.room_id, {}).get(ROOM_NAME_UNICODE, "")
|
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
|
|
|
|
def device_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()
|