Skip to content

Commit

Permalink
Add github build workflow (#1)
Browse files Browse the repository at this point in the history
* Create build.yml

* Remove travis build

* remove coroutine

* Add flake8 config and fix linting issue

* add simple test and avoid initial connect when autoreconnect is false
  • Loading branch information
Hyralex committed Jul 13, 2021
1 parent b38f140 commit 2192f2b
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 27 deletions.
16 changes: 16 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[flake8]
exclude = .github,.vscode,__pycache__,dist
max-complexity = 25
# To work with Black
# E501: line too long
# W503: Line break occurred before a binary operator
# E203: Whitespace before ':'
# D202 No blank lines allowed after function docstring
# W504 line break after binary operator
ignore =
E501,
W503,
E203,
D202,
W504
noqa-require-code = True
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Build

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [3.5, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.linting.flake8Enabled": true,
"python.linting.enabled": true
}
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ How can you help?
`EFF <https://supporters.eff.org/donate/>`__, or
`EPIC <https://epic.org>`__ and let me know that you did.

.. |Build Status| image:: https://travis-ci.org/nugget/python-anthemav.svg?branch=master
:target: https://travis-ci.org/nugget/python-anthemav
.. |Build Status| image:: https://github.com/nugget/python-anthemav/actions/workflows/build.yml/badge.svg?branch=master
:target: https://github.com/nugget/python-anthemav/actions/workflows/build.yml
.. |GitHub release| image:: https://img.shields.io/github/release/nugget/python-anthemav.svg
:target: https://github.com/nugget/python-anthemav/releases
.. |PyPI| image:: https://img.shields.io/pypi/v/anthemav.svg
Expand Down
9 changes: 5 additions & 4 deletions anthemav/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

try:
ensure_future = asyncio.ensure_future
except:
except Exception:
ensure_future = getattr(asyncio, "async")


Expand Down Expand Up @@ -70,15 +70,16 @@ async def create(
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)
ensure_future(conn.reconnect(), loop=conn._loop)

conn.protocol = protocol_class(
connection_lost_callback=connection_lost,
loop=conn._loop,
update_callback=update_callback,
)

await conn._reconnect()
if auto_reconnect:
await conn.reconnect()

return conn

Expand All @@ -100,7 +101,7 @@ def _reset_retry_interval(self):
def _increase_retry_interval(self):
self._retry_interval = min(300, 1.5 * self._retry_interval)

async def _reconnect(self):
async def reconnect(self):
while True:
try:
if self._halted:
Expand Down
5 changes: 3 additions & 2 deletions anthemav/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def formatted_command(self, command):
try:
self.transport.write(command)
time.sleep(0.01)
except:
except Exception:
self.log.warning("No transport found, unable to send command")

#
Expand Down Expand Up @@ -644,7 +644,8 @@ def mute(self):
@mute.setter
def mute(self, value):
self._set_boolean("Z1MUT", value)
# Query mute because the AVR doesn't always return back the state (eg: after power on without changing the volume first)
# Query mute because the AVR doesn't always return back the state
# (eg: after power on without changing the volume first)
self.query("Z1MUT")

#
Expand Down
9 changes: 4 additions & 5 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
log = logging.getLogger(__name__)


@asyncio.coroutine
def test():
async def test():
parser = argparse.ArgumentParser(description=test.__doc__)
parser.add_argument("--host", default="127.0.0.1", help="IP or FQDN of AVR")
parser.add_argument("--port", default="14999", help="Port of AVR")
Expand All @@ -32,15 +31,15 @@ def log_callback(message):

log.info("Connecting to Anthem AVR at %s:%i" % (host, port))

conn = yield from anthemav.Connection.create(
conn = await anthemav.Connection.create(
host=host, port=port, loop=loop, update_callback=log_callback
)

log.info("Power state is " + str(conn.protocol.power))
conn.protocol.power = True
log.info("Power state is " + str(conn.protocol.power))

yield from asyncio.sleep(2, loop=loop)
await asyncio.sleep(2, loop=loop)

log.info("Panel brightness (raw) is " + str(conn.protocol.panel_brightness))
log.info("Panel brightness (text) is " + str(conn.protocol.panel_brightness_text))
Expand All @@ -62,5 +61,5 @@ def log_callback(message):
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.get_event_loop()
asyncio.async(test())
loop.run_until_complete(test())
loop.run_forever()
3 changes: 3 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
asyncio
pytest-asyncio
pytest
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""" Test anthemav module """
3 changes: 0 additions & 3 deletions tests/fulltests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/usr/bin/env python3

import asyncio
import unittest
import logging

import anthemav


async def test():
log = logging.getLogger(__name__)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
from anthemav import Connection


def test_instantiate_connection():
Connection()


@pytest.mark.asyncio
async def test_create_connection_auto_reconnect_false():
conn = await Connection().create(auto_reconnect=False)
assert conn is not None

0 comments on commit 2192f2b

Please sign in to comment.