Skip to content

Commit

Permalink
Add new security keys to zwave_js config flow (#115835)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 authored Jun 8, 2024
1 parent 43343ea commit 2c41451
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 1 deletion.
27 changes: 26 additions & 1 deletion homeassistant/components/zwave_js/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
from typing import Any

from awesomeversion import AwesomeVersion
from zwave_js_server.client import Client as ZwaveClient
from zwave_js_server.const import CommandClass, RemoveNodeReason
from zwave_js_server.exceptions import BaseZwaveJSServerError, InvalidServerVersion
Expand Down Expand Up @@ -78,13 +79,17 @@
ATTR_VALUE,
ATTR_VALUE_RAW,
CONF_ADDON_DEVICE,
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY,
CONF_ADDON_LR_S2_AUTHENTICATED_KEY,
CONF_ADDON_NETWORK_KEY,
CONF_ADDON_S0_LEGACY_KEY,
CONF_ADDON_S2_ACCESS_CONTROL_KEY,
CONF_ADDON_S2_AUTHENTICATED_KEY,
CONF_ADDON_S2_UNAUTHENTICATED_KEY,
CONF_DATA_COLLECTION_OPTED_IN,
CONF_INTEGRATION_CREATED_ADDON,
CONF_LR_S2_ACCESS_CONTROL_KEY,
CONF_LR_S2_AUTHENTICATED_KEY,
CONF_NETWORK_KEY,
CONF_S0_LEGACY_KEY,
CONF_S2_ACCESS_CONTROL_KEY,
Expand All @@ -97,6 +102,7 @@
EVENT_DEVICE_ADDED_TO_REGISTRY,
LIB_LOGGER,
LOGGER,
LR_ADDON_VERSION,
USER_AGENT,
ZWAVE_JS_NOTIFICATION_EVENT,
ZWAVE_JS_VALUE_NOTIFICATION_EVENT,
Expand Down Expand Up @@ -1051,15 +1057,19 @@ async def async_ensure_addon_running(hass: HomeAssistant, entry: ConfigEntry) ->
s2_access_control_key: str = entry.data.get(CONF_S2_ACCESS_CONTROL_KEY, "")
s2_authenticated_key: str = entry.data.get(CONF_S2_AUTHENTICATED_KEY, "")
s2_unauthenticated_key: str = entry.data.get(CONF_S2_UNAUTHENTICATED_KEY, "")
lr_s2_access_control_key: str = entry.data.get(CONF_LR_S2_ACCESS_CONTROL_KEY, "")
lr_s2_authenticated_key: str = entry.data.get(CONF_LR_S2_AUTHENTICATED_KEY, "")
addon_state = addon_info.state

addon_config = {
CONF_ADDON_DEVICE: usb_path,
CONF_ADDON_S0_LEGACY_KEY: s0_legacy_key,
CONF_ADDON_S2_ACCESS_CONTROL_KEY: s2_access_control_key,
CONF_ADDON_S2_AUTHENTICATED_KEY: s2_authenticated_key,
CONF_ADDON_S2_UNAUTHENTICATED_KEY: s2_unauthenticated_key,
}
if addon_info.version and AwesomeVersion(addon_info.version) >= LR_ADDON_VERSION:
addon_config[CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY] = lr_s2_access_control_key
addon_config[CONF_ADDON_LR_S2_AUTHENTICATED_KEY] = lr_s2_authenticated_key

if addon_state == AddonState.NOT_INSTALLED:
addon_manager.async_schedule_install_setup_addon(
Expand Down Expand Up @@ -1099,6 +1109,21 @@ async def async_ensure_addon_running(hass: HomeAssistant, entry: ConfigEntry) ->
updates[CONF_S2_AUTHENTICATED_KEY] = addon_s2_authenticated_key
if s2_unauthenticated_key != addon_s2_unauthenticated_key:
updates[CONF_S2_UNAUTHENTICATED_KEY] = addon_s2_unauthenticated_key

if addon_info.version and AwesomeVersion(addon_info.version) >= AwesomeVersion(
LR_ADDON_VERSION
):
addon_lr_s2_access_control_key = addon_options.get(
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY, ""
)
addon_lr_s2_authenticated_key = addon_options.get(
CONF_ADDON_LR_S2_AUTHENTICATED_KEY, ""
)
if lr_s2_access_control_key != addon_lr_s2_access_control_key:
updates[CONF_LR_S2_ACCESS_CONTROL_KEY] = addon_lr_s2_access_control_key
if lr_s2_authenticated_key != addon_lr_s2_authenticated_key:
updates[CONF_LR_S2_AUTHENTICATED_KEY] = addon_lr_s2_authenticated_key

if updates:
hass.config_entries.async_update_entry(entry, data={**entry.data, **updates})

Expand Down
52 changes: 52 additions & 0 deletions homeassistant/components/zwave_js/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@
CONF_ADDON_DEVICE,
CONF_ADDON_EMULATE_HARDWARE,
CONF_ADDON_LOG_LEVEL,
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY,
CONF_ADDON_LR_S2_AUTHENTICATED_KEY,
CONF_ADDON_NETWORK_KEY,
CONF_ADDON_S0_LEGACY_KEY,
CONF_ADDON_S2_ACCESS_CONTROL_KEY,
CONF_ADDON_S2_AUTHENTICATED_KEY,
CONF_ADDON_S2_UNAUTHENTICATED_KEY,
CONF_INTEGRATION_CREATED_ADDON,
CONF_LR_S2_ACCESS_CONTROL_KEY,
CONF_LR_S2_AUTHENTICATED_KEY,
CONF_S0_LEGACY_KEY,
CONF_S2_ACCESS_CONTROL_KEY,
CONF_S2_AUTHENTICATED_KEY,
Expand Down Expand Up @@ -86,6 +90,8 @@
CONF_ADDON_S2_ACCESS_CONTROL_KEY: CONF_S2_ACCESS_CONTROL_KEY,
CONF_ADDON_S2_AUTHENTICATED_KEY: CONF_S2_AUTHENTICATED_KEY,
CONF_ADDON_S2_UNAUTHENTICATED_KEY: CONF_S2_UNAUTHENTICATED_KEY,
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY: CONF_LR_S2_ACCESS_CONTROL_KEY,
CONF_ADDON_LR_S2_AUTHENTICATED_KEY: CONF_LR_S2_AUTHENTICATED_KEY,
CONF_ADDON_LOG_LEVEL: CONF_LOG_LEVEL,
CONF_ADDON_EMULATE_HARDWARE: CONF_EMULATE_HARDWARE,
}
Expand Down Expand Up @@ -172,6 +178,8 @@ def __init__(self) -> None:
self.s2_access_control_key: str | None = None
self.s2_authenticated_key: str | None = None
self.s2_unauthenticated_key: str | None = None
self.lr_s2_access_control_key: str | None = None
self.lr_s2_authenticated_key: str | None = None
self.usb_path: str | None = None
self.ws_address: str | None = None
self.restart_addon: bool = False
Expand Down Expand Up @@ -565,6 +573,12 @@ async def async_step_on_supervisor(
self.s2_unauthenticated_key = addon_config.get(
CONF_ADDON_S2_UNAUTHENTICATED_KEY, ""
)
self.lr_s2_access_control_key = addon_config.get(
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY, ""
)
self.lr_s2_authenticated_key = addon_config.get(
CONF_ADDON_LR_S2_AUTHENTICATED_KEY, ""
)
return await self.async_step_finish_addon_setup()

if addon_info.state == AddonState.NOT_RUNNING:
Expand All @@ -584,6 +598,8 @@ async def async_step_configure_addon(
self.s2_access_control_key = user_input[CONF_S2_ACCESS_CONTROL_KEY]
self.s2_authenticated_key = user_input[CONF_S2_AUTHENTICATED_KEY]
self.s2_unauthenticated_key = user_input[CONF_S2_UNAUTHENTICATED_KEY]
self.lr_s2_access_control_key = user_input[CONF_LR_S2_ACCESS_CONTROL_KEY]
self.lr_s2_authenticated_key = user_input[CONF_LR_S2_AUTHENTICATED_KEY]
if not self._usb_discovery:
self.usb_path = user_input[CONF_USB_PATH]

Expand All @@ -594,6 +610,8 @@ async def async_step_configure_addon(
CONF_ADDON_S2_ACCESS_CONTROL_KEY: self.s2_access_control_key,
CONF_ADDON_S2_AUTHENTICATED_KEY: self.s2_authenticated_key,
CONF_ADDON_S2_UNAUTHENTICATED_KEY: self.s2_unauthenticated_key,
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY: self.lr_s2_access_control_key,
CONF_ADDON_LR_S2_AUTHENTICATED_KEY: self.lr_s2_authenticated_key,
}

if new_addon_config != addon_config:
Expand All @@ -614,6 +632,12 @@ async def async_step_configure_addon(
s2_unauthenticated_key = addon_config.get(
CONF_ADDON_S2_UNAUTHENTICATED_KEY, self.s2_unauthenticated_key or ""
)
lr_s2_access_control_key = addon_config.get(
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY, self.lr_s2_access_control_key or ""
)
lr_s2_authenticated_key = addon_config.get(
CONF_ADDON_LR_S2_AUTHENTICATED_KEY, self.lr_s2_authenticated_key or ""
)

schema = {
vol.Optional(CONF_S0_LEGACY_KEY, default=s0_legacy_key): str,
Expand All @@ -624,6 +648,12 @@ async def async_step_configure_addon(
vol.Optional(
CONF_S2_UNAUTHENTICATED_KEY, default=s2_unauthenticated_key
): str,
vol.Optional(
CONF_LR_S2_ACCESS_CONTROL_KEY, default=lr_s2_access_control_key
): str,
vol.Optional(
CONF_LR_S2_AUTHENTICATED_KEY, default=lr_s2_authenticated_key
): str,
}

if not self._usb_discovery:
Expand Down Expand Up @@ -670,6 +700,8 @@ async def async_step_finish_addon_setup(
CONF_S2_ACCESS_CONTROL_KEY: self.s2_access_control_key,
CONF_S2_AUTHENTICATED_KEY: self.s2_authenticated_key,
CONF_S2_UNAUTHENTICATED_KEY: self.s2_unauthenticated_key,
CONF_LR_S2_ACCESS_CONTROL_KEY: self.lr_s2_access_control_key,
CONF_LR_S2_AUTHENTICATED_KEY: self.lr_s2_authenticated_key,
}
)
return self._async_create_entry_from_vars()
Expand All @@ -690,6 +722,8 @@ def _async_create_entry_from_vars(self) -> ConfigFlowResult:
CONF_S2_ACCESS_CONTROL_KEY: self.s2_access_control_key,
CONF_S2_AUTHENTICATED_KEY: self.s2_authenticated_key,
CONF_S2_UNAUTHENTICATED_KEY: self.s2_unauthenticated_key,
CONF_LR_S2_ACCESS_CONTROL_KEY: self.lr_s2_access_control_key,
CONF_LR_S2_AUTHENTICATED_KEY: self.lr_s2_authenticated_key,
CONF_USE_ADDON: self.use_addon,
CONF_INTEGRATION_CREATED_ADDON: self.integration_created_addon,
},
Expand Down Expand Up @@ -801,6 +835,8 @@ async def async_step_configure_addon(
self.s2_access_control_key = user_input[CONF_S2_ACCESS_CONTROL_KEY]
self.s2_authenticated_key = user_input[CONF_S2_AUTHENTICATED_KEY]
self.s2_unauthenticated_key = user_input[CONF_S2_UNAUTHENTICATED_KEY]
self.lr_s2_access_control_key = user_input[CONF_LR_S2_ACCESS_CONTROL_KEY]
self.lr_s2_authenticated_key = user_input[CONF_LR_S2_AUTHENTICATED_KEY]
self.usb_path = user_input[CONF_USB_PATH]

new_addon_config = {
Expand All @@ -810,6 +846,8 @@ async def async_step_configure_addon(
CONF_ADDON_S2_ACCESS_CONTROL_KEY: self.s2_access_control_key,
CONF_ADDON_S2_AUTHENTICATED_KEY: self.s2_authenticated_key,
CONF_ADDON_S2_UNAUTHENTICATED_KEY: self.s2_unauthenticated_key,
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY: self.lr_s2_access_control_key,
CONF_ADDON_LR_S2_AUTHENTICATED_KEY: self.lr_s2_authenticated_key,
CONF_ADDON_LOG_LEVEL: user_input[CONF_LOG_LEVEL],
CONF_ADDON_EMULATE_HARDWARE: user_input.get(
CONF_EMULATE_HARDWARE, False
Expand Down Expand Up @@ -850,6 +888,12 @@ async def async_step_configure_addon(
s2_unauthenticated_key = addon_config.get(
CONF_ADDON_S2_UNAUTHENTICATED_KEY, self.s2_unauthenticated_key or ""
)
lr_s2_access_control_key = addon_config.get(
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY, self.lr_s2_access_control_key or ""
)
lr_s2_authenticated_key = addon_config.get(
CONF_ADDON_LR_S2_AUTHENTICATED_KEY, self.lr_s2_authenticated_key or ""
)
log_level = addon_config.get(CONF_ADDON_LOG_LEVEL, "info")
emulate_hardware = addon_config.get(CONF_ADDON_EMULATE_HARDWARE, False)

Expand All @@ -868,6 +912,12 @@ async def async_step_configure_addon(
vol.Optional(
CONF_S2_UNAUTHENTICATED_KEY, default=s2_unauthenticated_key
): str,
vol.Optional(
CONF_LR_S2_ACCESS_CONTROL_KEY, default=lr_s2_access_control_key
): str,
vol.Optional(
CONF_LR_S2_AUTHENTICATED_KEY, default=lr_s2_authenticated_key
): str,
vol.Optional(CONF_LOG_LEVEL, default=log_level): vol.In(
ADDON_LOG_LEVELS
),
Expand Down Expand Up @@ -921,6 +971,8 @@ async def async_step_finish_addon_setup(
CONF_S2_ACCESS_CONTROL_KEY: self.s2_access_control_key,
CONF_S2_AUTHENTICATED_KEY: self.s2_authenticated_key,
CONF_S2_UNAUTHENTICATED_KEY: self.s2_unauthenticated_key,
CONF_LR_S2_ACCESS_CONTROL_KEY: self.lr_s2_access_control_key,
CONF_LR_S2_AUTHENTICATED_KEY: self.lr_s2_authenticated_key,
CONF_USE_ADDON: True,
CONF_INTEGRATION_CREATED_ADDON: self.integration_created_addon,
}
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/zwave_js/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import logging

from awesomeversion import AwesomeVersion
from zwave_js_server.const.command_class.window_covering import (
WindowCoveringPropertyKey,
)

from homeassistant.const import APPLICATION_NAME, __version__ as HA_VERSION

LR_ADDON_VERSION = AwesomeVersion("0.5.0")

USER_AGENT = {APPLICATION_NAME: HA_VERSION}

CONF_ADDON_DEVICE = "device"
Expand All @@ -20,12 +23,16 @@
CONF_ADDON_S2_ACCESS_CONTROL_KEY = "s2_access_control_key"
CONF_ADDON_S2_AUTHENTICATED_KEY = "s2_authenticated_key"
CONF_ADDON_S2_UNAUTHENTICATED_KEY = "s2_unauthenticated_key"
CONF_ADDON_LR_S2_ACCESS_CONTROL_KEY = "lr_s2_access_control_key"
CONF_ADDON_LR_S2_AUTHENTICATED_KEY = "lr_s2_authenticated_key"
CONF_INTEGRATION_CREATED_ADDON = "integration_created_addon"
CONF_NETWORK_KEY = "network_key"
CONF_S0_LEGACY_KEY = "s0_legacy_key"
CONF_S2_ACCESS_CONTROL_KEY = "s2_access_control_key"
CONF_S2_AUTHENTICATED_KEY = "s2_authenticated_key"
CONF_S2_UNAUTHENTICATED_KEY = "s2_unauthenticated_key"
CONF_LR_S2_ACCESS_CONTROL_KEY = "lr_s2_access_control_key"
CONF_LR_S2_AUTHENTICATED_KEY = "lr_s2_authenticated_key"
CONF_USB_PATH = "usb_path"
CONF_USE_ADDON = "use_addon"
CONF_DATA_COLLECTION_OPTED_IN = "data_collection_opted_in"
Expand Down
Loading

0 comments on commit 2c41451

Please sign in to comment.