-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable config flow for html5 (#112806)
* html5: Enable config flow * Add tests * attempt check create_issue * replace len with call_count * fix config flow tests * test user config * more tests * remove whitespace * Update homeassistant/components/html5/issues.py Co-authored-by: Steven B. <[email protected]> * Update homeassistant/components/html5/issues.py Co-authored-by: Steven B. <[email protected]> * fix config * Adjust issues log * lint * lint * rename create issue * fix typing * update codeowners * fix test * fix tests * Update issues.py * Update tests/components/html5/test_config_flow.py Co-authored-by: J. Nick Koston <[email protected]> * Update tests/components/html5/test_config_flow.py Co-authored-by: J. Nick Koston <[email protected]> * Update tests/components/html5/test_config_flow.py Co-authored-by: J. Nick Koston <[email protected]> * update from review * remove ternary * fix * fix missing service * fix tests * updates * adress review comments * fix indent * fix * fix format * cleanup from review * Restore config schema and use HA issue * Restore config schema and use HA issue --------- Co-authored-by: alexyao2015 <[email protected]> Co-authored-by: Steven B. <[email protected]> Co-authored-by: J. Nick Koston <[email protected]> Co-authored-by: Joostlek <[email protected]>
- Loading branch information
1 parent
ac39bf9
commit 2628166
Showing
13 changed files
with
497 additions
and
37 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
"""The html5 component.""" | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import Platform | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import discovery | ||
|
||
from .const import DOMAIN | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Set up HTML5 from a config entry.""" | ||
await discovery.async_load_platform( | ||
hass, Platform.NOTIFY, DOMAIN, dict(entry.data), {} | ||
) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
"""Config flow for the html5 component.""" | ||
|
||
import binascii | ||
from typing import Any, cast | ||
|
||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives import serialization | ||
from cryptography.hazmat.primitives.asymmetric import ec | ||
from py_vapid import Vapid | ||
from py_vapid.utils import b64urlencode | ||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult | ||
from homeassistant.const import CONF_NAME | ||
from homeassistant.core import callback | ||
|
||
from .const import ATTR_VAPID_EMAIL, ATTR_VAPID_PRV_KEY, ATTR_VAPID_PUB_KEY, DOMAIN | ||
from .issues import async_create_html5_issue | ||
|
||
|
||
def vapid_generate_private_key() -> str: | ||
"""Generate a VAPID private key.""" | ||
private_key = ec.generate_private_key(ec.SECP256R1(), default_backend()) | ||
return b64urlencode( | ||
binascii.unhexlify(f"{private_key.private_numbers().private_value:x}".zfill(64)) | ||
) | ||
|
||
|
||
def vapid_get_public_key(private_key: str) -> str: | ||
"""Get the VAPID public key from a private key.""" | ||
vapid = Vapid.from_string(private_key) | ||
public_key = cast(ec.EllipticCurvePublicKey, vapid.public_key) | ||
return b64urlencode( | ||
public_key.public_bytes( | ||
serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint | ||
) | ||
) | ||
|
||
|
||
class HTML5ConfigFlow(ConfigFlow, domain=DOMAIN): | ||
"""Handle a config flow for HTML5.""" | ||
|
||
@callback | ||
def _async_create_html5_entry( | ||
self: "HTML5ConfigFlow", data: dict[str, str] | ||
) -> tuple[dict[str, str], ConfigFlowResult | None]: | ||
"""Create an HTML5 entry.""" | ||
errors = {} | ||
flow_result = None | ||
|
||
if not data.get(ATTR_VAPID_PRV_KEY): | ||
data[ATTR_VAPID_PRV_KEY] = vapid_generate_private_key() | ||
|
||
# we will always generate the corresponding public key | ||
try: | ||
data[ATTR_VAPID_PUB_KEY] = vapid_get_public_key(data[ATTR_VAPID_PRV_KEY]) | ||
except (ValueError, binascii.Error): | ||
errors[ATTR_VAPID_PRV_KEY] = "invalid_prv_key" | ||
|
||
if not errors: | ||
config = { | ||
ATTR_VAPID_EMAIL: data[ATTR_VAPID_EMAIL], | ||
ATTR_VAPID_PRV_KEY: data[ATTR_VAPID_PRV_KEY], | ||
ATTR_VAPID_PUB_KEY: data[ATTR_VAPID_PUB_KEY], | ||
CONF_NAME: DOMAIN, | ||
} | ||
flow_result = self.async_create_entry(title="HTML5", data=config) | ||
return errors, flow_result | ||
|
||
async def async_step_user( | ||
self: "HTML5ConfigFlow", user_input: dict[str, Any] | None = None | ||
) -> ConfigFlowResult: | ||
"""Handle a flow initialized by the user.""" | ||
errors: dict[str, str] = {} | ||
if user_input: | ||
errors, flow_result = self._async_create_html5_entry(user_input) | ||
if flow_result: | ||
return flow_result | ||
else: | ||
user_input = {} | ||
|
||
return self.async_show_form( | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required( | ||
ATTR_VAPID_EMAIL, default=user_input.get(ATTR_VAPID_EMAIL, "") | ||
): str, | ||
vol.Optional(ATTR_VAPID_PRV_KEY): str, | ||
} | ||
), | ||
errors=errors, | ||
) | ||
|
||
async def async_step_import( | ||
self: "HTML5ConfigFlow", import_config: dict | ||
) -> ConfigFlowResult: | ||
"""Handle config import from yaml.""" | ||
_, flow_result = self._async_create_html5_entry(import_config) | ||
if not flow_result: | ||
async_create_html5_issue(self.hass, False) | ||
return self.async_abort(reason="invalid_config") | ||
async_create_html5_issue(self.hass, True) | ||
return flow_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
"""Constants for the HTML5 component.""" | ||
|
||
DOMAIN = "html5" | ||
DATA_HASS_CONFIG = "html5_hass_config" | ||
SERVICE_DISMISS = "dismiss" | ||
|
||
ATTR_VAPID_PUB_KEY = "vapid_pub_key" | ||
ATTR_VAPID_PRV_KEY = "vapid_prv_key" | ||
ATTR_VAPID_EMAIL = "vapid_email" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Issues utility for HTML5.""" | ||
|
||
import logging | ||
|
||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback | ||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue | ||
|
||
from .const import DOMAIN | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
SUCCESSFUL_IMPORT_TRANSLATION_KEY = "deprecated_yaml" | ||
FAILED_IMPORT_TRANSLATION_KEY = "deprecated_yaml_import_issue" | ||
|
||
INTEGRATION_TITLE = "HTML5 Push Notifications" | ||
|
||
|
||
@callback | ||
def async_create_html5_issue(hass: HomeAssistant, import_success: bool) -> None: | ||
"""Create issues for HTML5.""" | ||
if import_success: | ||
async_create_issue( | ||
hass, | ||
HOMEASSISTANT_DOMAIN, | ||
f"deprecated_yaml_{DOMAIN}", | ||
breaks_in_ha_version="2025.4.0", | ||
is_fixable=False, | ||
issue_domain=DOMAIN, | ||
severity=IssueSeverity.WARNING, | ||
translation_key="deprecated_yaml", | ||
translation_placeholders={ | ||
"domain": DOMAIN, | ||
"integration_title": INTEGRATION_TITLE, | ||
}, | ||
) | ||
else: | ||
async_create_issue( | ||
hass, | ||
DOMAIN, | ||
f"deprecated_yaml_{DOMAIN}", | ||
breaks_in_ha_version="2025.4.0", | ||
is_fixable=False, | ||
issue_domain=DOMAIN, | ||
severity=IssueSeverity.WARNING, | ||
translation_key="deprecated_yaml_import_issue", | ||
translation_placeholders={ | ||
"domain": DOMAIN, | ||
"integration_title": INTEGRATION_TITLE, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
{ | ||
"domain": "html5", | ||
"name": "HTML5 Push Notifications", | ||
"codeowners": [], | ||
"codeowners": ["@alexyao2015"], | ||
"config_flow": true, | ||
"dependencies": ["http"], | ||
"documentation": "https://www.home-assistant.io/integrations/html5", | ||
"iot_class": "cloud_push", | ||
"loggers": ["http_ece", "py_vapid", "pywebpush"], | ||
"requirements": ["pywebpush==1.14.1"] | ||
"requirements": ["pywebpush==1.14.1"], | ||
"single_config_entry": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,6 +253,7 @@ | |
"homewizard", | ||
"homeworks", | ||
"honeywell", | ||
"html5", | ||
"huawei_lte", | ||
"hue", | ||
"huisbaasje", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.