Exemplo n.º 1
0
    async def test_subscribe(self):
        r = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(r)
        device = await factory.async_create_device('http://localhost:1234/dmr')
        event_handler = UpnpEventHandler('http://localhost:11302', r)

        service = device.service(
            'urn:schemas-upnp-org:service:RenderingControl:1')
        await event_handler.async_subscribe(service)
        assert event_handler.service_for_sid('uuid:dummy') == service
Exemplo n.º 2
0
    async def test_subscribe(self):
        """Test subscribing to a UpnpService."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://localhost:1234/dmr")
        event_handler = UpnpEventHandler("http://localhost:11302", requester)

        service = device.service(
            "urn:schemas-upnp-org:service:RenderingControl:1")
        success, sid = await event_handler.async_subscribe(service)
        assert event_handler.service_for_sid("uuid:dummy") == service
        assert success is True
        assert sid == "uuid:dummy"
Exemplo n.º 3
0
 def __init__(self,
              device: async_upnp_client.UpnpDevice,
              service: async_upnp_client.UpnpService,
              url: str):
     self._service = service
     self._task = None
     self._event_handler = UpnpEventHandler(url, device.requester)
Exemplo n.º 4
0
    async def test_on_notify_upnp_event(self):
        changed_vars: List[UpnpStateVariable] = []

        def on_event(self, changed_state_variables):
            nonlocal changed_vars
            changed_vars = changed_state_variables

        r = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(r)
        device = await factory.async_create_device('http://localhost:1234/dmr')
        service = device.service('urn:schemas-upnp-org:service:RenderingControl:1')
        service.on_event = on_event
        event_handler = UpnpEventHandler('http://localhost:11302', r)
        await event_handler.async_subscribe(service)

        headers = {
            'NT': 'upnp:event',
            'NTS': 'upnp:propchange',
            'SID': 'uuid:dummy',
        }
        body = """
<e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
    <e:property>
        <Volume>60</Volume>
    </e:property>
</e:propertyset>
"""

        result = await event_handler.handle_notify(headers, body)
        assert result == 200

        assert len(changed_vars) == 1

        state_var = service.state_variable('Volume')
        assert state_var.value == 60
Exemplo n.º 5
0
    async def test_action_not_exists(self):
        r = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(r)
        device = await factory.async_create_device('http://localhost:1234/dmr')
        event_handler = UpnpEventHandler('http://localhost:11302', r)
        profile = DmrDevice(device, event_handler=event_handler)

        # doesn't error
        assert profile._action('RC', 'NonExisting') is None
Exemplo n.º 6
0
    async def test_action_not_exists(self):
        """Test getting non-existing action."""
        r = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(r)
        device = await factory.async_create_device("http://localhost:1234/dmr")
        event_handler = UpnpEventHandler("http://localhost:11302", r)
        profile = DmrDevice(device, event_handler=event_handler)

        # doesn't error
        assert profile._action("RC", "NonExisting") is None
Exemplo n.º 7
0
    async def test_subscribe_renew(self):
        """Test renewing an existing subscription to a UpnpService."""
        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://localhost:1234/dmr")
        event_handler = UpnpEventHandler("http://localhost:11302", requester)

        service = device.service(
            "urn:schemas-upnp-org:service:RenderingControl:1")
        success, sid, timeout = await event_handler.async_subscribe(service)
        assert success is True
        assert sid == "uuid:dummy"
        assert event_handler.service_for_sid("uuid:dummy") == service
        assert timeout == timedelta(seconds=300)

        success, sid, timeout = await event_handler.async_resubscribe(service)
        assert event_handler.service_for_sid("uuid:dummy") == service
        assert success is True
        assert sid == "uuid:dummy"
        assert timeout == timedelta(seconds=300)
Exemplo n.º 8
0
    def __init__(self, requester, listen_port, listen_host=None, loop=None):
        """Initializer."""
        self._listen_port = listen_port
        self._listen_host = listen_host or get_local_ip()
        self._loop = loop or asyncio.get_event_loop()

        self._aiohttp_server = None
        self._server = None

        callback_url = "http://{}:{}/notify".format(self._listen_host,
                                                    self._listen_port)
        self.event_handler = UpnpEventHandler(callback_url, requester)
Exemplo n.º 9
0
    async def test_on_notify_dlna_event(self):
        """Test handling an event.."""
        changed_vars: List[UpnpStateVariable] = []

        def on_event(self, changed_state_variables):
            nonlocal changed_vars
            changed_vars += changed_state_variables

            assert changed_state_variables
            if changed_state_variables[0].name == "LastChange":
                last_change = changed_state_variables[0]
                assert last_change.name == "LastChange"

                dlna_handle_notify_last_change(last_change)

        r = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(r)
        device = await factory.async_create_device("http://localhost:1234/dmr")
        service = device.service(
            "urn:schemas-upnp-org:service:RenderingControl:1")
        service.on_event = on_event
        event_handler = UpnpEventHandler("http://localhost:11302", r)
        await event_handler.async_subscribe(service)

        headers = {
            "NT": "upnp:event",
            "NTS": "upnp:propchange",
            "SID": "uuid:dummy",
        }
        body = """
    <e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
        <e:property>
            <LastChange>
                &lt;Event xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/RCS/&quot;&gt;
                    &lt;InstanceID val=&quot;0&quot;&gt;
                        &lt;Mute channel=&quot;Master&quot; val=&quot;0&quot;/&gt;
                        &lt;Volume channel=&quot;Master&quot; val=&quot;50&quot;/&gt;
                        &lt;/InstanceID&gt;
                &lt;/Event&gt;
            </LastChange>
        </e:property>
    </e:propertyset>
    """

        result = await event_handler.handle_notify(headers, body)
        assert result == 200

        assert len(changed_vars) == 3

        state_var = service.state_variable("Volume")
        assert state_var.value == 50
Exemplo n.º 10
0
    async def test_on_notify_dlna_event(self):
        changed_vars = []

        def on_event(self, changed_state_variables):
            nonlocal changed_vars
            changed_vars += changed_state_variables

            assert changed_state_variables
            if changed_state_variables[0].name == 'LastChange':
                last_change = changed_state_variables[0]
                assert last_change.name == 'LastChange'

                dlna_handle_notify_last_change(last_change)

        r = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(r)
        device = await factory.async_create_device('http://localhost:1234/dmr')
        service = device.service(
            'urn:schemas-upnp-org:service:RenderingControl:1')
        service.on_event = on_event
        event_handler = UpnpEventHandler('http://localhost:11302', r)
        await event_handler.async_subscribe(service)

        headers = {
            'NT': 'upnp:event',
            'NTS': 'upnp:propchange',
            'SID': 'uuid:dummy',
        }
        body = """
<e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
    <e:property>
        <LastChange>
            &lt;Event xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/RCS/&quot;&gt;
                &lt;InstanceID val=&quot;0&quot;&gt;
                    &lt;Mute channel=&quot;Master&quot; val=&quot;0&quot;/&gt;
                    &lt;Volume channel=&quot;Master&quot; val=&quot;50&quot;/&gt;
                    &lt;/InstanceID&gt;
            &lt;/Event&gt;
        </LastChange>
    </e:property>
</e:propertyset>
"""

        result = await event_handler.handle_notify(headers, body)
        assert result == 200

        assert len(changed_vars) == 3

        state_var = service.state_variable('Volume')
        assert state_var.value == 50
Exemplo n.º 11
0
 def __init__(
     self,
     hass: HomeAssistant,
 ) -> None:
     """Initialize global data."""
     self.hass = hass
     self.lock = asyncio.Lock()
     session = aiohttp_client.async_get_clientsession(hass,
                                                      verify_ssl=False)
     self.requester = AiohttpSessionRequester(session, with_sleep=True)
     self.upnp_factory = UpnpFactory(self.requester, non_strict=True)
     # NOTE: event_handler is not actually used, and is only created to
     # satisfy the DmsDevice.__init__ signature
     self.event_handler = UpnpEventHandler("", self.requester)
     self.devices = {}
     self.sources = {}
Exemplo n.º 12
0
    def __init__(self,
                 requester: UpnpRequester,
                 listen_port: int,
                 listen_host: Optional[str] = None,
                 callback_url: Optional[str] = None,
                 loop: Optional[AbstractEventLoop] = None) -> None:
        """Initialize."""
        # pylint: disable=too-many-arguments
        self._listen_port = listen_port
        self._listen_host = listen_host or get_local_ip()
        self._callback_url = callback_url or \
            "http://{}:{}/notify".format(
                self._listen_host, self._listen_port)
        self._loop = loop or asyncio.get_event_loop()

        self._aiohttp_server: Optional[aiohttp.web.Server] = None
        self._server: Optional[AbstractServer] = None

        self.event_handler = UpnpEventHandler(self._callback_url, requester)
Exemplo n.º 13
0
    async def test_on_notify_upnp_event(self):
        """Test handling of a UPnP event."""
        changed_vars: List[UpnpStateVariable] = []

        def on_event(self, changed_state_variables):
            # pylint: disable=unused-argument
            nonlocal changed_vars
            changed_vars = changed_state_variables

        requester = UpnpTestRequester(RESPONSE_MAP)
        factory = UpnpFactory(requester)
        device = await factory.async_create_device("http://localhost:1234/dmr")
        service = device.service(
            "urn:schemas-upnp-org:service:RenderingControl:1")
        service.on_event = on_event
        event_handler = UpnpEventHandler("http://localhost:11302", requester)
        await event_handler.async_subscribe(service)

        headers = {
            "NT": "upnp:event",
            "NTS": "upnp:propchange",
            "SID": "uuid:dummy",
        }
        body = """
<e:propertyset xmlns:e="urn:schemas-upnp-org:event-1-0">
    <e:property>
        <Volume>60</Volume>
    </e:property>
</e:propertyset>
"""

        result = await event_handler.handle_notify(headers, body)
        assert result == 200

        assert len(changed_vars) == 1

        state_var = service.state_variable("Volume")
        assert state_var.value == 60