All checks were successful
continuous-integration/drone/push Build is passing
136 lines
4.4 KiB
Python
136 lines
4.4 KiB
Python
"""Number entities for Victron MK2 MQTT integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode
|
|
from homeassistant.const import UnitOfElectricCurrent
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from .const import DATA_BRIDGE, DOMAIN
|
|
from .coordinator import VictronMqttBridge
|
|
from .entity import VictronMqttEntity
|
|
|
|
|
|
async def async_setup_platform(
|
|
hass: HomeAssistant,
|
|
config: dict[str, Any],
|
|
async_add_entities,
|
|
discovery_info: dict[str, Any] | None = None,
|
|
) -> None:
|
|
"""Set up Victron number entities."""
|
|
bridge: VictronMqttBridge = hass.data[DOMAIN][DATA_BRIDGE]
|
|
entities: list[NumberEntity] = [VictronRemotePanelCurrentLimitNumber(bridge)]
|
|
if bridge.venus_guide_compat:
|
|
entities.extend(
|
|
[
|
|
VictronESSGridSetpointNumber(bridge),
|
|
VictronESSMaxChargePowerNumber(bridge),
|
|
VictronESSMaxDischargePowerNumber(bridge),
|
|
]
|
|
)
|
|
async_add_entities(entities)
|
|
|
|
|
|
class VictronRemotePanelCurrentLimitNumber(VictronMqttEntity, NumberEntity):
|
|
"""Remote panel AC input current limit."""
|
|
|
|
_attr_name = "Remote Panel Current Limit"
|
|
_attr_icon = "mdi:current-ac"
|
|
_attr_native_min_value = 0.0
|
|
_attr_native_max_value = 100.0
|
|
_attr_native_step = 0.1
|
|
_attr_mode = NumberMode.BOX
|
|
_attr_device_class = NumberDeviceClass.CURRENT
|
|
_attr_native_unit_of_measurement = UnitOfElectricCurrent.AMPERE
|
|
|
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
|
super().__init__(bridge)
|
|
self._attr_unique_id = f"{bridge.topic_root}_remote_panel_current_limit"
|
|
|
|
@property
|
|
def native_value(self) -> float | None:
|
|
return self.bridge.current_limit
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
return bool(self.bridge.command_topic)
|
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
|
await self.bridge.async_publish_command(
|
|
{"kind": "panel_state", "current_limit": float(value)}
|
|
)
|
|
|
|
|
|
class _VictronESSNumberBase(VictronMqttEntity, NumberEntity):
|
|
"""Base class for ESS compatibility numbers."""
|
|
|
|
_attr_mode = NumberMode.BOX
|
|
_attr_native_step = 1.0
|
|
_attr_native_min_value = -20000.0
|
|
_attr_native_max_value = 20000.0
|
|
_attr_native_unit_of_measurement = "W"
|
|
_attr_icon = "mdi:transmission-tower-export"
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
return bool(self.bridge.command_topic and self.bridge.venus_guide_compat)
|
|
|
|
|
|
class VictronESSGridSetpointNumber(_VictronESSNumberBase):
|
|
"""Guide-compatible ESS AC power setpoint."""
|
|
|
|
_attr_name = "ESS Grid Setpoint"
|
|
|
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
|
super().__init__(bridge)
|
|
self._attr_unique_id = f"{bridge.topic_root}_ess_grid_setpoint"
|
|
|
|
@property
|
|
def native_value(self) -> float | None:
|
|
return self.bridge.ess_setpoint
|
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
|
await self.bridge.async_publish_command({"kind": "ess_setpoint", "value": float(value)})
|
|
|
|
|
|
class VictronESSMaxChargePowerNumber(_VictronESSNumberBase):
|
|
"""Guide-compatible ESS max charge power."""
|
|
|
|
_attr_name = "ESS Max Charge Power"
|
|
_attr_native_min_value = 0.0
|
|
|
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
|
super().__init__(bridge)
|
|
self._attr_unique_id = f"{bridge.topic_root}_ess_max_charge_power"
|
|
|
|
@property
|
|
def native_value(self) -> float | None:
|
|
return self.bridge.ess_max_charge_power
|
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
|
await self.bridge.async_publish_command(
|
|
{"kind": "ess_max_charge_power", "value": float(value)}
|
|
)
|
|
|
|
|
|
class VictronESSMaxDischargePowerNumber(_VictronESSNumberBase):
|
|
"""Guide-compatible ESS max discharge power."""
|
|
|
|
_attr_name = "ESS Max Discharge Power"
|
|
_attr_native_min_value = 0.0
|
|
|
|
def __init__(self, bridge: VictronMqttBridge) -> None:
|
|
super().__init__(bridge)
|
|
self._attr_unique_id = f"{bridge.topic_root}_ess_max_discharge_power"
|
|
|
|
@property
|
|
def native_value(self) -> float | None:
|
|
return self.bridge.ess_max_discharge_power
|
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
|
await self.bridge.async_publish_command(
|
|
{"kind": "ess_max_discharge_power", "value": float(value)}
|
|
)
|