Skip to content

Commit

Permalink
getting on a plane
Browse files Browse the repository at this point in the history
  • Loading branch information
dalinicus committed Aug 30, 2023
1 parent ea370ba commit dffe7f5
Show file tree
Hide file tree
Showing 27 changed files with 1,079 additions and 220 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = tests/*
28 changes: 28 additions & 0 deletions .github/workflows/code-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Linting/Styling

on:
push:
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
name: Setup Python
with:
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements.dev.txt
- name: Ruff
run: ruff .
- name: Black
run : black .
- name: Codespell
run: codespell --ignore-words-list=hass
- name: MyPy
run: mypy --ignore-missing-imports .
67 changes: 67 additions & 0 deletions .github/workflows/codeql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
pull_request:

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
26 changes: 26 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Tests

on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
name: Setup Python
with:
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements.dev.txt
- name: Run Unit Tests
run: pytest --cov
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
9 changes: 1 addition & 8 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
name: validate
name: HACS/HASS

on:
push:
pull_request:
workflow_dispatch:

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]

validate-hassfest:
name: Hassfest validation
runs-on: ubuntu-latest
Expand Down
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ repos:
hooks:
- id: mypy

- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: pytest
language: system
pass_filenames: false
always_run: true

ci:
autofix_prs: true
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.formatting.provider": "none"
}
4 changes: 2 additions & 2 deletions custom_components/ac_infinity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant

from .acinfinity import ACInfinity
from .ac_infinity import ACInfinity
from .const import DOMAIN, PLATFORMS

_LOGGER = logging.getLogger(__name__)
Expand All @@ -19,7 +19,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})

ac_infinity = ACInfinity(entry.data[CONF_EMAIL], entry.data[CONF_PASSWORD])
await ac_infinity.update_data()
await ac_infinity.update()

hass.data[DOMAIN][entry.entry_id] = ac_infinity

Expand Down
111 changes: 111 additions & 0 deletions custom_components/ac_infinity/ac_infinity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from datetime import timedelta
from typing import List

from homeassistant.helpers.update_coordinator import UpdateFailed
from homeassistant.util import Throttle

from .client import ACInfinityClient
from .const import (
DEVICE_KEY_DEVICE_ID,
DEVICE_KEY_DEVICE_INFO,
DEVICE_KEY_DEVICE_NAME,
DEVICE_KEY_MAC_ADDR,
DEVICE_KEY_PORTS,
DEVICE_PORT_KEY_NAME,
DEVICE_PORT_KEY_PORT,
HOST,
)


class ACInfinityDevice:
def __init__(self, device_json) -> None:
self._device_id = str(device_json[DEVICE_KEY_DEVICE_ID])
self._mac_addr = device_json[DEVICE_KEY_MAC_ADDR]
self._device_name = device_json[DEVICE_KEY_DEVICE_NAME]
self._ports = [
ACInfinityDevicePort(port) for port in device_json[DEVICE_KEY_DEVICE_INFO][DEVICE_KEY_PORTS]
]

@property
def device_id(self):
return self._device_id

@property
def device_name(self):
return self._device_id

@property
def mac_addr(self):
return self._mac_addr

@property
def ports(self):
return self._ports

class ACInfinityDevicePort:
def __init__(self, device_json) -> None:
self._port_id = device_json[DEVICE_PORT_KEY_PORT]
self._port_name = device_json[DEVICE_PORT_KEY_NAME]

@property
def port_id(self):
return self._port_id

@property
def port_name(self):
return self._port_name

class ACInfinity:
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)

def __init__(self, email: str, password: str) -> None:
self._client = ACInfinityClient(HOST, email, password)
self._data: dict = {}

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def update(self):
try:
if not self._client.is_logged_in():
await self._client.login()

self._data = await self._client.get_all_device_info()
except Exception:
raise UpdateFailed from Exception

def get_all_device_meta_data(self) -> List[ACInfinityDevice]:
"""gets device metadata, such as ids, labels, macaddr, etc.. that are not expected to change"""
if(self._data) is None:
return []

return (
[ ACInfinityDevice(device) for device in self._data]
)

def get_device_property(self, device_id:(str|int), property:str):
""" gets a property, if it exists, from a given device, if it exists"""
result = next((
device for device in self._data
if device[DEVICE_KEY_DEVICE_ID] == str(device_id)
), None)

if result is not None:
if property in result:
return result[property]
elif property in result[DEVICE_KEY_DEVICE_INFO]:
return result[DEVICE_KEY_DEVICE_INFO][property]

return None

def get_device_port_property(self, device_id:str, port_id:int, property:str):
""" gets a property, if it exists, from the given port, if it exists, from the given device, if it exists"""
result = next((
port for device in self._data
if device[DEVICE_KEY_DEVICE_ID] == str(device_id)
for port in device[DEVICE_KEY_DEVICE_INFO][DEVICE_KEY_PORTS]
if port[DEVICE_PORT_KEY_PORT] == port_id
), None)

if result is not None and property in result:
return result[property]

return None
82 changes: 0 additions & 82 deletions custom_components/ac_infinity/acinfinity.py

This file was deleted.

Loading

0 comments on commit dffe7f5

Please sign in to comment.