Skip to content

Commit

Permalink
Add support for whole apsystems ez1 series by configuring the max_out…
Browse files Browse the repository at this point in the history
…put at setup to match the value by the inverter
  • Loading branch information
mawoka-myblock committed Aug 8, 2024
1 parent d08f4fb commit cc0b7cd
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions homeassistant/components/apsystems/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT, Platform
from homeassistant.core import HomeAssistant

from .const import DEFAULT_PORT
from .const import DEFAULT_PORT, MAX_OUTPUT
from .coordinator import ApSystemsDataCoordinator

PLATFORMS: list[Platform] = [
Expand All @@ -27,23 +27,26 @@ class ApSystemsData:

coordinator: ApSystemsDataCoordinator
device_id: str
max_output: int


type ApSystemsConfigEntry = ConfigEntry[ApSystemsData]


async def async_setup_entry(hass: HomeAssistant, entry: ApSystemsConfigEntry) -> bool:
"""Set up this integration using UI."""
max_output = entry.data.get(MAX_OUTPUT, 800)
api = APsystemsEZ1M(
ip_address=entry.data[CONF_IP_ADDRESS],
port=entry.data.get(CONF_PORT, DEFAULT_PORT),
max_power=max_output,
timeout=8,
)
coordinator = ApSystemsDataCoordinator(hass, api)
await coordinator.async_config_entry_first_refresh()
assert entry.unique_id
entry.runtime_data = ApSystemsData(
coordinator=coordinator, device_id=entry.unique_id
coordinator=coordinator, device_id=entry.unique_id, max_output=max_output
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/apsystems/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv

from .const import DEFAULT_PORT, DOMAIN
from .const import DEFAULT_PORT, DOMAIN, MAX_OUTPUT

DATA_SCHEMA = vol.Schema(
{
Expand Down Expand Up @@ -48,7 +48,7 @@ async def async_step_user(
self._abort_if_unique_id_configured()
return self.async_create_entry(
title="Solar",
data=user_input,
data={**user_input, MAX_OUTPUT: device_info.maxPower},
)

return self.async_show_form(
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/apsystems/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
LOGGER: Logger = getLogger(__package__)
DOMAIN = "apsystems"
DEFAULT_PORT = 8050
MAX_OUTPUT = "max_output"
2 changes: 1 addition & 1 deletion homeassistant/components/apsystems/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async def async_setup_entry(
class ApSystemsMaxOutputNumber(ApSystemsEntity, NumberEntity):
"""Base sensor to be used with description."""

_attr_native_max_value = 800
_attr_native_min_value = 30
_attr_native_step = 1
_attr_device_class = NumberDeviceClass.POWER
Expand All @@ -42,6 +41,7 @@ def __init__(
super().__init__(data)
self._api = data.coordinator.api
self._attr_unique_id = f"{data.device_id}_output_limit"
self._attr_native_max_value = data.max_output

async def async_update(self) -> None:
"""Set the state with the value fetched from the inverter."""
Expand Down
3 changes: 2 additions & 1 deletion tests/components/apsystems/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from unittest.mock import AsyncMock

from homeassistant.components.apsystems.const import DOMAIN
from homeassistant.components.apsystems.const import DOMAIN, MAX_OUTPUT
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
from homeassistant.core import HomeAssistant
Expand All @@ -25,6 +25,7 @@ async def test_form_create_success(
assert result["result"].unique_id == "MY_SERIAL_NUMBER"
assert result.get("type") is FlowResultType.CREATE_ENTRY
assert result["data"].get(CONF_IP_ADDRESS) == "127.0.0.1"
assert result["data"].get(MAX_OUTPUT) == 1000


async def test_form_create_success_custom_port(
Expand Down

0 comments on commit cc0b7cd

Please sign in to comment.