Skip to content

Commit f34c9b9

Browse files
committed
hopefully fixing race condition. Downsides...30s feels like a long time. Some excessive code added to possibly handle that?
1 parent 29d5ce6 commit f34c9b9

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

custom_components/powersensor/PowersensorMessageDispatacher.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(self, hass: HomeAssistant, vhh: VirtualHousehold):
1414
self._vhh = vhh
1515
self.plugs = dict()
1616
self.sensors = dict()
17+
self.on_start_sensor_queue = dict()
1718
self._unsubscribe_from_sensor_added_signal = (
1819
async_dispatcher_connect(self._hass,
1920
f"{DOMAIN}_sensor_added_to_homeassistant",
@@ -49,6 +50,7 @@ async def handle_message(self, event: str, message: dict):
4950
if mac not in self.sensors:
5051
role = None
5152
if 'role' in message:
53+
self.on_start_sensor_queue[mac] = role
5254
role = message['role']
5355
async_dispatcher_send(self._hass, f"{DOMAIN}_create_sensor", mac, role)
5456

custom_components/powersensor/sensor.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ async def async_setup_entry(
3636

3737
async_add_entities(plug_sensors, True)
3838

39-
async def handle_discovered_sensor(sensor_mac, role):
40-
if role == 'solar':
39+
async def handle_discovered_sensor(sensor_mac, sensor_role):
40+
if sensor_role == 'solar':
4141
my_data["with_solar"] = True # Remember for next time we start
4242

4343
new_sensors = [
@@ -46,14 +46,20 @@ async def handle_discovered_sensor(sensor_mac, role):
4646
PowersensorSensorEntity(hass, sensor_mac, SensorMeasurements.SUMMATION_ENERGY),
4747
]
4848
async_add_entities(new_sensors, True)
49-
async_dispatcher_send(hass, f"{DOMAIN}_sensor_added_to_homeassistant", sensor_mac, role)
49+
async_dispatcher_send(hass, f"{DOMAIN}_sensor_added_to_homeassistant", sensor_mac, sensor_role)
5050

5151
entry.async_on_unload(
5252
async_dispatcher_connect(
5353
hass, f"{DOMAIN}_create_sensor", handle_discovered_sensor
5454
)
5555
)
5656

57+
58+
# Possibly unnecessary but will add sensors where the messages came in early.
59+
# Hopefully keeps wait time less than 30s
60+
for mac, role in my_data['dispatcher'].on_start_sensor_queue.items():
61+
await handle_discovered_sensor(mac, role)
62+
5763
# Register the virtual household entities
5864
household_entities = []
5965
for measurement_type in HouseholdMeasurements:

0 commit comments

Comments
 (0)