def __init__(self, hub_ip, _loop, session): self._connection_data = { ATTR_HUB_IP: hub_ip, ATTR_LOOP: _loop, ATTR_WEB_SESSION: session } self._rooms = Rooms(hub_ip, _loop, session) self._shades = Shades(hub_ip, _loop, session) self._scenes = Scenes(hub_ip, _loop, session) self._scene_members = SceneMembers(hub_ip, _loop, session)
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.scenes import Scenes from aiopvapi.rooms import Rooms from aiopvapi.resources.scene import Scene as PvScene hub_address = config.get(HUB_ADDRESS) websession = async_get_clientsession(hass) _scenes = yield from Scenes(hub_address, hass.loop, websession).get_resources() _rooms = yield from Rooms(hub_address, hass.loop, websession).get_resources() if not _scenes or not _rooms: _LOGGER.error("Unable to initialize PowerView hub: %s", hub_address) return pvscenes = (PowerViewScene( hass, PvScene(_raw_scene, hub_address, hass.loop, websession), _rooms) for _raw_scene in _scenes[SCENE_DATA]) async_add_entities(pvscenes)
def __init__(self, hub_ip, loop_, session): self.request = AioRequest(hub_ip, loop=loop_, websession=session) self._scenes_entry_point = Scenes(self.request) self._rooms_entry_point = Rooms(self.request) self._shades_entry_point = Shades(self.request) self._scene_members_entry_point = SceneMembers(self.request) self.scenes = [] # A list of scene instances self.shades = [] # A list of shade instances self.rooms = [] # A list of room instances
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Hunter Douglas PowerView from a config entry.""" config = entry.data hub_address = config[CONF_HOST] websession = async_get_clientsession(hass) pv_request = AioRequest(hub_address, loop=hass.loop, websession=websession) try: async with async_timeout.timeout(10): device_info = await async_get_device_info(pv_request, hub_address) async with async_timeout.timeout(10): rooms = Rooms(pv_request) room_data = async_map_data_by_id( (await rooms.get_resources())[ROOM_DATA]) async with async_timeout.timeout(10): scenes = Scenes(pv_request) scene_data = async_map_data_by_id( (await scenes.get_resources())[SCENE_DATA]) async with async_timeout.timeout(10): shades = Shades(pv_request) shade_entries = await shades.get_resources() shade_data = async_map_data_by_id(shade_entries[SHADE_DATA]) except HUB_EXCEPTIONS as err: raise ConfigEntryNotReady( f"Connection error to PowerView hub: {hub_address}: {err}" ) from err if not device_info: raise ConfigEntryNotReady( f"Unable to initialize PowerView hub: {hub_address}") coordinator = PowerviewShadeUpdateCoordinator(hass, shades, hub_address) coordinator.async_set_updated_data(PowerviewShadeData()) # populate raw shade data into the coordinator for diagnostics coordinator.data.store_group_data(shade_entries[SHADE_DATA]) hass.data.setdefault(DOMAIN, {})[entry.entry_id] = PowerviewEntryData( api=pv_request, room_data=room_data, scene_data=scene_data, shade_data=shade_data, coordinator=coordinator, device_info=device_info, ) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Hunter Douglas PowerView from a config entry.""" config = entry.data hub_address = config[CONF_HOST] websession = async_get_clientsession(hass) pv_request = AioRequest(hub_address, loop=hass.loop, websession=websession) try: async with async_timeout.timeout(10): device_info = await async_get_device_info(pv_request) device_info[PV_HUB_ADDRESS] = hub_address async with async_timeout.timeout(10): rooms = Rooms(pv_request) room_data = async_map_data_by_id( (await rooms.get_resources())[ROOM_DATA]) async with async_timeout.timeout(10): scenes = Scenes(pv_request) scene_data = async_map_data_by_id( (await scenes.get_resources())[SCENE_DATA]) async with async_timeout.timeout(10): shades = Shades(pv_request) shade_data = async_map_data_by_id( (await shades.get_resources())[SHADE_DATA]) except HUB_EXCEPTIONS as err: raise ConfigEntryNotReady( f"Connection error to PowerView hub: {hub_address}: {err}" ) from err if not device_info: raise ConfigEntryNotReady( f"Unable to initialize PowerView hub: {hub_address}") coordinator = PowerviewShadeUpdateCoordinator(hass, shades, hub_address) coordinator.async_set_updated_data(PowerviewShadeData()) hass.data.setdefault(DOMAIN, {})[entry.entry_id] = { PV_API: pv_request, PV_ROOM_DATA: room_data, PV_SCENE_DATA: scene_data, PV_SHADES: shades, PV_SHADE_DATA: shade_data, COORDINATOR: coordinator, DEVICE_INFO: device_info, } hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Hunter Douglas PowerView from a config entry.""" config = entry.data hub_address = config.get(CONF_HOST) websession = async_get_clientsession(hass) pv_request = AioRequest(hub_address, loop=hass.loop, websession=websession) try: async with async_timeout.timeout(10): device_info = await async_get_device_info(pv_request) async with async_timeout.timeout(10): rooms = Rooms(pv_request) room_data = _async_map_data_by_id( (await rooms.get_resources())[ROOM_DATA]) async with async_timeout.timeout(10): scenes = Scenes(pv_request) scene_data = _async_map_data_by_id( (await scenes.get_resources())[SCENE_DATA]) async with async_timeout.timeout(10): shades = Shades(pv_request) shade_data = _async_map_data_by_id( (await shades.get_resources())[SHADE_DATA]) except HUB_EXCEPTIONS as err: _LOGGER.error("Connection error to PowerView hub: %s", hub_address) raise ConfigEntryNotReady from err if not device_info: _LOGGER.error("Unable to initialize PowerView hub: %s", hub_address) raise ConfigEntryNotReady async def async_update_data(): """Fetch data from shade endpoint.""" async with async_timeout.timeout(10): shade_entries = await shades.get_resources() if not shade_entries: raise UpdateFailed("Failed to fetch new shade data.") return _async_map_data_by_id(shade_entries[SHADE_DATA]) coordinator = DataUpdateCoordinator( hass, _LOGGER, name="powerview hub", update_method=async_update_data, update_interval=timedelta(seconds=60), ) hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = { PV_API: pv_request, PV_ROOM_DATA: room_data, PV_SCENE_DATA: scene_data, PV_SHADES: shades, PV_SHADE_DATA: shade_data, COORDINATOR: coordinator, DEVICE_INFO: device_info, } hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True
async def go(): await self.start_fake_server() scenes = Scenes(self.request) response = await scenes.get_instance(43436) return response
async def go(): await self.start_fake_server() scenes = Scenes(self.request) res = await scenes.create_scene( 12372, 'New scene', color_id=1, icon_id=2) return res
async def go(): await self.start_fake_server() scenes = Scenes(self.request) res = await scenes.get_resources() return res
def setup(self, ip_address): loop = asyncio.get_event_loop() session = aiohttp.ClientSession(loop=loop) self._shades = Shades(ip_address, loop, session) self._rooms = Rooms(ip_address, loop, session) self._scenes = Scenes(ip_address, loop, session)
class PowerViewCommands: def __init__(self, hub_ip, _loop, session): self._connection_data = { ATTR_HUB_IP: hub_ip, ATTR_LOOP: _loop, ATTR_WEB_SESSION: session } self._rooms = Rooms(hub_ip, _loop, session) self._shades = Shades(hub_ip, _loop, session) self._scenes = Scenes(hub_ip, _loop, session) self._scene_members = SceneMembers(hub_ip, _loop, session) @asyncio.coroutine def create_scene(self, scenename, roomId): result = None _newscene = yield from self._scenes.create_scene(roomId, scenename) if _newscene: result = Scene(_newscene, **self._connection_data) return result @asyncio.coroutine def get_scenes(self): _scenes = yield from self._scenes.get_resources() return _scenes @asyncio.coroutine def activate_scene(self, scene_id): result = None _scene = yield from self.get_scene(scene_id) if _scene: result = yield from _scene.activate() return result @asyncio.coroutine def delete_scene(self, scene_id): result = None _scene = yield from self.get_scene(scene_id) if _scene: result = yield from _scene.delete() return result @asyncio.coroutine def create_room(self, roomname): _newroom = yield from self._rooms.create_room(roomname) if _newroom: return Room(_newroom, **self._connection_data) return None @asyncio.coroutine def get_rooms(self): _rooms = yield from self._rooms.get_resources() return _rooms @asyncio.coroutine def delete_room(self, room_id): _result = None _room = yield from self.get_room(room_id) if _room: _result = yield from _room.delete() return _result @asyncio.coroutine def create_scene_member(self, scene_id, shade_id, shade_position): _scene_member = yield from self._scene_members.create_scene_member( shade_position, scene_id, shade_id) return _scene_member @asyncio.coroutine def create_room_scene_scene_member_move(self, room_name=None, scene_name=None, shade_id=None, position1=None, position2=None): """Creates a room, scene and adds a shade to the scene using a position object.""" _result = None _room = yield from self.create_room(room_name) yield from asyncio.sleep(3) _shade = yield from self.get_shade(shade_id) if _room and _shade: _scene = yield from self.create_scene(scene_name, _room.id) yield from asyncio.sleep(3) if _scene: yield from asyncio.sleep(3) _position = _shade.get_move_data(position1, position2) _result = yield from self.create_scene_member( _scene.id, shade_id, _position) if _result: return _scene return _result @asyncio.coroutine def open_shade(self, shadeId): _result = None shade = yield from self.get_shade(shadeId) if shade: _result = yield from shade.open() return _result @asyncio.coroutine def move_shade(self, shadeId, position1=None, position2=None): result = None shade = yield from self.get_shade(shadeId) if shade: result = yield from shade.move_to(position1=position1, position2=position2) return result @asyncio.coroutine def close_shade(self, shadeId): result = None shade = yield from self.get_shade(shadeId) if shade: result = yield from shade.close() return result @asyncio.coroutine def get_room(self, roomId): new_room = None _rooms = yield from self._rooms.get_resources() if _rooms: for _room in _rooms[ATTR_ROOM_DATA]: if _room[ATTR_ID] == roomId: new_room = Room(_room, **self._connection_data) break return new_room @asyncio.coroutine def get_scene(self, sceneId): new_scene = None _scenes = yield from self._scenes.get_resources() if _scenes: for _scene in _scenes[ATTR_SCENE_DATA]: if _scene[ATTR_ID] == sceneId: new_scene = Scene(_scene, **self._connection_data) break return new_scene @asyncio.coroutine def get_shade(self, shadeId): new_shade = None _shades = yield from self._shades.get_resources() if _shades: for _shade in _shades[ATTR_SHADE_DATA]: if _shade[ATTR_ID] == shadeId: new_shade = Shade(_shade, **self._connection_data) break return new_shade