-
-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add number platform to the Lektrico integration (#126119)
* 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
Showing
6 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |