hass-core/homeassistant/components/hunterdouglas_powerview/scene.py

104 lines
3 KiB
Python
Raw Normal View History

"""Support for Powerview scenes from a Powerview hub."""
2016-03-06 15:42:11 +01:00
import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.scene import Scene, DOMAIN
from homeassistant.const import CONF_PLATFORM
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import async_generate_entity_id
2016-03-06 15:42:11 +01:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 12:25:30 -07:00
ENTITY_ID_FORMAT = DOMAIN + ".{}"
HUB_ADDRESS = "address"
2016-03-06 15:42:11 +01:00
2019-07-31 12:25:30 -07:00
PLATFORM_SCHEMA = vol.Schema(
{
vol.Required(CONF_PLATFORM): "hunterdouglas_powerview",
vol.Required(HUB_ADDRESS): cv.string,
}
)
2019-07-31 12:25:30 -07:00
SCENE_DATA = "sceneData"
ROOM_DATA = "roomData"
SCENE_NAME = "name"
ROOM_NAME = "name"
SCENE_ID = "id"
ROOM_ID = "id"
ROOM_ID_IN_SCENE = "roomId"
STATE_ATTRIBUTE_ROOM_NAME = "roomName"
2016-03-06 15:42:11 +01:00
2019-07-31 12:25:30 -07:00
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up home assistant scene entries."""
# from aiopvapi.hub import Hub
from aiopvapi.helpers.aiorequest import AioRequest
from aiopvapi.scenes import Scenes
from aiopvapi.rooms import Rooms
from aiopvapi.resources.scene import Scene as PvScene
2016-03-06 15:42:11 +01:00
hub_address = config.get(HUB_ADDRESS)
websession = async_get_clientsession(hass)
2016-03-06 15:42:11 +01:00
pv_request = AioRequest(hub_address, loop=hass.loop, websession=websession)
_scenes = await Scenes(pv_request).get_resources()
_rooms = await Rooms(pv_request).get_resources()
2016-03-06 15:42:11 +01:00
if not _scenes or not _rooms:
2019-07-31 12:25:30 -07:00
_LOGGER.error("Unable to initialize PowerView hub: %s", hub_address)
return
2019-07-31 12:25:30 -07:00
pvscenes = (
PowerViewScene(hass, PvScene(_raw_scene, pv_request), _rooms)
for _raw_scene in _scenes[SCENE_DATA]
)
async_add_entities(pvscenes)
2016-03-06 15:42:11 +01:00
class PowerViewScene(Scene):
2016-03-09 11:15:04 +01:00
"""Representation of a Powerview scene."""
2016-03-06 15:42:11 +01:00
def __init__(self, hass, scene, room_data):
2016-03-09 11:15:04 +01:00
"""Initialize the scene."""
self._scene = scene
2016-03-06 15:42:11 +01:00
self.hass = hass
self._room_name = None
self._sync_room_data(room_data)
self.entity_id = async_generate_entity_id(
2019-07-31 12:25:30 -07:00
ENTITY_ID_FORMAT, str(self._scene.id), hass=hass
)
2016-03-06 15:42:11 +01:00
def _sync_room_data(self, room_data):
"""Sync room data."""
2019-07-31 12:25:30 -07:00
room = next(
(
room
for room in room_data[ROOM_DATA]
if room[ROOM_ID] == self._scene.room_id
),
{},
)
2019-07-31 12:25:30 -07:00
self._room_name = room.get(ROOM_NAME, "")
2016-03-06 15:42:11 +01:00
@property
def name(self):
2016-03-09 11:15:04 +01:00
"""Return the name of the scene."""
return self._scene.name
2016-03-06 15:42:11 +01:00
@property
def device_state_attributes(self):
2016-03-09 11:15:04 +01:00
"""Return the state attributes."""
return {STATE_ATTRIBUTE_ROOM_NAME: self._room_name}
2016-03-06 15:42:11 +01:00
@property
def icon(self):
"""Icon to use in the frontend."""
2019-07-31 12:25:30 -07:00
return "mdi:blinds"
async def async_activate(self):
"""Activate scene. Try to get entities into requested state."""
await self._scene.activate()