77from homeassistant .data_entry_flow import FlowResult
88from homeassistant .helpers .dispatcher import async_dispatcher_send
99from homeassistant .helpers .service_info import zeroconf
10- from homeassistant .const import CONF_HOST , CONF_PORT
1110from homeassistant .helpers .selector import selector
1211
1312from .const import DEFAULT_PORT , DOMAIN
13+ from .custom_translations import translate
1414
1515def _extract_device_name (discovery_info ) -> str :
1616 """Extract a user-friendly device name from zeroconf info."""
1717 properties = discovery_info .properties or {}
1818
1919 if "id" in properties :
20- return f'🔌 Mac({ properties ["id" ].strip ()} )'
20+ return translate ("formats.discovery_name" ) % properties ["id" ].strip ()
21+
2122 # Fall back to cleaning up the service name
2223 name = discovery_info .name or ""
2324
@@ -42,13 +43,6 @@ def _extract_device_name(discovery_info) -> str:
4243
4344_LOGGER = logging .getLogger (__name__ )
4445
45- STEP_USER_DATA_SCHEMA = vol .Schema (
46- {
47- vol .Required (CONF_HOST ): str ,
48- vol .Required (CONF_PORT , default = DEFAULT_PORT ): int ,
49- }
50- )
51-
5246
5347class PowersensorConfigFlow (config_entries .ConfigFlow , domain = DOMAIN ):
5448 """Handle a config flow."""
@@ -58,69 +52,36 @@ class PowersensorConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
5852 def __init__ (self ):
5953 """Initialize the config flow."""
6054
61- # async def async_step_user(self, user_input=None) -> ConfigFlowResult:
62- # """Handle manual user setup."""
63- # errors = {}
64- #
65- # if user_input is not None:
66- # # Validate the host/connection here if needed
67- # try:
68- # # Set unique ID based on host to prevent duplicates
69- # await self.async_set_unique_id(user_input[CONF_HOST])
70- # self._abort_if_unique_id_configured()
71- #
72- # return self.async_create_entry(
73- # title=user_input[CONF_NAME],
74- # data=user_input
75- # )
76- # except Exception as ex:
77- # _LOGGER.error("Error validating configuration: %s", ex)
78- # errors["base"] = "cannot_connect"
79- #
80- # data_schema = vol.Schema(
81- # {
82- # vol.Required(CONF_NAME): cv.string,
83- # vol.Required(CONF_HOST): cv.string,
84- # vol.Optional(CONF_PORT, default=80): cv.port,
85- # }
86- # )
87- #
88- # return self.async_show_form(
89- # step_id="user",
90- # data_schema=data_schema,
91- # errors=errors
92- # )
9355 async def async_step_reconfigure (self , user_input : dict | None = None )-> FlowResult :
9456 entry = self .hass .config_entries .async_get_entry (self .context ["entry_id" ])
57+ dispatcher = entry .runtime_data ["dispatcher" ]
9558
59+ mac2name = { mac : translate ("formats.sensor" ) % mac for mac in dispatcher .sensors }
60+
61+ unknown = "<unknown>"
9662 if user_input is not None :
97- # @todo: better handling of mac association
98- for key , role in user_input .items ():
99- mac = key [ 33 : - 2 ]
100- _LOGGER . debug ( f"Sending { mac } , { role } to { DOMAIN } _update_role" )
101- async_dispatcher_send ( self . hass , f" { DOMAIN } _update_role" ,
102- mac , role ) # default role
103- await self .hass . config_entries . async_reload ( entry . entry_id )
104- return self .async_abort (reason = "Powersensor role updates successful !" )
63+ name2mac = { name : mac for mac , name in mac2name . items () }
64+ for name , role in user_input .items ():
65+ mac = name2mac . get ( name )
66+ if role == unknown :
67+ role = None
68+ _LOGGER . debug ( f"Applying { role } to { mac } " )
69+ async_dispatcher_send ( self .hass , f" { DOMAIN } _update_role" , mac , role )
70+ return self .async_abort (reason = "Roles successfully applied !" )
10571
106- dispatcher = entry .runtime_data ["dispatcher" ]
10772 sensor_roles = {}
10873 for sensor_mac in dispatcher .sensors :
109- role = entry .data .get ('roles' , {}).get (sensor_mac , None )
110- if not role :
111- sensor_roles [vol .Optional (f"Powersensor Sensor [Mac Address: { sensor_mac } ]" )] = vol .In (
112- ["house-net" , "solar" , "water" , "appliance" , "none" ]
113- )
114- else :
115- sensor_roles [vol .Optional (f"Powersensor Sensor [Mac Address: { sensor_mac } ]" , description = {"suggested_value" : role })] = selector ({
116- "select" : {
117- "options" : ["house-net" , "solar" , "water" , "appliance" , "none" ] ,
118- "mode" : "dropdown"
119- }
120- })
121-
122- # if not sensor_roles:
123- # return self.async_abort(reason="no_sensors_found_without_roles")
74+ role = entry .data .get ('roles' , {}).get (sensor_mac , unknown )
75+ sel = selector ({
76+ "select" : {
77+ "options" : [
78+ # Note: these strings are NOT subject to translation
79+ "house-net" , "solar" , "water" , "appliance" , unknown
80+ ],
81+ "mode" : "dropdown" ,
82+ }
83+ })
84+ sensor_roles [vol .Optional (mac2name [sensor_mac ], description = {"suggested_value" : role })] = sel
12485
12586 return self .async_show_form (
12687 step_id = "reconfigure" ,
0 commit comments