Skip to content

Commit ada950d

Browse files
committed
More cleanup, next step debouncing logic to suppress spurious removals
1 parent 94fccb9 commit ada950d

File tree

7 files changed

+37
-23
lines changed

7 files changed

+37
-23
lines changed

custom_components/powersensor/PowersensorEntity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, hass: HomeAssistant, mac : str,
2828
self._hass = hass
2929
self._mac = mac
3030
self._model = f"PowersensorDevice"
31-
self._device_name = f'Powersensor Device (MAC address: {self._mac})'
31+
self._device_name = f'Powersensor Device (ID: {self._mac})'
3232
self._measurement_name= None
3333
self._remove_unavailability_tracker = None
3434
self._timeout = timedelta(seconds=timeout_seconds) # Adjust as needed

custom_components/powersensor/PowersensorHouseholdEntity.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ class HouseholdMeasurements(Enum):
2222
ENERGY_TO_GRID = 7
2323
ENERGY_SOLAR_GENERATION = 8
2424

25+
ConsumptionMeasurements = [
26+
HouseholdMeasurements.POWER_HOME_USE,
27+
HouseholdMeasurements.POWER_FROM_GRID,
28+
HouseholdMeasurements.ENERGY_HOME_USE,
29+
HouseholdMeasurements.ENERGY_FROM_GRID
30+
]
31+
ProductionMeasurements = [
32+
HouseholdMeasurements.POWER_TO_GRID,
33+
HouseholdMeasurements.POWER_SOLAR_GENERATION,
34+
HouseholdMeasurements.ENERGY_TO_GRID,
35+
HouseholdMeasurements.ENERGY_SOLAR_GENERATION
36+
]
2537
@dataclass
2638
class EntityConfig:
2739
name : str

custom_components/powersensor/PowersensorMessageDispatacher.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ async def _plug_remove(self,name, info):
208208
mac = self._known_plug_names[name]
209209
if mac in self.plugs:
210210
await self.plugs[mac].disconnect()
211-
212-
del self.plugs[mac]
211+
del self.plugs[mac]
213212
else:
214213
_LOGGER.warning(f"Received request to delete api for gateway with name [{name}], but this name"
215214
f"is not associated with an existing PlugAPI. Ignoring...")

custom_components/powersensor/PowersensorPlugEntity.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,5 @@ def device_info(self) -> DeviceInfo:
9090
}
9191

9292
def _default_device_name(self) -> str:
93-
# f'🔌 MAC address: ({self._mac})'
94-
return f"Powersensor Plug (MAC Address: {self._mac})"
93+
return f"Powersensor Plug (ID: {self._mac}) 🔌"
9594

custom_components/powersensor/PowersensorSensorEntity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ def _rename_based_on_role(self):
8686
return False
8787

8888
def _default_device_name(self):
89-
return f'Powersensor Sensor (MAC address: {self._mac})'
89+
return f'Powersensor Sensor (ID: {self._mac})'
9090

custom_components/powersensor/__init__.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2727
manifest = integration.manifest
2828

2929
# Establish create the zeroconf discovery service
30-
my_data["zeroconf"] = PowersensorDiscoveryService(hass, manifest["zeroconf"][0])
31-
await my_data["zeroconf"].start()
30+
zeroconf_listener = PowersensorDiscoveryService(hass, manifest["zeroconf"][0])
31+
await zeroconf_listener.start()
3232

3333
# Establish our virtual household
3434
vhh = VirtualHousehold(my_data["with_solar"] if "with_solar" in my_data else False)
35-
entry.runtime_data = { "vhh": vhh }
35+
3636

3737
# TODO: can we move the dispatcher into the entry.runtime_data dict?
38-
my_data["dispatcher"] = PowersensorMessageDispatcher(hass, vhh)
38+
dispatcher = PowersensorMessageDispatcher(hass, vhh)
3939
for mac, network_info in entry.data.items():
40-
await my_data["dispatcher"].enqueue_plug_for_adding(network_info)
41-
40+
await dispatcher.enqueue_plug_for_adding(network_info)
41+
entry.runtime_data = {"vhh": vhh, "zeroconf": zeroconf_listener, "dispatcher": dispatcher}
4242
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
4343
return True
4444

@@ -47,13 +47,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
4747
"""Unload a config entry."""
4848
_LOGGER.debug("Started unloading for %s", entry.entry_id)
4949
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
50-
if DOMAIN in hass.data.keys():
51-
if entry.entry_id in hass.data[DOMAIN].keys():
52-
my_data = hass.data[DOMAIN][entry.entry_id]
53-
if "dispatcher" in my_data.keys():
54-
await my_data["dispatcher"].disconnect()
55-
if "zeroconf" in my_data.keys():
56-
await my_data["zeroconf"].stop()
50+
if entry.runtime_data:
51+
if "dispatcher" in entry.runtime_data.keys():
52+
await entry.runtime_data["dispatcher"].disconnect()
53+
if "zeroconf" in entry.runtime_data.keys():
54+
await entry.runtime_data["zeroconf"].stop()
5755

5856
return unload_ok
5957

custom_components/powersensor/sensor.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from . import PowersensorMessageDispatcher
1212
from .PlugMeasurements import PlugMeasurements
13-
from .PowersensorHouseholdEntity import HouseholdMeasurements, PowersensorHouseholdEntity
13+
from .PowersensorHouseholdEntity import ConsumptionMeasurements, ProductionMeasurements, PowersensorHouseholdEntity
1414
from .PowersensorPlugEntity import PowersensorPlugEntity
1515
from .PowersensorSensorEntity import PowersensorSensorEntity
1616
from .SensorMeasurements import SensorMeasurements
@@ -26,7 +26,7 @@ async def async_setup_entry(
2626
"""Set up the Powersensor sensors."""
2727
vhh = entry.runtime_data["vhh"]
2828
my_data = hass.data[DOMAIN][entry.entry_id]
29-
dispatcher: PowersensorMessageDispatcher = my_data['dispatcher']
29+
dispatcher: PowersensorMessageDispatcher = entry.runtime_data['dispatcher']
3030

3131

3232
async def create_plug(plug_mac_address: str):
@@ -73,13 +73,19 @@ async def handle_discovered_sensor(sensor_mac: str, sensor_role: str):
7373

7474
# Possibly unnecessary but will add sensors where the messages came in early.
7575
# Hopefully keeps wait time less than 30s
76-
for mac, role in my_data['dispatcher'].on_start_sensor_queue.items():
76+
for mac, role in dispatcher.on_start_sensor_queue.items():
7777
await handle_discovered_sensor(mac, role)
7878

7979
# Register the virtual household entities
8080
household_entities = []
81-
for measurement_type in HouseholdMeasurements:
81+
for measurement_type in ConsumptionMeasurements:
8282
household_entities.append(PowersensorHouseholdEntity(vhh, measurement_type))
83+
84+
if "with_solar" in my_data.keys():
85+
if my_data["with_solar"]:
86+
for measurement_type in ProductionMeasurements:
87+
household_entities.append(PowersensorHouseholdEntity(vhh, measurement_type))
88+
8389
async_add_entities(household_entities)
8490

8591
async_dispatcher_send(hass, f"{DOMAIN}_setup_complete", True)

0 commit comments

Comments
 (0)