Skip to content

Commit

Permalink
Add pre-commit, reformat things
Browse files Browse the repository at this point in the history
  • Loading branch information
palfrey committed Apr 1, 2024
1 parent 36ebf18 commit 05e123c
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 56 deletions.
31 changes: 16 additions & 15 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
---
name: Testing

on:
push:
branches:
- main
pull_request:
push:
branches:
- main
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: ./uv pip install -r requirements.test
- name: pytest
run: pytest -vvv
env:
PYTHONPATH: .
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: ./uv pip install -r requirements.test
- name: pytest
run: pytest -vvv
env:
PYTHONPATH: .
19 changes: 10 additions & 9 deletions .github/workflows/ha.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
---
name: HACS/HA

on:
push:
branches:
- main
- main
pull_request:
schedule:
- cron: "0 0 * * *"
- cron: 0 0 * * *

jobs:
hacs:
name: HACS and Home assistant
runs-on: "ubuntu-latest"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: "home-assistant/actions/hassfest@master"
- name: HACS Action
uses: "hacs/action@main"
with:
category: integration
- uses: actions/checkout@v4
- uses: home-assistant/actions/hassfest@master
- name: HACS Action
uses: hacs/action@main
with:
category: integration
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.downloads/
.venv/
__pycache__/
__pycache__/
.dmypy.json
44 changes: 44 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
repos:
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
args:
- --safe
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-docstrings==1.5.0
- pydocstyle==5.0.2
- Flake8-pyproject==1.2.3
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: [--profile, black]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.3.0
hooks:
- id: check-executables-have-shebangs
stages: [manual]
- id: check-json
- id: trailing-whitespace
exclude_types: [yaml]
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
rev: 0.2.3
hooks:
- id: yamlfmt
args: [--mapping, '2', --offset, '0', --sequence, '2']
exclude: pnpm-lock.yaml
- repo: local
hooks:
- id: mypy
name: mypy
language: system
entry: make mypy
types: [python]
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ sync: requirements.test
watch-tests: sync
PYTHONPATH=. ptw . --now -vvv -s

.PHONY: sync venv watch-tests
pre-commit: sync
pre-commit run -a

mypy: sync
MYPYPATH=stubs dmypy run .

watch-mypy:
watchmedo auto-restart --directory=./ --pattern="*.py;*.pyi" --no-restart-on-command-exit --recursive -- ${MAKE} mypy

.PHONY: sync venv watch-tests
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Previous attempt in core: https://github.com/home-assistant/core/pull/52334
Authenticated plugin: https://github.com/custom-components/authenticated
Discussion: https://community.home-assistant.io/t/add-ip-ban-whitelist-for-http-integration/301365/26
Discussion: https://community.home-assistant.io/t/add-ip-ban-whitelist-for-http-integration/301365/26
34 changes: 21 additions & 13 deletions custom_components/ban_whitelist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""The Ban Whitelist integration."""

from __future__ import annotations

import logging
from ipaddress import IPv4Address, IPv6Address
from typing import List

import voluptuous as vol
from homeassistant.components.http.ban import KEY_BAN_MANAGER, IpBanManager
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
from homeassistant.helpers import config_validation as cv
from homeassistant.components.http.ban import KEY_BAN_MANAGER, IpBanManager
from ipaddress import IPv4Address, IPv6Address
from homeassistant.helpers.typing import ConfigType

from .const import DOMAIN

Expand All @@ -22,7 +23,8 @@
vol.Required("ip_addresses"): vol.All(cv.ensure_list, [cv.string]),
}
)
})
}
)


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
Expand All @@ -34,16 +36,22 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
_LOGGER.info("Not setting whitelist, as no IPs set")
else:
_LOGGER.info("Setting whitelist with %s", whitelist)

original_async_add_ban = ban_manager.async_add_ban

async def whitelist_async_add_ban(self, remote_addr: IPv4Address | IPv6Address) -> None:
original_async_add_ban = IpBanManager.async_add_ban

async def whitelist_async_add_ban(
self: IpBanManager, remote_addr: IPv4Address | IPv6Address
) -> None:
if remote_addr in whitelist:
_LOGGER.info("Not adding %s to ban list, as it's in the whitelist", remote_addr)
_LOGGER.info(
"Not adding %s to ban list, as it's in the whitelist", remote_addr
)
return

await original_async_add_ban(self, remote_addr)

ban_manager.async_add_ban = whitelist_async_add_ban

return True

ban_manager.async_add_ban = ( # type:ignore[method-assign]
whitelist_async_add_ban # type:ignore[assignment]
)

return True
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.mypy]
pretty = true
show_error_codes = true
show_error_context = true
explicit_package_bases = true
warn_unused_ignores = true

[tool.flake8]
max_line_length = 110
2 changes: 1 addition & 1 deletion requirements.constraints
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Fix Python 3.11. See https://github.com/aio-libs/yarl/issues/706
yarl >= 1.8.1
yarl >= 1.8.1
32 changes: 29 additions & 3 deletions requirements.test
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ certifi==2024.2.2
# requests
cffi==1.16.0
# via cryptography
cfgv==3.4.0
# via pre-commit
charset-normalizer==3.3.2
# via requests
ciso8601==2.3.1
Expand All @@ -99,12 +101,16 @@ dbus-fast==2.21.1
# bleak
# bleak-retry-connector
# bluetooth-adapters
distlib==0.3.8
# via virtualenv
ecdsa==0.18.0
# via python-jose
envs==1.4
# via pycognito
execnet==2.0.2
# via pytest-xdist
filelock==3.13.3
# via virtualenv
freezegun==1.4.0
# via
# pytest-freezer
Expand All @@ -124,13 +130,18 @@ hass-nabucasa==0.78.0
home-assistant-bluetooth==1.12.0
# via homeassistant
homeassistant==2024.3.3
# via pytest-homeassistant-custom-component
# via
# homeassistant-stubs
# pytest-homeassistant-custom-component
homeassistant-stubs==2024.3.3
httpcore==1.0.5
# via httpx
httpx==0.27.0
# via
# homeassistant
# respx
identify==2.5.35
# via pre-commit
idna==3.6
# via
# anyio
Expand Down Expand Up @@ -159,6 +170,11 @@ multidict==6.0.5
# via
# aiohttp
# yarl
mypy==1.9.0
mypy-extensions==1.0.0
# via mypy
nodeenv==1.8.0
# via pre-commit
numpy==1.26.0
# via pytest-homeassistant-custom-component
orjson==3.9.15
Expand All @@ -174,8 +190,11 @@ pip==24.0
# via homeassistant
pipdeptree==2.15.1
# via pytest-homeassistant-custom-component
platformdirs==4.2.0
# via virtualenv
pluggy==1.4.0
# via pytest
pre-commit==3.7.0
pyasn1==0.6.0
# via
# python-jose
Expand Down Expand Up @@ -254,7 +273,9 @@ pytz==2024.1
# astral
# pyrfc3339
pyyaml==6.0.1
# via homeassistant
# via
# homeassistant
# pre-commit
requests==2.31.0
# via
# acme
Expand All @@ -270,7 +291,9 @@ rsa==4.9
s3transfer==0.10.1
# via boto3
setuptools==69.2.0
# via acme
# via
# acme
# nodeenv
six==1.16.0
# via
# ecdsa
Expand All @@ -296,6 +319,7 @@ typing-extensions==4.10.0
# via
# bleak
# homeassistant
# mypy
# pydantic
# sqlalchemy
ulid-transform==0.9.0
Expand All @@ -309,6 +333,8 @@ usb-devices==0.4.5
# via
# bluetooth-adapters
# bluetooth-auto-recovery
virtualenv==20.25.1
# via pre-commit
voluptuous==0.13.1
# via
# homeassistant
Expand Down
7 changes: 5 additions & 2 deletions requirements.test.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pytest
homeassistant
homeassistant >= 2023.8.3
homeassistant-stubs
voluptuous
pytest-watcher
pytest-homeassistant-custom-component >=0.13.53
anyio[asyncio]
anyio[asyncio]
pre-commit
mypy
14 changes: 14 additions & 0 deletions stubs/voluptuous.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# FIXME: need >=0.14 and HA depends on 0.13.1 right now

from typing import Any, Callable, List

class Schema:
def __init__(self, config: dict) -> None: ...

class Required:
def __init__(self, name: str) -> None: ...

class All:
def __init__(
self, *args: List[Callable[[Any], Any]] | Callable[[Any], Any]
) -> None: ...
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the Ban Whitelist integration."""
6 changes: 2 additions & 4 deletions tests/ban_whitelist/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Common fixtures for the Ban Whitelist tests."""

from collections.abc import Generator
from unittest.mock import AsyncMock, patch

import pytest


@pytest.fixture
def anyio_backend():
return 'asyncio'
"""Config anyio with asyncio (as that's what HA uses)."""
return "asyncio"
Loading

0 comments on commit 05e123c

Please sign in to comment.