|
| 1 | +""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine |
| 2 | + Copyright 2003 Paul Scott-Murphy, 2014 William McBrine |
| 3 | +
|
| 4 | + This module provides a framework for the use of DNS Service Discovery |
| 5 | + using IP multicast. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 20 | + USA |
| 21 | +""" |
| 22 | + |
| 23 | +import time |
| 24 | +from typing import Optional, Set, Tuple, Union |
| 25 | + |
| 26 | +from ..const import _SERVICE_TYPE_ENUMERATION_NAME |
| 27 | +from ..core import Zeroconf |
| 28 | +from ..services import ServiceBrowser, ServiceListener |
| 29 | +from ..utils.net import IPVersion, InterfaceChoice, InterfacesType |
| 30 | + |
| 31 | + |
| 32 | +class ZeroconfServiceTypes(ServiceListener): |
| 33 | + """ |
| 34 | + Return all of the advertised services on any local networks |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self) -> None: |
| 38 | + """Keep track of found services in a set.""" |
| 39 | + self.found_services: Set[str] = set() |
| 40 | + |
| 41 | + def add_service(self, zc: Zeroconf, type_: str, name: str) -> None: |
| 42 | + """Service added.""" |
| 43 | + self.found_services.add(name) |
| 44 | + |
| 45 | + def update_service(self, zc: Zeroconf, type_: str, name: str) -> None: |
| 46 | + """Service updated.""" |
| 47 | + |
| 48 | + def remove_service(self, zc: Zeroconf, type_: str, name: str) -> None: |
| 49 | + """Service removed.""" |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def find( |
| 53 | + cls, |
| 54 | + zc: Optional[Zeroconf] = None, |
| 55 | + timeout: Union[int, float] = 5, |
| 56 | + interfaces: InterfacesType = InterfaceChoice.All, |
| 57 | + ip_version: Optional[IPVersion] = None, |
| 58 | + ) -> Tuple[str, ...]: |
| 59 | + """ |
| 60 | + Return all of the advertised services on any local networks. |
| 61 | +
|
| 62 | + :param zc: Zeroconf() instance. Pass in if already have an |
| 63 | + instance running or if non-default interfaces are needed |
| 64 | + :param timeout: seconds to wait for any responses |
| 65 | + :param interfaces: interfaces to listen on. |
| 66 | + :param ip_version: IP protocol version to use. |
| 67 | + :return: tuple of service type strings |
| 68 | + """ |
| 69 | + local_zc = zc or Zeroconf(interfaces=interfaces, ip_version=ip_version) |
| 70 | + listener = cls() |
| 71 | + browser = ServiceBrowser(local_zc, _SERVICE_TYPE_ENUMERATION_NAME, listener=listener) |
| 72 | + |
| 73 | + # wait for responses |
| 74 | + time.sleep(timeout) |
| 75 | + |
| 76 | + browser.cancel() |
| 77 | + |
| 78 | + # close down anything we opened |
| 79 | + if zc is None: |
| 80 | + local_zc.close() |
| 81 | + |
| 82 | + return tuple(sorted(listener.found_services)) |
0 commit comments