3 from __future__
import annotations
6 from collections.abc
import Callable, Iterable
8 from dataclasses
import dataclass, field
10 from typing
import Any
14 from .const
import DEVICE_DISCOVERY_TIMEOUT, DEVICE_INTERVIEW_TIMEOUT, ZONES
16 _LOGGER = logging.getLogger(__name__)
21 """Onkyo Receiver Callbacks."""
23 connect: list[Callable[[Receiver],
None]] = field(default_factory=list)
24 update: list[Callable[[Receiver, tuple[str, str, Any]],
None]] = field(
33 conn: pyeiscp.Connection
37 first_connect: bool =
True
38 callbacks: Callbacks = field(default_factory=Callbacks)
42 """Set up Onkyo Receiver."""
44 receiver: Receiver |
None =
None
46 def on_connect(_origin: str) ->
None:
47 assert receiver
is not None
50 def on_update(message: tuple[str, str, Any], _origin: str) ->
None:
51 assert receiver
is not None
52 receiver.on_update(message)
54 _LOGGER.debug(
"Creating receiver: %s (%s)", info.model_name, info.host)
56 connection = await pyeiscp.Connection.create(
59 connect_callback=on_connect,
60 update_callback=on_update,
67 model_name=info.model_name,
68 identifier=info.identifier,
73 def on_connect(self) -> None:
74 """Receiver (re)connected."""
75 _LOGGER.debug(
"Receiver (re)connected: %s (%s)", self.model_name, self.host)
80 self.conn.query_property(zone,
"power")
82 for callback
in self.callbacks.connect:
87 def on_update(self, message: tuple[str, str, Any]) ->
None:
88 """Process new message from the receiver."""
89 _LOGGER.debug(
"Received update callback from %s: %s", self.model_name, message)
90 for callback
in self.callbacks.update:
91 callback(self, message)
96 """Onkyo receiver information."""
105 """Interview Onkyo Receiver."""
106 _LOGGER.debug(
"Interviewing receiver: %s", host)
108 receiver_info: ReceiverInfo |
None =
None
110 event = asyncio.Event()
112 async
def _callback(conn: pyeiscp.Connection) ->
None:
113 """Receiver interviewed, connection not yet active."""
114 nonlocal receiver_info
115 if receiver_info
is None:
116 info =
ReceiverInfo(host, conn.port, conn.name, conn.identifier)
117 _LOGGER.debug(
"Receiver interviewed: %s (%s)", info.model_name, info.host)
121 timeout = DEVICE_INTERVIEW_TIMEOUT
123 await pyeiscp.Connection.discover(
124 host=host, discovery_callback=_callback, timeout=timeout
127 with contextlib.suppress(asyncio.TimeoutError):
128 await asyncio.wait_for(event.wait(), timeout)
134 """Discover Onkyo Receivers."""
135 _LOGGER.debug(
"Discovering receivers")
137 receiver_infos: list[ReceiverInfo] = []
139 async
def _callback(conn: pyeiscp.Connection) ->
None:
140 """Receiver discovered, connection not yet active."""
141 info =
ReceiverInfo(conn.host, conn.port, conn.name, conn.identifier)
142 _LOGGER.debug(
"Receiver discovered: %s (%s)", info.model_name, info.host)
143 receiver_infos.append(info)
145 timeout = DEVICE_DISCOVERY_TIMEOUT
147 await pyeiscp.Connection.discover(discovery_callback=_callback, timeout=timeout)
149 await asyncio.sleep(timeout)
151 return receiver_infos
Receiver async_create(cls, ReceiverInfo info)
ReceiverInfo|None async_interview(str host)
Iterable[ReceiverInfo] async_discover()