Skip to content

Commit 01743eb

Browse files
committed
feat: add battery capacity sensor
1 parent 79c6427 commit 01743eb

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

custom_components/solis_cloud_control/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .api import SolisCloudControlApiClient
1010
from .const import CONF_INVERTER_SN
1111

12-
PLATFORMS: list[Platform] = [Platform.SELECT, Platform.TEXT, Platform.NUMBER, Platform.SWITCH]
12+
PLATFORMS: list[Platform] = [Platform.SELECT, Platform.TEXT, Platform.NUMBER, Platform.SWITCH, Platform.SENSOR]
1313

1414

1515
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

custom_components/solis_cloud_control/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
CID_BATTERY_RESERVE_SOC = 157
1515
CID_BATTERY_OVER_DISCHARGE_SOC = 158
1616
CID_BATTERY_FORCE_CHARGE_SOC = 160
17+
CID_BATTERY_CAPACITY = 172
1718
CID_STORAGE_MODE = 636
1819
CID_CHARGE_SLOT1_SWITCH = 5916
1920
CID_CHARGE_SLOT1_TIME = 5946

custom_components/solis_cloud_control/coordinator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from custom_components.solis_cloud_control.api import SolisCloudControlApiClient, SolisCloudControlApiError
99
from custom_components.solis_cloud_control.const import (
10+
CID_BATTERY_CAPACITY,
1011
CID_BATTERY_FORCE_CHARGE_SOC,
1112
CID_BATTERY_OVER_DISCHARGE_SOC,
1213
CID_BATTERY_RESERVE_SOC,
@@ -32,6 +33,7 @@
3233
CID_BATTERY_RESERVE_SOC,
3334
CID_BATTERY_FORCE_CHARGE_SOC,
3435
CID_BATTERY_OVER_DISCHARGE_SOC,
36+
CID_BATTERY_CAPACITY,
3537
CID_CHARGE_SLOT1_CURRENT,
3638
CID_CHARGE_SLOT1_SOC,
3739
CID_CHARGE_SLOT1_SWITCH,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity, SensorEntityDescription
2+
from homeassistant.config_entries import ConfigEntry
3+
from homeassistant.const import PERCENTAGE
4+
from homeassistant.core import HomeAssistant
5+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
6+
7+
from .const import CID_BATTERY_CAPACITY
8+
from .coordinator import SolisCloudControlCoordinator
9+
from .entity import SolisCloudControlEntity
10+
11+
12+
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: # noqa: ARG001
13+
coordinator: SolisCloudControlCoordinator = entry.runtime_data
14+
async_add_entities(
15+
[
16+
BatteryCapacitySensor(
17+
coordinator=coordinator,
18+
entity_description=SensorEntityDescription(
19+
key="battery_capacity",
20+
name="Battery Capacity",
21+
icon="mdi:battery",
22+
),
23+
cid=CID_BATTERY_CAPACITY,
24+
),
25+
]
26+
)
27+
28+
29+
class BatteryCapacitySensor(SolisCloudControlEntity, SensorEntity):
30+
def __init__(
31+
self, coordinator: SolisCloudControlCoordinator, entity_description: SensorEntityDescription, cid: int
32+
) -> None:
33+
super().__init__(coordinator, entity_description, cid)
34+
self._attr_device_class = SensorDeviceClass.BATTERY
35+
self._attr_native_unit_of_measurement = PERCENTAGE
36+
37+
@property
38+
def native_value(self) -> float | None:
39+
if not self.coordinator.data:
40+
return None
41+
42+
value_str = self.coordinator.data.get(self.cid)
43+
return float(value_str) if value_str is not None else None

custom_components/solis_cloud_control/switch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def async_setup_entry(
2121
entity_description=SwitchEntityDescription(
2222
key="slot1_charge_switch",
2323
name="Slot1 Charge",
24-
icon="mdi:battery-charging",
24+
icon="mdi:battery-plus-outline",
2525
),
2626
cid=CID_CHARGE_SLOT1_SWITCH,
2727
),
@@ -30,7 +30,7 @@ async def async_setup_entry(
3030
entity_description=SwitchEntityDescription(
3131
key="slot1_discharge_switch",
3232
name="Slot1 Discharge",
33-
icon="mdi:battery-minus",
33+
icon="mdi:battery-minus-outline",
3434
),
3535
cid=CID_DISCHARGE_SLOT1_SWITCH,
3636
),

0 commit comments

Comments
 (0)