Skip to content

Commit b0c8d04

Browse files
committed
add async_step_user to support kits that don't discover powersensor
1 parent 77427dc commit b0c8d04

File tree

5 files changed

+86
-38
lines changed

5 files changed

+86
-38
lines changed

custom_components/powersensor/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
8585
if entry.version == 1:
8686
# Move device info into subkey, add with_solar key
8787
devices = { **entry.data }
88-
new_data = { 'devices': devices, 'with_solar': False, 'roles': {} }
88+
new_data = { 'devices': devices, 'with_solar': False, 'with_mains': False, 'roles': {} }
8989
hass.config_entries.async_update_entry(entry, data=new_data, version=2, minor_version=1)
9090

9191
_LOGGER.debug("Upgrading config to %s.%s", entry.version, entry.minor_version)

custom_components/powersensor/config_flow.py

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,14 @@ async def async_step_reconfigure(self, user_input: dict | None = None)->FlowResu
9191
}
9292
)
9393

94-
async def async_step_zeroconf(
95-
self, discovery_info: zeroconf.ZeroconfServiceInfo
96-
) -> ConfigFlowResult:
97-
"""Handle zeroconf discovery."""
98-
host = discovery_info.host
99-
port = discovery_info.port or DEFAULT_PORT
100-
display_name = _extract_device_name(discovery_info) or ""
101-
properties = discovery_info.properties or {}
102-
mac = None
103-
if "id" in properties:
104-
mac = properties["id"].strip()
105-
106-
plug_data = {'host' : host,'port' : port, 'display_name' : display_name,
107-
'mac': mac, 'name': discovery_info.name}
108-
94+
async def _common_setup(self):
10995
if DOMAIN not in self.hass.data:
11096
self.hass.data[DOMAIN] = {}
11197

11298
discovered_plugs_key = "discovered_plugs"
11399
if discovered_plugs_key not in self.hass.data[DOMAIN]:
114100
self.hass.data[DOMAIN][discovered_plugs_key] = {}
115101

116-
if mac in self.hass.data[DOMAIN][discovered_plugs_key].keys():
117-
_LOGGER.debug("Mac found existing in data!")
118-
else:
119-
self.hass.data[DOMAIN][discovered_plugs_key][mac] = plug_data
120-
121-
122-
123102
# register a unique id for the single power sensor entry
124103
await self.async_set_unique_id(DOMAIN)
125104

@@ -128,19 +107,48 @@ async def async_step_zeroconf(
128107
_LOGGER.warning("Aborting - found existing entry!")
129108
return self.async_abort(reason="already_configured")
130109

110+
131111
display_name = f"⚡ Powersensor 🔌\n"
132112
self.context.update({
133113
"title_placeholders": {
134114
"name": display_name
135115
}
136116
})
137-
return await self.async_step_discovery_confirm()
138117

139-
async def async_step_discovery_confirm(
118+
async def async_step_user(
140119
self, user_input: dict[str, Any] | None = None
141120
) -> ConfigFlowResult:
121+
"""Handle zeroconf discovery."""
122+
await self._common_setup()
123+
return await self.async_step_manual_confirm()
124+
125+
126+
async def async_step_zeroconf(
127+
self, discovery_info: zeroconf.ZeroconfServiceInfo
128+
) -> ConfigFlowResult:
129+
"""Handle zeroconf discovery."""
130+
await self._common_setup()
131+
discovered_plugs_key = "discovered_plugs"
132+
host = discovery_info.host
133+
port = discovery_info.port or DEFAULT_PORT
134+
display_name = _extract_device_name(discovery_info) or ""
135+
properties = discovery_info.properties or {}
136+
mac = None
137+
if "id" in properties:
138+
mac = properties["id"].strip()
139+
140+
plug_data = {'host' : host,'port' : port, 'display_name' : display_name,
141+
'mac': mac, 'name': discovery_info.name}
142+
143+
144+
if mac in self.hass.data[DOMAIN][discovered_plugs_key].keys():
145+
_LOGGER.debug("Mac found existing in data!")
146+
else:
147+
self.hass.data[DOMAIN][discovered_plugs_key][mac] = plug_data
142148

143-
"""Confirm discovery."""
149+
return await self.async_step_discovery_confirm()
150+
151+
async def async_step_confirm(self, step_id :str, user_input: dict[str, Any] | None = None):
144152
if user_input is not None:
145153
_LOGGER.debug(self.hass.data[DOMAIN]["discovered_plugs"])
146154
return self.async_create_entry(
@@ -151,4 +159,16 @@ async def async_step_discovery_confirm(
151159
'roles': {},
152160
}
153161
)
154-
return self.async_show_form(step_id="discovery_confirm")
162+
return self.async_show_form(step_id=step_id)
163+
164+
async def async_step_discovery_confirm(
165+
self, user_input: dict[str, Any] | None = None
166+
) -> ConfigFlowResult:
167+
return await self.async_step_confirm(step_id="discovery_confirm", user_input=user_input)
168+
169+
170+
async def async_step_manual_confirm(
171+
self, user_input: dict[str, Any] | None = None
172+
) -> ConfigFlowResult:
173+
174+
return await self.async_step_confirm(step_id="manual_confirm", user_input=user_input)

custom_components/powersensor/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
CREATE_SENSOR_SIGNAL = f"{DOMAIN}_create_sensor"
1010
DATA_UPDATE_SIGNAL_FMT_MAC_EVENT = f"{DOMAIN}_data_update_%s_%s"
1111
HAVE_SOLAR_SENSOR_SIGNAL = f"{DOMAIN}_have_solar_sensor"
12+
HAVE_MAINS_SENSOR_SIGNAL = f"{DOMAIN}_have_mains_sensor"
1213
ROLE_UPDATE_SIGNAL = f"{DOMAIN}_update_role"
1314
PLUG_ADDED_TO_HA_SIGNAL = f"{DOMAIN}_plug_added_to_homeassistant"
1415
SENSOR_ADDED_TO_HA_SIGNAL = f"{DOMAIN}_sensor_added_to_homeassistant"

custom_components/powersensor/sensor.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
from .PowersensorSensorEntity import PowersensorSensorEntity
1818
from .SensorMeasurements import SensorMeasurements
1919
from .const import (CREATE_PLUG_SIGNAL,
20-
CREATE_SENSOR_SIGNAL,
21-
DATA_UPDATE_SIGNAL_FMT_MAC_EVENT,
22-
HAVE_SOLAR_SENSOR_SIGNAL,
23-
PLUG_ADDED_TO_HA_SIGNAL,
24-
ROLE_UPDATE_SIGNAL,
25-
SENSOR_ADDED_TO_HA_SIGNAL,
26-
)
20+
CREATE_SENSOR_SIGNAL,
21+
DATA_UPDATE_SIGNAL_FMT_MAC_EVENT,
22+
HAVE_SOLAR_SENSOR_SIGNAL,
23+
PLUG_ADDED_TO_HA_SIGNAL,
24+
ROLE_UPDATE_SIGNAL,
25+
SENSOR_ADDED_TO_HA_SIGNAL,
26+
HAVE_MAINS_SENSOR_SIGNAL,
27+
)
2728

2829
_LOGGER = logging.getLogger(__name__)
2930

@@ -79,6 +80,11 @@ async def handle_role_update(mac_address: str, new_role: str):
7980
persist_entry = True
8081
async_dispatcher_send(hass, HAVE_SOLAR_SENSOR_SIGNAL)
8182

83+
if new_role == 'house-net':
84+
new_data['with_mains'] = True # Remember for next time we start
85+
persist_entry = True
86+
async_dispatcher_send(hass, HAVE_MAINS_SENSOR_SIGNAL)
87+
8288
# TODO: for house-net/solar <-> we'd need to change the entities too
8389

8490
if persist_entry:
@@ -117,6 +123,9 @@ async def handle_discovered_sensor(sensor_mac: str, sensor_role: str):
117123
if sensor_role == "solar":
118124
async_dispatcher_send(hass, HAVE_SOLAR_SENSOR_SIGNAL)
119125

126+
if sensor_role == "mains":
127+
async_dispatcher_send(hass, HAVE_MAINS_SENSOR_SIGNAL)
128+
120129
entry.async_on_unload(
121130
async_dispatcher_connect(
122131
hass, CREATE_SENSOR_SIGNAL, handle_discovered_sensor
@@ -130,10 +139,24 @@ async def handle_discovered_sensor(sensor_mac: str, sensor_role: str):
130139
await handle_discovered_sensor(mac, role)
131140

132141
# Register the virtual household entities
133-
household_entities = []
134-
for measurement_type in ConsumptionMeasurements:
135-
household_entities.append(PowersensorHouseholdEntity(vhh, measurement_type))
136-
async_add_entities(household_entities)
142+
143+
144+
async def add_mains_to_virtual_household():
145+
_LOGGER.debug("Enabling mains components in virtual household")
146+
household_entities = []
147+
for measurement_type in ConsumptionMeasurements:
148+
household_entities.append(PowersensorHouseholdEntity(vhh, measurement_type))
149+
async_add_entities(household_entities)
150+
151+
with_mains = entry.data.get('with_mains', False)
152+
if with_mains:
153+
await add_mains_to_virtual_household()
154+
else:
155+
entry.async_on_unload(
156+
async_dispatcher_connect(
157+
hass, HAVE_MAINS_SENSOR_SIGNAL, add_mains_to_virtual_household
158+
)
159+
)
137160

138161
async def add_solar_to_virtual_household():
139162
_LOGGER.debug("Enabling solar components in virtual household")

custom_components/powersensor/translations/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"description": "Do you want to add Powersensor to Home Assistant?",
77
"title": "Powersensor plugs discovered"
88
},
9+
"manual_confirm": {
10+
"description": "No Powersensor plugs were discovered on your network. You can still add the integration and if plugs are later discovered they will be added to Home Assistant. Do you want to add Powersensor to Home Assistant?",
11+
"title": "No Powersensor plugs discovered"
12+
},
913
"reconfigure" : {
1014
"title" : "Update sensor roles",
1115
"description": "Note: Roles provided by sensors will override user settings"

0 commit comments

Comments
 (0)