Skip to content

Commit

Permalink
Add support for x40 series (#27)
Browse files Browse the repository at this point in the history
* Add support for x40 series

* Fix async and blocking issues with Home Assistant

* Avoid refreshing data too often when receiver is starting

* Add initialised function to better support HA UI setup
  • Loading branch information
Hyralex authored Feb 12, 2022
1 parent c346bf1 commit 8dd814f
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.6, 3.8, 3.9]
python-version: ["3.9", "3.10"]

steps:
- uses: actions/checkout@v2
Expand Down
40 changes: 23 additions & 17 deletions anthemav/connection.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
"""Module containing the connection wrapper for the AVR interface."""
import asyncio
import logging
from typing import Callable
from .protocol import AVR

__all__ = "Connection"

try:
ensure_future = asyncio.ensure_future
except Exception:
ensure_future = getattr(asyncio, "async")
__all__ = ["Connection"]


class Connection:
Expand All @@ -17,16 +13,25 @@ class Connection:
def __init__(self):
"""Instantiate the Connection object."""
self.log = logging.getLogger(__name__)
self.host = ""
self.port = 0
self._loop: asyncio.AbstractEventLoop = None
self._retry_interval = 1
self._closed = False
self._closing = False
self._halted = False
self._auto_reconnect = False
self.protocol: asyncio.Protocol = None

@classmethod
async def create(
cls,
host="localhost",
port=14999,
auto_reconnect=True,
loop=None,
protocol_class=AVR,
update_callback=None,
host: str = "localhost",
port: int = 14999,
auto_reconnect: bool = True,
loop: asyncio.AbstractEventLoop = None,
protocol_class: asyncio.Protocol = AVR,
update_callback: Callable[[str], None] = None,
):
"""Initiate a connection to a specific device.
Expand Down Expand Up @@ -55,7 +60,7 @@ async def create(
:type update_callback:
callable
"""
assert port >= 0, "Invalid port value: %r" % (port)
assert port >= 0, f"Invalid port value: {port}"
conn = cls()

conn.host = host
Expand All @@ -67,10 +72,10 @@ async def create(
conn._halted = False
conn._auto_reconnect = auto_reconnect

def connection_lost():
async def connection_lost():
"""Function callback for Protocoal class when connection is lost."""
if conn._auto_reconnect and not conn._closing:
ensure_future(conn.reconnect(), loop=conn._loop)
await conn.reconnect()

conn.protocol = protocol_class(
connection_lost_callback=connection_lost,
Expand Down Expand Up @@ -102,10 +107,11 @@ def _increase_retry_interval(self):
self._retry_interval = min(300, 1.5 * self._retry_interval)

async def reconnect(self):
"""Connect to the host and keep the connection open"""
while True:
try:
if self._halted:
await asyncio.sleep(2, loop=self._loop)
await asyncio.sleep(2)
else:
self.log.debug(
"Connecting to Anthem AVR at %s:%d", self.host, self.port
Expand All @@ -122,7 +128,7 @@ async def reconnect(self):
self.log.warning("Connecting failed, retrying in %i seconds", interval)
if not self._auto_reconnect or self._closing:
raise
await asyncio.sleep(interval, loop=self._loop)
await asyncio.sleep(interval)

if not self._auto_reconnect or self._closing:
break
Expand Down
Loading

0 comments on commit 8dd814f

Please sign in to comment.