Skip to content

Commit

Permalink
refactor: Add type safety (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy authored Nov 4, 2024
1 parent f076c99 commit ff6234c
Show file tree
Hide file tree
Showing 8 changed files with 665 additions and 459 deletions.
22 changes: 20 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ jobs:
name: Lint
steps:
- name: Check out code from GitHub
uses: "actions/[email protected].1"
uses: "actions/[email protected].2"
- name: Setup Python ${{ env.DEFAULT_PYTHON }}
uses: "actions/setup-python@v5.2.0"
uses: "actions/setup-python@v5.3.0"
with:
python-version: ${{ env.DEFAULT_PYTHON }}
- name: Set up poetry
Expand All @@ -79,3 +79,21 @@ jobs:
run: poetry run ruff check .
- name: "Format"
run: poetry run ruff format . --check


mypy:
runs-on: "ubuntu-latest"
name: Typing
steps:
- name: Check out code from GitHub
uses: "actions/[email protected]"
- name: Setup Python ${{ env.DEFAULT_PYTHON }}
uses: "actions/[email protected]"
with:
python-version: ${{ env.DEFAULT_PYTHON }}
- name: Set up poetry
uses: snok/[email protected]
- name: "Set up dependencies"
run: poetry install --no-interaction
- name: "mypy"
run: poetry run mypy -p custom_components.hass_web_proxy
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ __pycache__
*.egg-info
*/build/*
*/dist/*

.mypy_cache/*

# misc
.coverage
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hass_web_proxy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
)


class HASSWebProxyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # type: ignore[call-arg,misc]
class HASSWebProxyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for HASS Web Proxy."""

@staticmethod
@callback # type: ignore[misc]
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> HASSWebProxyOptionsFlowHandler:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hass_web_proxy/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"documentation": "https://github.com/dermotduffy/hass-web-proxy-integration",
"iot_class": "local_push",
"issue_tracker": "https://github.com/dermotduffy/hass-web-proxy-integration/issues",
"requirements": ["hass-web-proxy-lib==0.0.4", "urlmatch==1.0.1"],
"requirements": ["hass-web-proxy-lib==0.0.6", "urlmatch==1.0.1"],
"version": "0.0.0"
}
34 changes: 17 additions & 17 deletions custom_components/hass_web_proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@
client_context_no_verify,
)

from custom_components.hass_web_proxy.const import DOMAIN
from custom_components.hass_web_proxy.data import (
DynamicProxiedURL,
HASSWebProxyConfigEntry,
HASSWebProxyData,
)

from .const import (
CONF_ALLOW_UNAUTHENTICATED,
CONF_DYNAMIC_URLS,
Expand All @@ -46,9 +39,15 @@
CONF_TTL,
CONF_URL_ID,
CONF_URL_PATTERN,
DOMAIN,
SERVICE_CREATE_PROXIED_URL,
SERVICE_DELETE_PROXIED_URL,
)
from .data import (
DynamicProxiedURL,
HASSWebProxyConfigEntry,
HASSWebProxyData,
)

if TYPE_CHECKING:
import ssl
Expand All @@ -58,7 +57,6 @@
from aiohttp import web
from homeassistant.core import HomeAssistant, ServiceCall

from .const import HASSWebProxySSLCiphers

CREATE_PROXIED_URL_SCHEMA = vol.Schema(
{
Expand Down Expand Up @@ -172,7 +170,7 @@ def _get_options(self) -> MappingProxyType[str, Any]:
"""Get a ConfigEntry options for a given request."""
return self._get_config_entry().options

def _get_proxied_url(self, request: web.Request) -> ProxiedURL:
def _get_proxied_url(self, request: web.Request, **_kwargs: Any) -> ProxiedURL:
"""Get the URL to proxy."""
if "url" not in request.query:
raise HASSWebProxyLibNotFoundRequestError
Expand Down Expand Up @@ -207,7 +205,7 @@ def _get_proxied_url(self, request: web.Request) -> ProxiedURL:

for url_pattern in self._get_options().get("url_patterns", []):
if urlmatch.urlmatch(url_pattern, url_to_proxy, path_required=False):
ssl_cipher = options.get(CONF_SSL_CIPHERS)
ssl_cipher = str(options.get(CONF_SSL_CIPHERS))
ssl_verification = options.get(CONF_SSL_VERIFICATION, True)

return ProxiedURL(
Expand All @@ -221,23 +219,25 @@ def _get_proxied_url(self, request: web.Request) -> ProxiedURL:
raise HASSWebProxyLibExpiredError
raise HASSWebProxyLibNotFoundRequestError

def _get_ssl_context_no_verify(
self, ssl_cipher: HASSWebProxySSLCiphers
) -> ssl.SSLContext:
def _get_ssl_context_no_verify(self, ssl_cipher: str) -> ssl.SSLContext:
"""Get an SSL context."""
return client_context_no_verify(
self._proxy_ssl_cipher_to_ha_ssl_cipher(ssl_cipher)
)

def _get_ssl_context(self, ssl_ciphers: HASSWebProxySSLCiphers) -> ssl.SSLContext:
def _get_ssl_context(self, ssl_ciphers: str) -> ssl.SSLContext:
"""Get an SSL context."""
return client_context(self._proxy_ssl_cipher_to_ha_ssl_cipher(ssl_ciphers))

def _proxy_ssl_cipher_to_ha_ssl_cipher(self, ssl_ciphers: str) -> SSLCipherList:
"""Convert a proxy SSL cipher to a HA SSL cipher."""
if ssl_ciphers == CONF_SSL_CIPHERS_DEFAULT:
return SSLCipherList.PYTHON_DEFAULT
return ssl_ciphers
if ssl_ciphers == CONF_SSL_CIPHERS_INSECURE:
return SSLCipherList.INSECURE
if ssl_ciphers == CONF_SSL_CIPHERS_MODERN:
return SSLCipherList.MODERN
if ssl_ciphers == CONF_SSL_CIPHERS_INTERMEDIATE:
return SSLCipherList.INTERMEDIATE
return SSLCipherList.PYTHON_DEFAULT


class V0ProxyView(HAProxyView):
Expand Down
Loading

0 comments on commit ff6234c

Please sign in to comment.