Skip to content

Commit 82fc71b

Browse files
authored
Merge pull request #20 from IoTLabs-pl/TLS-Root-Cert-Fix
Fix misconfigured Haier TLS Certs
2 parents 780846b + eb220ef commit 82fc71b

File tree

5 files changed

+100
-9
lines changed

5 files changed

+100
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ __pycache__/
22
.idea/
33
scripts/translations/
44
scripts/test*
5+
.venv/

custom_components/hon/__init__.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pathlib import Path
33
from typing import Any
44

5+
from aiohttp.client_exceptions import ClientConnectorCertificateError
56
from pyhon import Hon
67
import voluptuous as vol # type: ignore[import-untyped]
78

@@ -12,6 +13,7 @@
1213
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
1314

1415
from .const import CONF_REFRESH_TOKEN, DOMAIN, PLATFORMS
16+
from .ssl import update_certifi_certificates
1517

1618
_LOGGER = logging.getLogger(__name__)
1719

@@ -35,14 +37,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3537
email = entry.data[CONF_EMAIL]
3638
password = entry.data[CONF_PASSWORD]
3739
refresh_token = entry.data.get(CONF_REFRESH_TOKEN)
38-
hon = await Hon(
39-
email=email,
40-
password=password,
41-
session=session,
42-
mqtt=True,
43-
test_data_path=Path(config_dir),
44-
refresh_token=refresh_token,
45-
).setup()
40+
try:
41+
hon = await Hon(
42+
email=email,
43+
password=password,
44+
session=session,
45+
mqtt=True,
46+
test_data_path=Path(config_dir),
47+
refresh_token=refresh_token,
48+
).setup()
49+
except ClientConnectorCertificateError:
50+
await update_certifi_certificates(hass)
51+
return False
4652

4753
if (new_refresh_token := hon.auth.refresh_token) != refresh_token:
4854
hass.config_entries.async_update_entry(

custom_components/hon/button.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async def async_press(self) -> None:
104104
persistent_notification.create(
105105
self._hass, f"````\n```\n{diagnostic_info}\n```\n````", title
106106
)
107-
_LOGGER.info(diagnostic_info.replace(" ", "\u200B "))
107+
_LOGGER.info(diagnostic_info.replace(" ", "\u200b "))
108108

109109

110110
class HonDataArchive(HonEntity, ButtonEntity):
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIEszCCA5ugAwIBAgIQCyWUIs7ZgSoVoE6ZUooO+jANBgkqhkiG9w0BAQsFADBh
3+
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
4+
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH
5+
MjAeFw0xNzExMDIxMjI0MzNaFw0yNzExMDIxMjI0MzNaMGAxCzAJBgNVBAYTAlVT
6+
MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j
7+
b20xHzAdBgNVBAMTFlJhcGlkU1NMIFRMUyBSU0EgQ0EgRzEwggEiMA0GCSqGSIb3
8+
DQEBAQUAA4IBDwAwggEKAoIBAQC/uVklRBI1FuJdUEkFCuDL/I3aJQiaZ6aibRHj
9+
ap/ap9zy1aYNrphe7YcaNwMoPsZvXDR+hNJOo9gbgOYVTPq8gXc84I75YKOHiVA4
10+
NrJJQZ6p2sJQyqx60HkEIjzIN+1LQLfXTlpuznToOa1hyTD0yyitFyOYwURM+/CI
11+
8FNFMpBhw22hpeAQkOOLmsqT5QZJYeik7qlvn8gfD+XdDnk3kkuuu0eG+vuyrSGr
12+
5uX5LRhFWlv1zFQDch/EKmd163m6z/ycx/qLa9zyvILc7cQpb+k7TLra9WE17YPS
13+
n9ANjG+ECo9PDW3N9lwhKQCNvw1gGoguyCQu7HE7BnW8eSSFAgMBAAGjggFmMIIB
14+
YjAdBgNVHQ4EFgQUDNtsgkkPSmcKuBTuesRIUojrVjgwHwYDVR0jBBgwFoAUTiJU
15+
IBiV5uNu5g/6+rkS7QYXjzkwDgYDVR0PAQH/BAQDAgGGMB0GA1UdJQQWMBQGCCsG
16+
AQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAGAQH/AgEAMDQGCCsGAQUFBwEB
17+
BCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AuZGlnaWNlcnQuY29tMEIGA1Ud
18+
HwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEds
19+
b2JhbFJvb3RHMi5jcmwwYwYDVR0gBFwwWjA3BglghkgBhv1sAQEwKjAoBggrBgEF
20+
BQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQUzALBglghkgBhv1sAQIw
21+
CAYGZ4EMAQIBMAgGBmeBDAECAjANBgkqhkiG9w0BAQsFAAOCAQEAGUSlOb4K3Wtm
22+
SlbmE50UYBHXM0SKXPqHMzk6XQUpCheF/4qU8aOhajsyRQFDV1ih/uPIg7YHRtFi
23+
CTq4G+zb43X1T77nJgSOI9pq/TqCwtukZ7u9VLL3JAq3Wdy2moKLvvC8tVmRzkAe
24+
0xQCkRKIjbBG80MSyDX/R4uYgj6ZiNT/Zg6GI6RofgqgpDdssLc0XIRQEotxIZcK
25+
zP3pGJ9FCbMHmMLLyuBd+uCWvVcF2ogYAawufChS/PT61D9rqzPRS5I2uqa3tmIT
26+
44JhJgWhBnFMb7AGQkvNq9KNS9dd3GWc17H/dXa1enoxzWjE0hBdFjxPhUb0W3wi
27+
8o34/m8Fxw==
28+
-----END CERTIFICATE-----
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import asyncio
2+
import logging
3+
from pathlib import Path
4+
import shutil
5+
6+
import certifi
7+
from homeassistant.core import HomeAssistant
8+
9+
10+
_LOGGER = logging.getLogger(__name__)
11+
12+
13+
# Heavily based on https://github.com/Athozs/hass-additional-ca/blob/c9499d39a4b4d7175336d1d31c4e1b6d9bd6932f/custom_components/additional_ca/__init__.py#L125
14+
async def update_certifi_certificates(hass: HomeAssistant) -> bool:
15+
"""Update CA certificates in Certifi bundle."""
16+
17+
certifi_bundle_path = Path(certifi.where())
18+
_LOGGER.debug(f"Certifi CA bundle path: {certifi_bundle_path}")
19+
20+
certifi_backup_path = certifi_bundle_path.with_suffix(
21+
certifi_bundle_path.suffix + ".bak"
22+
)
23+
24+
rapidssl_ca_path = Path(__file__).with_name("RapidSSL_TLS_RSA_CA_G1.crt")
25+
26+
if certifi_backup_path.exists():
27+
# reset Certifi bundle
28+
await hass.async_add_executor_job(
29+
shutil.copyfile, certifi_backup_path, certifi_bundle_path
30+
)
31+
else:
32+
# backup Certifi bundle
33+
await hass.async_add_executor_job(
34+
shutil.copyfile, certifi_bundle_path, certifi_backup_path
35+
)
36+
37+
_LOGGER.info("Certifi bundle CA ready.")
38+
39+
cacerts, rapidssl_ca = await asyncio.gather(
40+
*(
41+
hass.async_add_executor_job(path.read_text)
42+
for path in (certifi_bundle_path, rapidssl_ca_path)
43+
)
44+
)
45+
46+
if rapidssl_ca not in cacerts:
47+
cert_name = rapidssl_ca_path.stem.replace("_", " ")
48+
cacerts += f"\n# Haier hOn: {cert_name}\n"
49+
cacerts += rapidssl_ca
50+
await hass.async_add_executor_job(certifi_bundle_path.write_text, cacerts)
51+
52+
_LOGGER.info(
53+
f"{cert_name} -> loaded into Certifi CA bundle. Restart Home Assistant to apply changes."
54+
)
55+
56+
return True

0 commit comments

Comments
 (0)