Skip to content

Commit

Permalink
Optimizations in ConfigFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdelprete committed Jan 12, 2025
1 parent 370b2a2 commit 0a61a59
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
16 changes: 2 additions & 14 deletions custom_components/abb_powerone_pvi_sunspec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ class RuntimeData:
update_listener: Callable


def get_instance_count(hass: HomeAssistant) -> int:
"""Return number of instances."""
entries = [
entry
for entry in hass.config_entries.async_entries(DOMAIN)
if not entry.disabled_by
]
return len(entries)


async def async_setup_entry(
hass: HomeAssistant, config_entry: ABBPowerOneFimerConfigEntry
):
Expand Down Expand Up @@ -144,15 +134,13 @@ async def async_unload_entry(
# Close API connection if exists
# coordinator = getattr(config_entry.runtime_data, 'coordinator', None)
coordinator = config_entry.runtime_data.coordinator
if coordinator and coordinator.api:
if coordinator.api:
coordinator.api.close()
_LOGGER.debug("Closed API connection")

# Remove update listener if exists
if config_entry.entry_id in hass.data[DOMAIN]:
update_listener = hass.data[DOMAIN][
config_entry.entry_id
].update_listener
update_listener = config_entry.runtime_data.update_listener
if update_listener:
update_listener()
_LOGGER.debug("Removed update listener")
Expand Down
15 changes: 10 additions & 5 deletions custom_components/abb_powerone_pvi_sunspec/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.selector import selector
Expand Down Expand Up @@ -53,7 +58,7 @@ def get_host_from_config(hass: HomeAssistant):
}


class ABBPowerOneFimerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class ABBPowerOneFimerConfigFlow(ConfigFlow, domain=DOMAIN):
"""ABB Power-One PVI SunSpec config flow."""

VERSION = 1
Expand Down Expand Up @@ -92,7 +97,7 @@ async def test_connection(
)
return False

async def async_step_user(self, user_input=None):
async def async_step_user(self, user_input=None) -> ConfigFlowResult:
"""Handle the initial step."""
errors = {}

Expand Down Expand Up @@ -167,7 +172,7 @@ async def async_step_user(self, user_input=None):
)


class ABBPowerOneFimerOptionsFlow(config_entries.OptionsFlow):
class ABBPowerOneFimerOptionsFlow(OptionsFlow):
"""Config flow options handler."""

VERSION = 1
Expand Down Expand Up @@ -213,7 +218,7 @@ def __init__(self, config_entry: ConfigEntry) -> None:
}
)

async def async_step_init(self, user_input=None):
async def async_step_init(self, user_input=None) -> ConfigFlowResult:
"""Manage the options."""

if user_input is not None:
Expand Down
5 changes: 4 additions & 1 deletion custom_components/abb_powerone_pvi_sunspec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ async def async_setup_entry(
return True


class ABBPowerOneFimerSensor(CoordinatorEntity, SensorEntity):
class ABBPowerOneFimerSensor(
CoordinatorEntity, ABBPowerOneFimerConfigEntry, SensorEntity
):
"""Representation of an ABB SunSpec Modbus sensor."""

def __init__(self, coordinator, config_entry, sensor_data):
Expand Down Expand Up @@ -127,6 +129,7 @@ def _handle_coordinator_update(self) -> None:
"_handle_coordinator_update: sensors state written to state machine"
)

# when has_entity_name is True, the resulting entity name will be: {device_name}_{self._name}
@property
def has_entity_name(self):
"""Return the name state."""
Expand Down

0 comments on commit 0a61a59

Please sign in to comment.