Thanks for this - I will try it out.
In case anyone finds this interesting - I have achieved much greater reliability over the last few years by taking advantage of the 2nd telnet port that is available on my unit. I use docker, so I use that to replace the component in the docker image with a modified file, which persists when I update the docker image.
I believe this helps while multiple simultaneous connections are made, but I am not sure. Logging indicates that the secondary port is used successfully often enough after initial failure.
First I added the ‘self._altport = 23’:
def __init__(self, name, host, port, timeout, sources):
"""Initialize the Pioneer device."""
self._name = name
self._host = host
self._port = 8102
self._altport = 23 # addition here
self._timeout = timeout
self._pwstate = "PWR1"
self._volume = 0
self._muted = False
self._selected_source = ""
self._source_name_to_number = sources
self._source_number_to_name = dict((v, k) for k, v in sources.items())
Then added a second try/except to use the ‘self._altport’ if the first is refused:
def telnet_command(self, command):
"""Establish a telnet connection and sends command."""
try:
try:
telnet = telnetlib.Telnet(self._host, self._port, self._timeout)
except (ConnectionRefusedError, OSError):
_LOGGER.warning("AVR %s refused connection on main port - %s", self._name, self._port)
try: # addition start
telnet = telnetlib.Telnet(self._host, self._altport, self._timeout)
except (ConnectionRefusedError, OSError):
_LOGGER.error("AVR %s also refused connection on alt port %s", self._name, self._altport)
return
_LOGGER.warning("Successful connection made to AVR on alt port %s", self._altport) # addition end
telnet.write(command.encode("ASCII") + b"\r")
telnet.read_very_eager() # skip response
telnet.close()
except telnetlib.socket.timeout:
_LOGGER.error("AVR %s - command %s - timed out", self._name, command)