Skip to content

Commit

Permalink
Add number platform to the Lektrico integration (#126119)
Browse files Browse the repository at this point in the history
* Add platform number.

* Remove number user_limit.

* Change LED to led in number snapshot.

* Update homeassistant/components/lektrico/number.py

---------

Co-authored-by: Joost Lekkerkerker <[email protected]>
  • Loading branch information
Lektrico and joostlek authored Sep 17, 2024
1 parent 9209986 commit 84c2074
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 2 deletions.
6 changes: 5 additions & 1 deletion homeassistant/components/lektrico/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from .coordinator import LektricoDeviceDataUpdateCoordinator

# List the platforms that charger supports.
CHARGERS_PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.SENSOR]
CHARGERS_PLATFORMS: list[Platform] = [
Platform.BUTTON,
Platform.NUMBER,
Platform.SENSOR,
]

# List the platforms that load balancer device supports.
LB_DEVICES_PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.SENSOR]
Expand Down
100 changes: 100 additions & 0 deletions homeassistant/components/lektrico/number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Support for Lektrico number entities."""

from collections.abc import Callable, Coroutine
from dataclasses import dataclass
from typing import Any

from lektricowifi import Device

from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.const import (
ATTR_SERIAL_NUMBER,
CONF_TYPE,
PERCENTAGE,
EntityCategory,
UnitOfElectricCurrent,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import LektricoConfigEntry, LektricoDeviceDataUpdateCoordinator
from .entity import LektricoEntity


@dataclass(frozen=True, kw_only=True)
class LektricoNumberEntityDescription(NumberEntityDescription):
"""Describes Lektrico number entity."""

value_fn: Callable[[dict[str, Any]], int]
set_value_fn: Callable[[Device, int], Coroutine[Any, Any, dict[Any, Any]]]


NUMBERS: tuple[LektricoNumberEntityDescription, ...] = (
LektricoNumberEntityDescription(
key="led_max_brightness",
translation_key="led_max_brightness",
entity_category=EntityCategory.CONFIG,
native_min_value=0,
native_max_value=100,
native_step=5,
native_unit_of_measurement=PERCENTAGE,
value_fn=lambda data: int(data["led_max_brightness"]),
set_value_fn=lambda data, value: data.set_led_max_brightness(value),
),
LektricoNumberEntityDescription(
key="dynamic_limit",
translation_key="dynamic_limit",
entity_category=EntityCategory.CONFIG,
native_min_value=0,
native_max_value=32,
native_step=1,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
value_fn=lambda data: int(data["dynamic_current"]),
set_value_fn=lambda data, value: data.set_dynamic_current(value),
),
)


async def async_setup_entry(
hass: HomeAssistant,
entry: LektricoConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Lektrico number entities based on a config entry."""
coordinator = entry.runtime_data

async_add_entities(
LektricoNumber(
description,
coordinator,
f"{entry.data[CONF_TYPE]}_{entry.data[ATTR_SERIAL_NUMBER]}",
)
for description in NUMBERS
)


class LektricoNumber(LektricoEntity, NumberEntity):
"""Defines a Lektrico number entity."""

entity_description: LektricoNumberEntityDescription

def __init__(
self,
description: LektricoNumberEntityDescription,
coordinator: LektricoDeviceDataUpdateCoordinator,
device_name: str,
) -> None:
"""Initialize Lektrico number."""
super().__init__(coordinator, device_name)
self.entity_description = description
self._attr_unique_id = f"{coordinator.serial_number}_{description.key}"

@property
def native_value(self) -> int | None:
"""Return the state of the number."""
return self.entity_description.value_fn(self.coordinator.data)

async def async_set_native_value(self, value: float) -> None:
"""Set the selected value."""
await self.entity_description.set_value_fn(self.coordinator.device, int(value))
await self.coordinator.async_request_refresh()
8 changes: 8 additions & 0 deletions homeassistant/components/lektrico/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
"name": "Charge stop"
}
},
"number": {
"led_max_brightness": {
"name": "Led brightness"
},
"dynamic_limit": {
"name": "Dynamic limit"
}
},
"sensor": {
"state": {
"name": "State",
Expand Down
5 changes: 4 additions & 1 deletion tests/components/lektrico/fixtures/get_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"current_limit_reason": "installation_current",
"voltage_l1": 220.0,
"current_l1": 0.0,
"fw_version": "1.44"
"fw_version": "1.44",
"led_max_brightness": 20,
"dynamic_current": 32,
"user_current": 32
}
113 changes: 113 additions & 0 deletions tests/components/lektrico/snapshots/test_number.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# serializer version: 1
# name: test_all_entities[number.1p7k_500006_dynamic_limit-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max': 32,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'number.1p7k_500006_dynamic_limit',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Dynamic limit',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'dynamic_limit',
'unique_id': '500006_dynamic_limit',
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
})
# ---
# name: test_all_entities[number.1p7k_500006_dynamic_limit-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': '1p7k_500006 Dynamic limit',
'max': 32,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1,
'unit_of_measurement': <UnitOfElectricCurrent.AMPERE: 'A'>,
}),
'context': <ANY>,
'entity_id': 'number.1p7k_500006_dynamic_limit',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '32',
})
# ---
# name: test_all_entities[number.1p7k_500006_led_brightness-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max': 100,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 5,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'number',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'number.1p7k_500006_led_brightness',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Led brightness',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'led_max_brightness',
'unique_id': '500006_led_max_brightness',
'unit_of_measurement': '%',
})
# ---
# name: test_all_entities[number.1p7k_500006_led_brightness-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': '1p7k_500006 Led brightness',
'max': 100,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 5,
'unit_of_measurement': '%',
}),
'context': <ANY>,
'entity_id': 'number.1p7k_500006_led_brightness',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '20',
})
# ---
31 changes: 31 additions & 0 deletions tests/components/lektrico/test_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Tests for the Lektrico number platform."""

from unittest.mock import AsyncMock, patch

from syrupy import SnapshotAssertion

from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

from . import setup_integration

from tests.common import MockConfigEntry, snapshot_platform


async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_device: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
with patch.multiple(
"homeassistant.components.lektrico",
CHARGERS_PLATFORMS=[Platform.NUMBER],
LB_DEVICES_PLATFORMS=[Platform.NUMBER],
):
await setup_integration(hass, mock_config_entry)

await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)

0 comments on commit 84c2074

Please sign in to comment.