示例#1
0
文件: test.py 项目: gavc1/pizone
    async def test_async(self):
        self.loop = asyncio.get_event_loop()
        self.condition = Condition(loop=self.loop)

        async with discovery(self):
            async with self.condition:
                await self.condition.wait()

            self.dump_data()

            # test setting values
            await self.ctrl.set_mode(Controller.Mode.AUTO)

            Controller.CONNECT_RETRY_TIMEOUT = 2

            self.ctrl._ip = 'bababa'  # pylint: disable=protected-access
            try:
                await self.ctrl.set_sleep_timer(30)
                assert False, "Should be unreachable code"
            except ConnectionError:
                pass

            # Should reconnect here
            async with self.condition:
                await self.condition.wait()

            await self.ctrl.set_mode(Controller.Mode.HEAT)

            self.dump_data()
示例#2
0
async def test_power():

    listener = ListenerTesting()

    async with discovery(listener):
        ctrl = await listener.await_controller()

        result = ctrl.power

        dump_data(ctrl)
示例#3
0
async def test_rescan(send):
    async with discovery() as service:
        assert not service.is_closed
        assert send.call_count == 1

        await service.rescan()
        await sleep(0)
        assert send.call_count == 2

    assert service.is_closed
示例#4
0
async def test_reconnect():
    listener = ListenerTesting()

    async with discovery(listener):
        ctrl = await listener.await_controller()

        assert listener.connect_count == 1

        # test automatic reconnection
        Controller.CONNECT_RETRY_TIMEOUT = 2

        ctrl._ip = "bababa"  # pylint: disable=protected-access
        with raises(ConnectionError):
            await ctrl.set_sleep_timer(30)

        # Should reconnect here
        await listener.await_controller()

        assert listener.connect_count == 2

        await ctrl.set_sleep_timer(0)
示例#5
0
async def test_full_stack(loop):
    controllers = []
    event = Event()

    class TestListener(Listener):
        def controller_discovered(self, _ctrl):
            controllers.append(_ctrl)
            event.set()

        def controller_reconnected(self, ctrl):
            event.set()
    listener = TestListener()

    async with discovery(listener):
        await event.wait()
        event.clear()

        ctrl = controllers[0]

        dump_data(ctrl)

        # test setting values
        await ctrl.set_mode(Controller.Mode.AUTO)

        Controller.CONNECT_RETRY_TIMEOUT = 2

        ctrl._ip = 'bababa'  # pylint: disable=protected-access
        with raises(ConnectionError):
            await ctrl.set_sleep_timer(30)

        # Should reconnect here
        await event.wait()
        event.clear()

        await ctrl.set_mode(Controller.Mode.HEAT)

        dump_data(ctrl)
示例#6
0
async def async_start_discovery_service(hass: HomeAssistant):
    """Set up the pizone internal discovery."""
    disco = hass.data.get(DATA_DISCOVERY_SERVICE)
    if disco:
        # Already started
        return disco

    # discovery local services
    disco = DiscoveryService(hass)
    hass.data[DATA_DISCOVERY_SERVICE] = disco

    # Start the pizone discovery service, disco is the listener
    session = aiohttp_client.async_get_clientsession(hass)
    loop = hass.loop

    disco.pi_disco = pizone.discovery(disco, loop=loop, session=session)
    await disco.pi_disco.start_discovery()

    async def shutdown_event(event):
        await async_stop_discovery_service(hass)

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_event)

    return disco
示例#7
0
async def async_start_discovery_service(opp: OpenPeerPower):
    """Set up the pizone internal discovery."""
    disco = opp.data.get(DATA_DISCOVERY_SERVICE)
    if disco:
        # Already started
        return disco

    # discovery local services
    disco = DiscoveryService(opp)
    opp.data[DATA_DISCOVERY_SERVICE] = disco

    # Start the pizone discovery service, disco is the listener
    session = aiohttp_client.async_get_clientsession(opp)
    loop = opp.loop

    disco.pi_disco = pizone.discovery(disco, loop=loop, session=session)
    await disco.pi_disco.start_discovery()

    async def shutdown_event(event):
        await async_stop_discovery_service(opp)

    opp.bus.async_listen_once(EVENT_OPENPEERPOWER_STOP, shutdown_event)

    return disco
示例#8
0
async def test_full_stack():
    listener = ListenerTesting()

    async with discovery(listener):
        ctrl = await listener.await_controller()

        dump_data(ctrl)

        old_mode = ctrl.mode
        old_airflow_min = ctrl.zones[1].airflow_min
        old_airflow_max = ctrl.zones[1].airflow_max

        try:
            # test setting values
            mode = (Controller.Mode.COOL if old_mode == Controller.Mode.AUTO
                    else Controller.Mode.AUTO)
            await ctrl.set_mode(mode)
            assert ctrl.mode == mode

            # test set airflow min
            nmin = 20 if old_airflow_min == 10 else 10
            await ctrl.zones[1].set_airflow_min(nmin)

            with raises(AttributeError):
                await ctrl.zones[1].set_airflow_min(41)

            with raises(AttributeError):
                await ctrl.zones[1].set_airflow_min(-1)

            with raises(AttributeError):
                await ctrl.zones[1].set_airflow_min(105)

            assert ctrl.zones[1].airflow_min == nmin

            # test set airflow max
            nmax = 80 if old_airflow_max == 90 else 90
            await ctrl.zones[1].set_airflow_max(nmax)

            with raises(AttributeError):
                await ctrl.zones[1].set_airflow_max(41)

            with raises(AttributeError):
                await ctrl.zones[1].set_airflow_max(-1)

            with raises(AttributeError):
                await ctrl.zones[1].set_airflow_max(105)

            assert ctrl.zones[1].airflow_max == nmax

            # Wait for a re-read from the server
            old_count = listener.update_count
            await listener.await_update()
            assert listener.update_count > old_count

            assert ctrl.mode == mode
            assert ctrl.zones[1].airflow_min == nmin
            assert ctrl.zones[1].airflow_max == nmax

        finally:
            # Tidy everything up
            await ctrl.set_mode(old_mode)
            await ctrl.zones[1].set_airflow_min(old_airflow_min)
            await ctrl.zones[1].set_airflow_max(old_airflow_max)

        dump_data(ctrl)
async def async_start_discovery_service(hass: HomeAssistant):
    """Set up the pizone internal discovery."""
    if disco := hass.data.get(DATA_DISCOVERY_SERVICE):
        # Already started
        return disco

    # discovery local services
    disco = DiscoveryService(hass)
    hass.data[DATA_DISCOVERY_SERVICE] = disco

    # Start the pizone discovery service, disco is the listener
    session = aiohttp_client.async_get_clientsession(hass)
    loop = hass.loop

    disco.pi_disco = pizone.discovery(disco, loop=loop, session=session)
    await disco.pi_disco.start_discovery()

    async def shutdown_event(event):
        await async_stop_discovery_service(hass)

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_event)

    return disco


async def async_stop_discovery_service(hass: HomeAssistant):
    """Stop the discovery service."""
    if not (disco := hass.data.get(DATA_DISCOVERY_SERVICE)):
        return
示例#10
0
async def async_start_discovery_service(hass: HomeAssistantType):
    """Set up the pychromecast internal discovery."""
    import pizone

    class DiscoveryService(pizone.Listener):
        """Discovery data and interfacing with pizone library."""
        def __init__(self):
            self.controllers: Dict[str, pizone.Controller] = {}
            self.controller_ready = Event()
            self.components: Dict[pizone.Controller, 'IZoneController'] = {}

            self.pi_disco = None
            self.stop_listener = None

        # Listener interface
        def controller_disconnected(self, ctrl: pizone.Controller,
                                    ex: Exception) -> None:
            """Disconnected from contrller."""
            component = self.components.get(ctrl)
            if not component:
                return
            component.set_available(False)

        def controller_reconnected(self, ctrl: pizone.Controller) -> None:
            """Reconnected to controller."""
            component = self.components.get(ctrl)
            if not component:
                return
            component.set_available(True)

        async def _controller_discovered(self, ctrl: pizone.Controller):
            _LOGGER.debug("Controller discovered uid=%s", ctrl.device_uid)

            conf: ConfigType = hass.data.get(DATA_CONFIG)

            # Filter out any entities excluded in the config file
            if conf and ctrl.device_uid in conf[CONF_EXCLUDE]:
                return

            self.controllers[ctrl.device_uid] = ctrl
            self.controller_ready.set()

            # This will be present if the component is configured.
            # otherwise init_controller will be called when the config entry
            # is created.
            async_add_entries = hass.data.get(DATA_ADD_ENTRIES)
            if async_add_entries:
                self.init_controller(ctrl, async_add_entries)

        def init_controller(self, controller: pizone.Controller,
                            async_add_entries):
            """Register the controller device and the containing zones."""
            device = ControllerDevice(controller, async_add_entries)
            self.components[controller] = device

        def controller_discovered(self, ctrl: pizone.Controller) -> None:
            assert ctrl.device_uid not in self.controllers, \
                "discovered device that already exists"

            hass.async_create_task(self._controller_discovered(ctrl))

        def controller_update(self, ctrl: pizone.Controller) -> None:
            component = self.components.get(ctrl)
            if not component:
                return

            hass.async_add_job(component.async_update_ha_state)

        def zone_update(self, ctrl: pizone.Controller,
                        zone: pizone.Zone) -> None:
            if zone.type == pizone.Zone.Type.CONST:
                return

            component = self.components.get(ctrl)
            if not component:
                return

            zone_component = component.zones[zone]
            hass.async_add_job(zone_component.async_update_ha_state)

    disco = hass.data.get(DATA_DISCOVERY_SERVICE)
    if disco:
        # Already started
        return disco

    # discovery local services
    disco = DiscoveryService()
    hass.data[DATA_DISCOVERY_SERVICE] = disco

    # Start the pizone discovery service, disco is the listener
    session = aiohttp_client.async_get_clientsession(hass)
    loop = hass.loop

    disco.pi_disco = pizone.discovery(disco, loop=loop, session=session)
    await disco.pi_disco.start_discovery()

    async def shutdown_event(event):
        await disco.pi_disco.close()
        del hass.data[DATA_DISCOVERY_SERVICE]

    disco.stop_listener = hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP,
                                                     shutdown_event)

    return disco
示例#11
0
async def test_broadcast(broadcasts):
    broadcasts.return_value = []

    async with discovery():
        assert broadcasts.called
示例#12
0
async def test_messages_sent(send_broadcasts):
    async with discovery():
        assert send_broadcasts.called
示例#13
0
文件: discovery.py 项目: jbouwh/core
        async_dispatcher_send(self.hass, DISPATCH_ZONE_UPDATE, ctrl, zone)


async def async_start_discovery_service(hass: HomeAssistant):
    """Set up the pizone internal discovery."""
    if disco := hass.data.get(DATA_DISCOVERY_SERVICE):
        # Already started
        return disco

    # discovery local services
    disco = DiscoveryService(hass)
    hass.data[DATA_DISCOVERY_SERVICE] = disco

    # Start the pizone discovery service, disco is the listener
    session = aiohttp_client.async_get_clientsession(hass)
    disco.pi_disco = pizone.discovery(disco, session=session)
    await disco.pi_disco.start_discovery()

    async def shutdown_event(event):
        await async_stop_discovery_service(hass)

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown_event)

    return disco


async def async_stop_discovery_service(hass: HomeAssistant):
    """Stop the discovery service."""
    if not (disco := hass.data.get(DATA_DISCOVERY_SERVICE)):
        return