Skip to content

Commit

Permalink
HACS update to PowerCalc #16
Browse files Browse the repository at this point in the history
  • Loading branch information
BeardedTinker committed Nov 9, 2022
1 parent 5ce54b2 commit 9d14ea1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
16 changes: 9 additions & 7 deletions custom_components/powercalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
get_power_profile,
has_manufacturer_and_model_information,
)
from .sensors.group import update_associated_group_entry
from .sensors.group import remove_from_associated_group_entries
from .strategy.factory import PowerCalculatorStrategyFactory

PLATFORMS = [Platform.SENSOR]
Expand Down Expand Up @@ -246,12 +246,6 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
)

if unload_ok:
updated_group_entry = await update_associated_group_entry(
hass, config_entry, remove=True
)
if updated_group_entry and updated_group_entry.state == ConfigEntryState.LOADED:
await hass.config_entries.async_reload(updated_group_entry.entry_id)

used_unique_ids: list[str] = hass.data[DOMAIN][DATA_USED_UNIQUE_IDS]
try:
used_unique_ids.remove(config_entry.unique_id)
Expand All @@ -261,6 +255,14 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
return unload_ok


async def async_remove_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Called after a config entry is removed."""
updated_entries = await remove_from_associated_group_entries(hass, config_entry)
for group_entry in updated_entries:
if group_entry.state == ConfigEntryState.LOADED:
await hass.config_entries.async_reload(group_entry.entry_id)


async def create_domain_groups(
hass: HomeAssistant, global_config: dict, domains: list[str]
):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
"requirements": [
"numpy>=1.21.1"
],
"version": "v1.0.3"
"version": "v1.0.4"
}
4 changes: 2 additions & 2 deletions custom_components/powercalc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@
)
from .sensors.energy import create_energy_sensor
from .sensors.group import (
add_to_associated_group,
create_group_sensors,
create_group_sensors_from_config_entry,
update_associated_group_entry,
)
from .sensors.power import RealPowerSensor, VirtualPowerSensor, create_power_sensor
from .sensors.utility_meter import create_utility_meters
Expand Down Expand Up @@ -284,7 +284,7 @@ async def async_setup_entry(
return

# Add entry to an existing group
updated_group_entry = await update_associated_group_entry(hass, entry, remove=False)
updated_group_entry = await add_to_associated_group(hass, entry)

if CONF_UNIQUE_ID not in sensor_config:
sensor_config[CONF_UNIQUE_ID] = entry.unique_id
Expand Down
50 changes: 38 additions & 12 deletions custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,34 +154,60 @@ async def create_group_sensors_from_config_entry(
return group_sensors


async def update_associated_group_entry(
hass: HomeAssistant, config_entry: ConfigEntry, remove: bool
async def remove_from_associated_group_entries(
hass: HomeAssistant, config_entry: ConfigEntry
) -> list[ConfigEntry]:
"""
When the user remove a virtual power config entry we need to update all the groups which this sensor belongs to
"""
sensor_type = config_entry.data.get(CONF_SENSOR_TYPE)
if sensor_type != SensorType.VIRTUAL_POWER:
return []

group_entries = [
entry
for entry in hass.config_entries.async_entries(DOMAIN)
if entry.data.get(CONF_SENSOR_TYPE) == SensorType.GROUP
and config_entry.entry_id in entry.data.get(CONF_GROUP_MEMBER_SENSORS)
]

for group_entry in group_entries:
member_sensors = group_entry.data.get(CONF_GROUP_MEMBER_SENSORS) or []
member_sensors.remove(config_entry.entry_id)

hass.config_entries.async_update_entry(
group_entry,
data={**group_entry.data, CONF_GROUP_MEMBER_SENSORS: member_sensors},
)

return group_entries


async def add_to_associated_group(
hass: HomeAssistant, config_entry: ConfigEntry
) -> ConfigEntry | None:
"""
Update the group config entry when the virtual power config entry is associated to a group
Adds the sensor to the group on creation of the config entry
Removes the sensor from the group on removal of the config entry
When the user has set a group on a virtual power config entry,
we need to add this config entry to the group members sensors and update the group
"""
sensor_type = config_entry.data.get(CONF_SENSOR_TYPE)
if sensor_type != SensorType.VIRTUAL_POWER:
return None

if CONF_GROUP not in config_entry.data:
return None

group_entry_id = config_entry.data.get(CONF_GROUP)
if not group_entry_id:
group_entry = hass.config_entries.async_get_entry(group_entry_id)

if not group_entry:
_LOGGER.error(
f"Cannot add/remove power sensor to group {group_entry_id}. It does not exist."
)
return None

group_entry = hass.config_entries.async_get_entry(group_entry_id)
member_sensors = group_entry.data.get(CONF_GROUP_MEMBER_SENSORS) or []

if remove and config_entry.entry_id in member_sensors:
member_sensors.remove(config_entry.entry_id)
elif config_entry.entry_id not in member_sensors:
member_sensors.append(config_entry.entry_id)
member_sensors.append(config_entry.entry_id)

hass.config_entries.async_update_entry(
group_entry,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/sensors/utility_meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def create_utility_meters(

utility_meters = []

if DATA_UTILITY not in hass.data:
if DATA_UTILITY not in hass.data: # pragma: no cover
hass.data[DATA_UTILITY] = {}

tariffs = sensor_config.get(CONF_UTILITY_METER_TARIFFS)
Expand Down

0 comments on commit 9d14ea1

Please sign in to comment.