From 5dbcf094a19b15e37d49467ce9f43897e53c77cf Mon Sep 17 00:00:00 2001 From: Michel van de Wetering Date: Thu, 1 Aug 2024 21:37:25 +0200 Subject: [PATCH 1/4] Bump min aiohttp version to 3.9 Need support for server_hostname option since my hack does not work anymore on 3.10 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 3fc6c74..60ebf1e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -aiohttp<4 \ No newline at end of file +aiohttp>=3.9.0<4 \ No newline at end of file From 615ab247412700636be89d0f5c0d6409e60dd1de Mon Sep 17 00:00:00 2001 From: Michel van de Wetering Date: Thu, 1 Aug 2024 21:37:54 +0200 Subject: [PATCH 2/4] Remove hacky servername inserter, use official supported option --- aiohuesyncbox/huesyncbox.py | 56 ++----------------------------------- 1 file changed, 2 insertions(+), 54 deletions(-) diff --git a/aiohuesyncbox/huesyncbox.py b/aiohuesyncbox/huesyncbox.py index a57ec76..454fb38 100644 --- a/aiohuesyncbox/huesyncbox.py +++ b/aiohuesyncbox/huesyncbox.py @@ -19,42 +19,6 @@ logger = logging.getLogger(__name__) - -class CommonNameInserterResolver(aiohttp.DefaultResolver): # type: ignore - def __init__(self, common_name, loop=None, *args, **kwargs): - super().__init__(loop=loop, *args, **kwargs) - self._common_name = common_name - - async def resolve(self, host, port=0, family=socket.AF_INET): - hosts = [] - if host.startswith("_"): - # Host was an IP address that was mangled to force a DNS lookup (_ are not valid) - # and with that forced lookup end up in this call. - # This is needed as IP addresses don't need lookup, - # but I need to set the hostname to the common_name for certificate validation - # and this seems to be the only place I could hook into - - # Generate a suitable entry in the hosts list - hosts.append( - { - "host": host[1:], - "port": port, - "family": family, - "proto": 6, # TCP I think - "flags": socket.AI_NUMERICHOST, - } - ) - else: - hosts.append(await super().resolve(host, port=port, family=family)) - - for host in hosts: - host["hostname"] = self._common_name - - logger.debug("Resolved hosts: %s", hosts) - - return hosts - - class HueSyncBox: """Control a Philips Hue Play HDMI Sync Box.""" @@ -100,9 +64,6 @@ def _get_clientsession(self) -> aiohttp.ClientSession: enable_cleanup_closed=True, # Home Assistant sets it so lets do it also ssl=context, limit_per_host=1, # Syncbox can handle a limited amount of connections, only take what we need - resolver=CommonNameInserterResolver( - self._id - ), # Use custom resolver to get certificate validation on common_name working ) return aiohttp.ClientSession(connector=connector, timeout=aiohttp.ClientTimeout(total=10)) @@ -183,19 +144,6 @@ async def update(self): self.hue = Hue(response["hue"], self.request) self.hdmi = Hdmi(response["hdmi"], self.request) - def _mangled_host(self) -> str: - """ - Returns the hostname or a modified hostname in case the host is an IP address - to make sure DNS lookups are required as that allows to use the common_name - instead of servername for certificate validation. - """ - try: - ipaddress.ip_address(self._host) - return f"_{self._host}" - except ValueError: - pass - return self._host - async def request( self, method: str, path: str, data: Optional[Dict] = None, auth: bool = True ): @@ -206,7 +154,7 @@ async def request( # This solves an issue when Updates were scheduled and HA was shutdown return None - url = f"https://{self._mangled_host()}:{self._port}{self._path}/v1{path}" + url = f"https://{self._host}:{self._port}{self._path}/v1{path}" try: logger.debug("%s, %s, %s" % (method, url, data)) @@ -216,7 +164,7 @@ async def request( headers["Authorization"] = f"Bearer {self._access_token}" async with self._clientsession.request( - method, url, json=data, headers=headers + method, url, json=data, headers=headers, server_hostname=self._id ) as resp: logger.debug("%s, %s" % (resp.status, await resp.text("utf-8"))) From 2052c2b974c13c103792b303e455e758e921e721 Mon Sep 17 00:00:00 2001 From: Michel van de Wetering Date: Thu, 1 Aug 2024 21:44:09 +0200 Subject: [PATCH 3/4] Python 3.7 was dropped by aiohttp 3.9 --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 7839259..662e773 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] steps: - uses: actions/checkout@v4 From 578379246917da717ab9ed236fc7f92a3f0c3cbb Mon Sep 17 00:00:00 2001 From: Michel van de Wetering Date: Thu, 1 Aug 2024 21:46:16 +0200 Subject: [PATCH 4/4] Fix missing , in requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 60ebf1e..173359c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -aiohttp>=3.9.0<4 \ No newline at end of file +aiohttp>=3.9.0,<4 \ No newline at end of file