1 """Config flow for the html5 component."""
3 from __future__
import annotations
6 from typing
import Any, cast
8 from cryptography.hazmat.backends
import default_backend
9 from cryptography.hazmat.primitives
import serialization
10 from cryptography.hazmat.primitives.asymmetric
import ec
11 from py_vapid
import Vapid
12 from py_vapid.utils
import b64urlencode
13 import voluptuous
as vol
19 from .const
import ATTR_VAPID_EMAIL, ATTR_VAPID_PRV_KEY, ATTR_VAPID_PUB_KEY, DOMAIN
20 from .issues
import async_create_html5_issue
24 """Generate a VAPID private key."""
25 private_key = ec.generate_private_key(ec.SECP256R1(), default_backend())
27 binascii.unhexlify(f
"{private_key.private_numbers().private_value:x}".zfill(64))
32 """Get the VAPID public key from a private key."""
33 vapid = Vapid.from_string(private_key)
34 public_key = cast(ec.EllipticCurvePublicKey, vapid.public_key)
36 public_key.public_bytes(
37 serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint
43 """Handle a config flow for HTML5."""
47 self: HTML5ConfigFlow, data: dict[str, str]
48 ) -> tuple[dict[str, str], ConfigFlowResult |
None]:
49 """Create an HTML5 entry."""
53 if not data.get(ATTR_VAPID_PRV_KEY):
59 except (ValueError, binascii.Error):
60 errors[ATTR_VAPID_PRV_KEY] =
"invalid_prv_key"
64 ATTR_VAPID_EMAIL: data[ATTR_VAPID_EMAIL],
65 ATTR_VAPID_PRV_KEY: data[ATTR_VAPID_PRV_KEY],
66 ATTR_VAPID_PUB_KEY: data[ATTR_VAPID_PUB_KEY],
70 return errors, flow_result
73 self: HTML5ConfigFlow, user_input: dict[str, Any] |
None =
None
74 ) -> ConfigFlowResult:
75 """Handle a flow initialized by the user."""
76 errors: dict[str, str] = {}
85 data_schema=vol.Schema(
88 ATTR_VAPID_EMAIL, default=user_input.get(ATTR_VAPID_EMAIL,
"")
90 vol.Optional(ATTR_VAPID_PRV_KEY): str,
97 self: HTML5ConfigFlow, import_config: dict
98 ) -> ConfigFlowResult:
99 """Handle config import from yaml."""
ConfigFlowResult async_step_import(HTML5ConfigFlow self, dict import_config)
tuple[dict[str, str], ConfigFlowResult|None] _async_create_html5_entry(HTML5ConfigFlow self, dict[str, str] data)
ConfigFlowResult async_step_user(HTML5ConfigFlow self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
str vapid_get_public_key(str private_key)
str vapid_generate_private_key()
None async_create_html5_issue(HomeAssistant hass, bool import_success)