1 """ZHA repair for inconsistent network settings."""
3 from __future__
import annotations
8 from zigpy.backups
import NetworkBackup
16 from ..const
import DOMAIN
17 from ..radio_manager
import ZhaRadioManager
19 _LOGGER = logging.getLogger(__name__)
21 ISSUE_INCONSISTENT_NETWORK_SETTINGS =
"inconsistent_network_settings"
25 """Format the difference between two network backups."""
29 lines: list[str], text: str, old: Any, new: Any, pre: bool =
True
31 """Add a line to the list if the values are different."""
32 wrap =
"`" if pre
else ""
35 lines.append(f
"{text}: {wrap}{old}{wrap} \u2192 {wrap}{new}{wrap}")
40 old=old_state.network_info.channel,
41 new=new_state.network_info.channel,
47 old=old_state.node_info.ieee,
48 new=new_state.node_info.ieee,
53 old=old_state.network_info.pan_id,
54 new=new_state.network_info.pan_id,
59 old=old_state.network_info.extended_pan_id,
60 new=new_state.network_info.extended_pan_id,
65 old=old_state.network_info.nwk_update_id,
66 new=new_state.network_info.nwk_update_id,
72 old=old_state.network_info.tc_link_key.key,
73 new=new_state.network_info.tc_link_key.key,
78 old=old_state.network_info.network_key.key,
79 new=new_state.network_info.network_key.key,
82 return "\n".join([f
"- {line}" for line
in lines])
87 config_entry: ConfigEntry,
88 old_state: NetworkBackup,
89 new_state: NetworkBackup,
91 """Create a repair if the network settings are inconsistent with the last backup."""
93 ir.async_create_issue(
96 issue_id=ISSUE_INCONSISTENT_NETWORK_SETTINGS,
98 severity=ir.IssueSeverity.ERROR,
99 translation_key=ISSUE_INCONSISTENT_NETWORK_SETTINGS,
101 "config_entry_id": config_entry.entry_id,
102 "old_state": old_state.as_dict(),
103 "new_state": new_state.as_dict(),
109 """Handler for an issue fixing flow."""
111 def __init__(self, hass: HomeAssistant, data: dict[str, Any]) ->
None:
112 """Initialize the flow."""
114 self.
_old_state_old_state = NetworkBackup.from_dict(data[
"old_state"])
115 self.
_new_state_new_state = NetworkBackup.from_dict(data[
"new_state"])
117 self._entry_id: str = data[
"config_entry_id"]
119 config_entry = self.
hasshass.config_entries.async_get_entry(self._entry_id)
120 assert config_entry
is not None
121 self.
_radio_mgr_radio_mgr = ZhaRadioManager.from_config_entry(self.
hasshass, config_entry)
124 self, user_input: dict[str, str] |
None =
None
126 """Handle the first step of a fix flow."""
129 menu_options=[
"restore_old_settings",
"use_new_settings"],
130 description_placeholders={
136 self, user_input: dict[str, str] |
None =
None
138 """Step to use the new settings found on the radio."""
139 async
with self.
_radio_mgr_radio_mgr.connect_zigpy_app()
as app:
140 app.backups.add_backup(self.
_new_state_new_state)
142 await self.
hasshass.config_entries.async_reload(self._entry_id)
146 self, user_input: dict[str, str] |
None =
None
148 """Step to restore the most recent backup."""
151 await self.
hasshass.config_entries.async_reload(self._entry_id)
FlowResult async_step_init(self, dict[str, str]|None user_input=None)
FlowResult async_step_use_new_settings(self, dict[str, str]|None user_input=None)
None __init__(self, HomeAssistant hass, dict[str, Any] data)
FlowResult async_step_restore_old_settings(self, dict[str, str]|None user_input=None)
_FlowResultT async_show_menu(self, *str|None step_id=None, Container[str] menu_options, Mapping[str, str]|None description_placeholders=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)
bool restore_backup(str config_dir_path)
None warn_on_inconsistent_network_settings(HomeAssistant hass, ConfigEntry config_entry, NetworkBackup old_state, NetworkBackup new_state)
str _format_settings_diff(NetworkBackup old_state, NetworkBackup new_state)