Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split ServiceInfo class in two classes #790

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions pychromecast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
DISCOVER_TIMEOUT,
CastBrowser,
CastListener, # Deprecated
ServiceInfo,
SimpleCastListener,
discover_chromecasts,
start_discovery,
stop_discovery,
)
from .dial import get_cast_type
from .const import CAST_TYPE_CHROMECAST, REQUEST_TIMEOUT, SERVICE_TYPE_HOST
from .const import CAST_TYPE_CHROMECAST, REQUEST_TIMEOUT
from .controllers.media import STREAM_TYPE_BUFFERED # noqa: F401
from .models import CastInfo
from .models import CastInfo, HostServiceInfo
from .response_handler import WaitResponse

__all__ = ("get_chromecasts", "Chromecast")
Expand All @@ -44,7 +43,7 @@ def get_chromecast_from_host(host, tries=None, retry_wait=None, timeout=None):
ip_address, port, uuid, model_name, friendly_name = host
_LOGGER.debug("get_chromecast_from_host %s", host)
port = port or 8009
services = [ServiceInfo(SERVICE_TYPE_HOST, (ip_address, port))]
services = [HostServiceInfo(ip_address, port)]
cast_info = CastInfo(
services, uuid, model_name, friendly_name, ip_address, port, None, None
)
Expand Down
2 changes: 0 additions & 2 deletions pychromecast/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
"V705-H3": (CAST_TYPE_CHROMECAST, MF_VIZIO),
}

SERVICE_TYPE_HOST = "host"
SERVICE_TYPE_MDNS = "mdns"

MESSAGE_TYPE = "type"
REQUEST_ID = "requestId"
Expand Down
17 changes: 6 additions & 11 deletions pychromecast/dial.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@

import zeroconf

from .const import (
CAST_TYPE_AUDIO,
CAST_TYPE_CHROMECAST,
CAST_TYPE_GROUP,
SERVICE_TYPE_HOST,
)
from .models import ZEROCONF_ERRORS, CastInfo, ServiceInfo
from .const import CAST_TYPE_AUDIO, CAST_TYPE_CHROMECAST, CAST_TYPE_GROUP
from .models import ZEROCONF_ERRORS, CastInfo, HostServiceInfo

XML_NS_UPNP_DEVICE = "{urn:schemas-upnp-org:device-1-0}"

Expand All @@ -31,8 +26,8 @@ def get_host_from_service(service, zconf):
"""Resolve host and port from service."""
service_info = None

if service.type == SERVICE_TYPE_HOST:
return service.data + (None,)
if isinstance(service, HostServiceInfo):
return (service.host, service.port, None)

try:
service_info = zconf.get_service_info("_googlecast._tcp.local.", service.data)
Expand Down Expand Up @@ -177,7 +172,7 @@ def get_device_info( # pylint: disable=too-many-locals

try:
if services is None:
services = [ServiceInfo(SERVICE_TYPE_HOST, (host, 8009))]
services = [HostServiceInfo(host, 8009)]

# Try connection with SSL first, and if it fails fall back to non-SSL
try:
Expand Down Expand Up @@ -271,7 +266,7 @@ def get_multizone_status(host, services=None, zconf=None, timeout=30, context=No

try:
if services is None:
services = [ServiceInfo(SERVICE_TYPE_HOST, (host, 8009))]
services = [HostServiceInfo(host, 8009)]
_, status = _get_status(
services,
zconf,
Expand Down
25 changes: 9 additions & 16 deletions pychromecast/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@

import zeroconf

from .const import (
CAST_TYPE_AUDIO,
CAST_TYPE_GROUP,
CAST_TYPES,
MF_GOOGLE,
SERVICE_TYPE_HOST,
SERVICE_TYPE_MDNS,
)
from .const import CAST_TYPE_AUDIO, CAST_TYPE_GROUP, CAST_TYPES, MF_GOOGLE
from .dial import get_device_info, get_multizone_status, get_ssl_context
from .models import ZEROCONF_ERRORS, CastInfo, ServiceInfo
from .models import ZEROCONF_ERRORS, CastInfo, HostServiceInfo, MDNSServiceInfo

DISCOVER_TIMEOUT = 5

Expand Down Expand Up @@ -111,7 +104,7 @@ def remove_service(self, _zconf, typ, name):
cast_info = None
device_removed = False
uuid = None
service_info = ServiceInfo(SERVICE_TYPE_MDNS, name)
service_info = MDNSServiceInfo(name)
# Lock because the HostBrowser may also add or remove items
with self._services_lock:
for uuid, info_for_uuid in self._devices.items():
Expand Down Expand Up @@ -204,7 +197,7 @@ def get_value(key):
)
return

service_info = ServiceInfo(SERVICE_TYPE_MDNS, name)
service_info = MDNSServiceInfo(name)

# Lock because the HostBrowser may also add or remove items
with self._services_lock:
Expand Down Expand Up @@ -419,8 +412,8 @@ def _update_devices(self, host, devices, host_uuids):
for uuid in self._devices:
for service in self._devices[uuid].services.copy():
if (
service.type == SERVICE_TYPE_HOST
and service.data[0] == host
isinstance(service, HostServiceInfo)
and service.host == host
and uuid not in host_uuids
):
self._remove_host_service(host, uuid, callbacks)
Expand All @@ -440,7 +433,7 @@ def _add_host_service(
cast_type,
manufacturer,
):
service_info = ServiceInfo(SERVICE_TYPE_HOST, (host, port))
service_info = HostServiceInfo(host, port)

callback = self._cast_listener.add_cast
if uuid in self._devices:
Expand Down Expand Up @@ -493,9 +486,9 @@ def _remove_host_service(self, host, uuid, callbacks):

info_for_uuid = self._devices[uuid]
for service in info_for_uuid.services:
if service.type == SERVICE_TYPE_HOST and service.data[0] == host:
if isinstance(service, HostServiceInfo) and service.host == host:
info_for_uuid.services.remove(service)
port = service.data[1]
port = service.port
name = f"{host}:{port}"
_LOGGER.debug(
"Host %s down or no longer handles uuid %s, removing host based service",
Expand Down
15 changes: 11 additions & 4 deletions pychromecast/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class CastInfo:
"""Cast info container."""

services: list[ServiceInfo]
services: list[HostServiceInfo | MDNSServiceInfo]
uuid: UUID
model_name: str | None
friendly_name: str | None
Expand All @@ -30,8 +30,15 @@ class CastInfo:


@dataclass(frozen=True)
class ServiceInfo:
class HostServiceInfo:
"""Service info container."""

type: str
data: tuple[str, int] | str
host: str
port: int


@dataclass(frozen=True)
class MDNSServiceInfo:
"""Service info container."""

name: str