Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add number input for apsystems #118825

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ omit =
homeassistant/components/apsystems/__init__.py
homeassistant/components/apsystems/coordinator.py
homeassistant/components/apsystems/entity.py
homeassistant/components/apsystems/number.py
homeassistant/components/apsystems/sensor.py
homeassistant/components/aqualogic/*
homeassistant/components/aquostv/media_player.py
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/apsystems/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

from .coordinator import ApSystemsDataCoordinator

PLATFORMS: list[Platform] = [Platform.SENSOR]
PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR]


@dataclass
class ApSystemsData:
"""Store runtime data."""

coordinator: ApSystemsDataCoordinator
api: APsystemsEZ1M
mawoka-myblock marked this conversation as resolved.
Show resolved Hide resolved
device_id: str


Expand All @@ -33,7 +34,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ApSystemsConfigEntry) ->
await coordinator.async_config_entry_first_refresh()
assert entry.unique_id
entry.runtime_data = ApSystemsData(
coordinator=coordinator, device_id=entry.unique_id
coordinator=coordinator, api=api, device_id=entry.unique_id
mawoka-myblock marked this conversation as resolved.
Show resolved Hide resolved
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
56 changes: 56 additions & 0 deletions homeassistant/components/apsystems/number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""The output limit which can be set in the APsystems local API integration."""

from __future__ import annotations

from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode
from homeassistant.const import UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import DiscoveryInfoType

from . import ApSystemsConfigEntry, ApSystemsData
from .entity import ApSystemsEntity


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ApSystemsConfigEntry,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
config = config_entry.runtime_data

add_entities([ApSystemsMaxOutputNumber(config)])
mawoka-myblock marked this conversation as resolved.
Show resolved Hide resolved


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
_attr_mode = NumberMode.BOX
_attr_native_unit_of_measurement = UnitOfPower.WATT

def __init__(
self,
data: ApSystemsData,
) -> None:
"""Initialize the sensor."""
super().__init__(data)
self._api = data.api
mawoka-myblock marked this conversation as resolved.
Show resolved Hide resolved
self._attr_unique_id = f"{data.device_id}_output_limit"

async def async_update(self) -> None:
"""Set the state with the value fetched from the inverter."""
self._attr_native_value = float(await self._api.get_max_power())
mawoka-myblock marked this conversation as resolved.
Show resolved Hide resolved

async def async_set_native_value(self, value: float) -> None:
"""Set the desired output power."""
value_as_int = int(value)
await self._api.set_max_power(value_as_int)
mawoka-myblock marked this conversation as resolved.
Show resolved Hide resolved
self._attr_native_value = float(
value_as_int
) # Doing that to eliminate the chance of rounding disparaties in contrast to using the input directly
mawoka-myblock marked this conversation as resolved.
Show resolved Hide resolved