async def start_fake_server(self):
     info = await self.server.start()
     resolver = FakeResolver(info, loop=self.loop)
     connector = aiohttp.TCPConnector(loop=self.loop, resolver=resolver)
     _session = aiohttp.ClientSession(connector=connector, loop=self.loop)
     self.request = AioRequest(FAKE_BASE_URL, loop=self.loop,
                               websession=_session, timeout=1)
Exemple #2
0
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

    hub_address = config.get(HUB_ADDRESS)
    websession = async_get_clientsession(hass)

    pv_request = AioRequest(hub_address, loop=hass.loop, websession=websession)

    _scenes = await Scenes(pv_request).get_resources()
    _rooms = await Rooms(pv_request).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, pv_request), _rooms)
                for _raw_scene in _scenes[SCENE_DATA])
    async_add_entities(pvscenes)
Exemple #3
0
async def get_user_data(hub_ip):
    request = AioRequest(hub_ip)
    hub = Hub(request)
    await hub.query_user_data()

    print("UserData")
    print("hub name: {}".format(hub.user_data.hub_name))
    pprint(hub.user_data._raw)
 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
Exemple #5
0
async def get_firmware(hub_ip):
    request = AioRequest(hub_ip)
    hub = Hub(request)
    await hub.query_firmware()

    print("MAIN PROCESSOR")
    print(hub.main_processor_version)

    print("RADIO")
    print(hub.radio_version)
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
    def test_remove_shade_from_scene(self):
        """Tests create new scene."""
        loop = asyncio.get_event_loop()
        request = AioRequest(self.fake_ip, loop)

        _del_mock = AsyncMock(return_value=None)

        request.websession.delete = _del_mock

        async def go():
            scene_members = SceneMembers(request)
            await scene_members.delete_shade_from_scene(1234, 5678)

        resp = loop.run_until_complete(go())
        _del_mock.mock.assert_called_once_with(
            'http://{}/api/scenemembers'.format(self.fake_ip),
            params={
                "sceneId": 5678,
                'shadeId': 1234
            })
Exemple #9
0
async def async_setup(hass, config):
    """Set up a PowerView hub."""

    from aiopvapi.shades import Shades
    from aiopvapi.helpers.aiorequest import AioRequest, PvApiError

    conf = config.get(DOMAIN)

    ip = conf[CONF_IP_ADDRESS]
    websession = async_get_clientsession(hass)

    hub_request = AioRequest(ip, hass.loop, websession)

    shades_entry_point = Shades(hub_request)

    async def scan_for_shades(event_data=None):
        _tries = 3
        while _tries > 0:
            try:
                _LOGGER.debug("getting shades.")
                shades = await get_shades(shades_entry_point)
                break
            except PvApiError as err:
                _LOGGER.error(err)
                await asyncio.sleep(5)
        else:
            _LOGGER.error("Failed to setup PowerView shades.")
            return False

        _LOGGER.debug("recieved shades. %s", shades)
        hass.data[DOMAIN] = {"shades": shades}

        for component in POWERVIEW_COMPONENTS:
            await discovery.async_load_platform(hass, component, DOMAIN, {},
                                                config)

    await scan_for_shades()

    hass.services.async_register(DOMAIN, "scan", scan_for_shades)

    return True
Exemple #10
0
async def validate_input(opp: core.OpenPeerPower,
                         hub_address: str) -> dict[str, str]:
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """

    websession = async_get_clientsession(opp)

    pv_request = AioRequest(hub_address, loop=opp.loop, websession=websession)

    try:
        async with async_timeout.timeout(10):
            device_info = await async_get_device_info(pv_request)
    except HUB_EXCEPTIONS as err:
        raise CannotConnect from err

    # Return info that you want to store in the config entry.
    return {
        "title": device_info[DEVICE_NAME],
        "unique_id": device_info[DEVICE_SERIAL_NUMBER],
    }
Exemple #11
0
async def validate_input(hass: core.HomeAssistant,
                         hub_address: str) -> dict[str, str]:
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """

    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)
    except HUB_EXCEPTIONS as err:
        raise CannotConnect from err

    # Return info that you want to store in the config entry.
    return {
        "title": device_info.name,
        "unique_id": device_info.serial_number,
    }
Exemple #12
0
async def validate_input(hass: core.HomeAssistant, data):
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """

    hub_address = data[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)
    except HUB_EXCEPTIONS:
        raise CannotConnect
    if not device_info:
        raise CannotConnect

    # Return info that you want to store in the config entry.
    return {
        "title": device_info[DEVICE_NAME],
        "unique_id": device_info[DEVICE_SERIAL_NUMBER],
    }
Exemple #13
0
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
Exemple #14
0
def fake_aiorequest():
    request = AioRequest('127.0.0.1', websession=MagicMock())
    request.get = AsyncMock()
    request.put = AsyncMock()
    return request
Exemple #15
0
 def __init__(self, loop, websession, base_path):
     if websession is None:
         _LOGGER.debug("No session defined. Creating a new one")
         websession = aiohttp.ClientSession(loop=loop)
     self.request = AioRequest(loop, websession)
     self._base_path = base_path
Exemple #16
0
 def __init__(self, hub_ip):
     self.request = AioRequest(hub_ip)
     self.shades = []
     self._shades_entry_point = Shades(self.request)
 def __init__(self, hub_ip):
     self.request = AioRequest(hub_ip)
     self.raw_scene_members = []
     self.scene_members=[]
     self._scene_members_entry_point = SceneMembers(self.request)
Exemple #18
0
 def __init__(self, hub_ip, loop, websession=None):
     ApiBase.__init__(self, loop, websession)
     self.ip_address = hub_ip
     self.request = AioRequest(loop, websession)
     self._base_path = get_base_path(hub_ip, 'api')
Exemple #19
0
 def __init__(self, hub_ip):
     self.request = AioRequest(hub_ip)
     self.raw_rooms = []
     self.rooms = []
     self._shades_entry_point = Rooms(self.request)