Files
homeassistant-jackery/custom_components/jackery/number.py
Timo b81215415e
Some checks failed
Validate / Validate (push) Has been cancelled
try fix
2026-05-31 12:17:42 +02:00

89 lines
2.9 KiB
Python

"""Jackery Number Platform."""
import logging
from typing import TYPE_CHECKING
from homeassistant.components.number import NumberEntity, NumberMode
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN
if TYPE_CHECKING:
from .sensor import JackeryCoordinator
_LOGGER = logging.getLogger(__name__)
NUMBERS = {
"socChgLimit": {"name": "SOC Charge Limit", "min": 0, "max": 100, "step": 1},
"socDischgLimit": {"name": "SOC Discharge Limit", "min": 0, "max": 100, "step": 1},
"maxOutPw": {"name": "Max Output Power (OnGrid)", "min": 0, "max": 10000, "step": 10},
"autoStandby": {"name": "Auto Standby Mode", "min": 0, "max": 2, "step": 1},
}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
coordinator: "JackeryCoordinator" = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities([
JackeryMainNumber(key=key, coordinator=coordinator, **cfg)
for key, cfg in NUMBERS.items()
])
class JackeryMainNumber(NumberEntity):
"""Numeric setting on the main Jackery device."""
_attr_should_poll = False
_attr_has_entity_name = True
_attr_mode = NumberMode.SLIDER
def __init__(
self,
key: str,
name: str,
min: float,
max: float,
step: float,
coordinator: "JackeryCoordinator",
) -> None:
self._key = key
self._coordinator = coordinator
self._attr_name = name
self._attr_unique_id = f"jackery_{coordinator.entry_id}_main_{key}"
self._attr_native_min_value = min
self._attr_native_max_value = max
self._attr_native_step = step
self._attr_device_info = {
"identifiers": {(DOMAIN, coordinator.entry_id)},
"name": f"Jackery {coordinator.device_sn}",
"manufacturer": "Jackery",
"model": "Energy Monitor",
}
async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
self._coordinator.register_entity(f"main_number_{self._key}", self)
async def async_will_remove_from_hass(self) -> None:
self._coordinator.unregister_entity(f"main_number_{self._key}")
await super().async_will_remove_from_hass()
def _update_from_coordinator(self, data: dict) -> None:
val = data.get(self._key)
if val is None:
return
try:
self._attr_native_value = float(val)
self._attr_available = True
self.async_write_ha_state()
except (TypeError, ValueError):
pass
async def async_set_native_value(self, value: float) -> None:
await self._coordinator.control_main({self._key: int(value)})