44import voluptuous as vol
55from homeassistant import config_entries
66from homeassistant .config_entries import ConfigFlowResult
7+ from homeassistant .data_entry_flow import FlowResult
8+ from homeassistant .helpers .dispatcher import async_dispatcher_send
79from homeassistant .helpers .service_info import zeroconf
8- from homeassistant .const import CONF_HOST , CONF_PORT
10+ from homeassistant .helpers . selector import selector
911
10- from .const import DEFAULT_PORT , DOMAIN
12+ from .const import DEFAULT_PORT , DOMAIN , SENSOR_NAME_FORMAT
1113
1214def _extract_device_name (discovery_info ) -> str :
1315 """Extract a user-friendly device name from zeroconf info."""
1416 properties = discovery_info .properties or {}
1517
1618 if "id" in properties :
17- return f'🔌 Mac({ properties ["id" ].strip ()} )'
19+ return f"🔌 Mac({ properties ["id" ].strip ()} )"
20+
1821 # Fall back to cleaning up the service name
1922 name = discovery_info .name or ""
2023
@@ -39,13 +42,6 @@ def _extract_device_name(discovery_info) -> str:
3942
4043_LOGGER = logging .getLogger (__name__ )
4144
42- STEP_USER_DATA_SCHEMA = vol .Schema (
43- {
44- vol .Required (CONF_HOST ): str ,
45- vol .Required (CONF_PORT , default = DEFAULT_PORT ): int ,
46- }
47- )
48-
4945
5046class PowersensorConfigFlow (config_entries .ConfigFlow , domain = DOMAIN ):
5147 """Handle a config flow."""
@@ -55,38 +51,45 @@ class PowersensorConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
5551 def __init__ (self ):
5652 """Initialize the config flow."""
5753
58- # async def async_step_user(self, user_input=None) -> ConfigFlowResult:
59- # """Handle manual user setup."""
60- # errors = {}
61- #
62- # if user_input is not None:
63- # # Validate the host/connection here if needed
64- # try:
65- # # Set unique ID based on host to prevent duplicates
66- # await self.async_set_unique_id(user_input[CONF_HOST])
67- # self._abort_if_unique_id_configured()
68- #
69- # return self.async_create_entry(
70- # title=user_input[CONF_NAME],
71- # data=user_input
72- # )
73- # except Exception as ex:
74- # _LOGGER.error("Error validating configuration: %s", ex)
75- # errors["base"] = "cannot_connect"
76- #
77- # data_schema = vol.Schema(
78- # {
79- # vol.Required(CONF_NAME): cv.string,
80- # vol.Required(CONF_HOST): cv.string,
81- # vol.Optional(CONF_PORT, default=80): cv.port,
82- # }
83- # )
84- #
85- # return self.async_show_form(
86- # step_id="user",
87- # data_schema=data_schema,
88- # errors=errors
89- # )
54+ async def async_step_reconfigure (self , user_input : dict | None = None )-> FlowResult :
55+ entry = self .hass .config_entries .async_get_entry (self .context ["entry_id" ])
56+ dispatcher = entry .runtime_data ["dispatcher" ]
57+
58+ mac2name = { mac : SENSOR_NAME_FORMAT % mac for mac in dispatcher .sensors }
59+
60+ unknown = "<unknown>"
61+ if user_input is not None :
62+ name2mac = { name : mac for mac , name in mac2name .items () }
63+ for name , role in user_input .items ():
64+ mac = name2mac .get (name )
65+ if role == unknown :
66+ role = None
67+ _LOGGER .debug (f"Applying { role } to { mac } " )
68+ async_dispatcher_send (self .hass , f"{ DOMAIN } _update_role" , mac , role )
69+ return self .async_abort (reason = "Roles successfully applied!" )
70+
71+ sensor_roles = {}
72+ for sensor_mac in dispatcher .sensors :
73+ role = entry .data .get ('roles' , {}).get (sensor_mac , unknown )
74+ sel = selector ({
75+ "select" : {
76+ "options" : [
77+ # Note: these strings are NOT subject to translation
78+ "house-net" , "solar" , "water" , "appliance" , unknown
79+ ],
80+ "mode" : "dropdown" ,
81+ }
82+ })
83+ sensor_roles [vol .Optional (mac2name [sensor_mac ], description = {"suggested_value" : role })] = sel
84+
85+ return self .async_show_form (
86+ step_id = "reconfigure" ,
87+ data_schema = vol .Schema (sensor_roles ),
88+ description_placeholders = {
89+ "device_count" : str (len (sensor_roles )),
90+ "docs_url" : "https://dius.github.io/homeassistant-powersensor/data.html#virtual-household"
91+ }
92+ )
9093
9194 async def async_step_zeroconf (
9295 self , discovery_info : zeroconf .ZeroconfServiceInfo
0 commit comments