2121 ConfigFlowResult ,
2222 OptionsFlowWithReload ,
2323)
24- from homeassistant .const import CONF_EXCLUDE , CONF_HOSTS
2524from homeassistant .core import HomeAssistant , callback
26- from homeassistant .helpers import config_validation as cv
2725from homeassistant .helpers .device_registry import format_mac
2826from homeassistant .helpers .selector import TextSelector , TextSelectorConfig
2927from homeassistant .helpers .typing import VolDictType
3028
3129from .const import (
3230 CONF_HOME_INTERVAL ,
31+ CONF_HOSTS_EXCLUDE ,
32+ CONF_HOSTS_LIST ,
3333 CONF_MAC_EXCLUDE ,
3434 CONF_OPTIONS ,
3535 DEFAULT_OPTIONS ,
@@ -56,11 +56,9 @@ async def async_get_network(hass: HomeAssistant) -> str:
5656 return str (ip_network (f"{ local_ip } /{ network_prefix } " , False ))
5757
5858
59- def _normalize_ips_and_network (hosts_str : str ) -> list [str ] | None :
59+ def _normalize_ips_and_network (hosts : list [ str ] ) -> list [str ] | None :
6060 """Check if a list of hosts are all ips or ip networks."""
61-
6261 normalized_hosts = []
63- hosts = [host for host in cv .ensure_list_csv (hosts_str ) if host != "" ]
6462
6563 for host in sorted (hosts ):
6664 try :
@@ -118,17 +116,17 @@ def _normalize_mac_addresses(mac_addresses: list[str]) -> list[str] | None:
118116def normalize_input (user_input : dict [str , Any ]) -> dict [str , str ]:
119117 """Validate hosts and exclude are valid."""
120118 errors = {}
121- normalized_hosts = _normalize_ips_and_network (user_input [CONF_HOSTS ])
119+ normalized_hosts = _normalize_ips_and_network (user_input [CONF_HOSTS_LIST ])
122120 if not normalized_hosts :
123- errors [CONF_HOSTS ] = "invalid_hosts"
121+ errors [CONF_HOSTS_LIST ] = "invalid_hosts"
124122 else :
125- user_input [CONF_HOSTS ] = "," . join ( normalized_hosts )
123+ user_input [CONF_HOSTS_LIST ] = normalized_hosts
126124
127- normalized_exclude = _normalize_ips_and_network (user_input [CONF_EXCLUDE ])
125+ normalized_exclude = _normalize_ips_and_network (user_input [CONF_HOSTS_EXCLUDE ])
128126 if normalized_exclude is None :
129- errors [CONF_EXCLUDE ] = "invalid_hosts"
127+ errors [CONF_HOSTS_EXCLUDE ] = "invalid_hosts"
130128 else :
131- user_input [CONF_EXCLUDE ] = "," . join ( normalized_exclude )
129+ user_input [CONF_HOSTS_EXCLUDE ] = normalized_exclude
132130
133131 normalized_mac_exclude = _normalize_mac_addresses (user_input [CONF_MAC_EXCLUDE ])
134132 if normalized_mac_exclude is None :
@@ -142,18 +140,23 @@ def normalize_input(user_input: dict[str, Any]) -> dict[str, str]:
142140async def _async_build_schema_with_user_input (
143141 hass : HomeAssistant , user_input : dict [str , Any ], include_options : bool
144142) -> vol .Schema :
145- hosts = user_input .get (CONF_HOSTS , await async_get_network (hass ))
146- exclude = user_input .get (
147- CONF_EXCLUDE , await network .async_get_source_ip (hass , MDNS_TARGET_IP )
143+ hosts = user_input .get (CONF_HOSTS_LIST , [ await async_get_network (hass )] )
144+ ip_exclude = user_input .get (
145+ CONF_HOSTS_EXCLUDE , [ await network .async_get_source_ip (hass , MDNS_TARGET_IP )]
148146 )
147+
149148 mac_exclude = user_input .get (CONF_MAC_EXCLUDE , [])
150149
151150 schema : VolDictType = {
152- vol .Required (CONF_HOSTS , default = hosts ): str ,
151+ vol .Required (CONF_HOSTS_LIST , default = hosts ): TextSelector (
152+ TextSelectorConfig (multiple = True )
153+ ),
153154 vol .Required (
154155 CONF_HOME_INTERVAL , default = user_input .get (CONF_HOME_INTERVAL , 0 )
155156 ): int ,
156- vol .Optional (CONF_EXCLUDE , default = exclude ): str ,
157+ vol .Optional (CONF_HOSTS_EXCLUDE , default = ip_exclude ): TextSelector (
158+ TextSelectorConfig (multiple = True )
159+ ),
157160 vol .Optional (CONF_MAC_EXCLUDE , default = mac_exclude ): TextSelector (
158161 TextSelectorConfig (multiple = True )
159162 ),
@@ -195,8 +198,9 @@ async def async_step_init(
195198 self .options .update (user_input )
196199
197200 if not errors :
201+ title_hosts = ", " .join (self .options [CONF_HOSTS_LIST ])
198202 return self .async_create_entry (
199- title = f"Nmap Tracker { self . options [ CONF_HOSTS ] } " , data = self .options
203+ title = f"Nmap Tracker { title_hosts } " , data = self .options
200204 )
201205
202206 return self .async_show_form (
@@ -212,6 +216,7 @@ class NmapTrackerConfigFlow(ConfigFlow, domain=DOMAIN):
212216 """Handle a config flow for Nmap Tracker."""
213217
214218 VERSION = 1
219+ MINOR_VERSION = 2
215220
216221 def __init__ (self ) -> None :
217222 """Initialize config flow."""
@@ -230,8 +235,9 @@ async def async_step_user(
230235 self .options .update (user_input )
231236
232237 if not errors :
238+ title_hosts = ", " .join (user_input [CONF_HOSTS_LIST ])
233239 return self .async_create_entry (
234- title = f"Nmap Tracker { user_input [ CONF_HOSTS ] } " ,
240+ title = f"Nmap Tracker { title_hosts } " ,
235241 data = {},
236242 options = user_input ,
237243 )
@@ -245,9 +251,9 @@ async def async_step_user(
245251 )
246252
247253 def _async_is_unique_host_list (self , user_input : dict [str , Any ]) -> bool :
248- hosts = _normalize_ips_and_network (user_input [CONF_HOSTS ])
254+ hosts = _normalize_ips_and_network (user_input [CONF_HOSTS_LIST ])
249255 for entry in self ._async_current_entries ():
250- if _normalize_ips_and_network (entry .options [CONF_HOSTS ]) == hosts :
256+ if _normalize_ips_and_network (entry .options [CONF_HOSTS_LIST ]) == hosts :
251257 return False
252258 return True
253259
0 commit comments