File: axis.py

package info (click to toggle)
python-netdisco 2.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 508 kB
  • sloc: python: 1,532; xml: 247; sh: 10; makefile: 8
file content (41 lines) | stat: -rw-r--r-- 1,230 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Discover Axis devices."""
from . import MDNSDiscoverable

from ..const import (
    ATTR_HOST, ATTR_PORT, ATTR_HOSTNAME, ATTR_PROPERTIES)


class Discoverable(MDNSDiscoverable):
    """Add support for discovering Axis devices."""

    def info_from_entry(self, entry):
        """Return most important info from mDNS entries."""
        properties = {}

        for key, value in entry.properties.items():
            if isinstance(value, bytes):
                value = value.decode('utf-8')
            properties[key.decode('utf-8')] = value

        return {
            ATTR_HOST: self.ip_from_host(entry.server),
            ATTR_PORT: entry.port,
            ATTR_HOSTNAME: entry.server,
            ATTR_PROPERTIES: properties,
        }

    def __init__(self, nd):
        """Initialize the Axis discovery."""
        super(Discoverable, self).__init__(nd, '_axis-video._tcp.local.')

    def ip_from_host(self, host):
        """Attempt to return the ip address from an mDNS host.

        Return host if failed.
        """
        ips = self.netdis.mdns.zeroconf.cache.entries_with_name(host.lower())

        try:
            return repr(ips[0]) if ips else host
        except TypeError:
            return host