-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathbutton.py
More file actions
90 lines (73 loc) · 2.98 KB
/
button.py
File metadata and controls
90 lines (73 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Button entities for Huawei Solar."""
import logging
from huawei_solar import SUN2000Device, register_names as rn, register_values as rv
from homeassistant.components.button import ButtonEntity
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CONF_ENABLE_PARAMETER_CONFIGURATION, DATA_DEVICE_DATAS
from .types import (
HuaweiSolarConfigEntry,
HuaweiSolarDeviceData,
HuaweiSolarEntity,
HuaweiSolarInverterData,
)
from .update_coordinator import HuaweiSolarUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
entry: HuaweiSolarConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Huawei Solar Button entities Setup."""
if not entry.data.get(CONF_ENABLE_PARAMETER_CONFIGURATION):
return
device_datas: list[HuaweiSolarDeviceData] = entry.runtime_data[DATA_DEVICE_DATAS]
entities_to_add: list[ButtonEntity] = []
for ucs in device_datas:
if not isinstance(ucs, HuaweiSolarInverterData):
continue
if not ucs.connected_energy_storage:
continue
entities_to_add.append(
StopForcibleChargeButtonEntity(
ucs.device,
ucs.connected_energy_storage,
ucs.configuration_update_coordinator,
)
)
async_add_entities(entities_to_add)
class StopForcibleChargeButtonEntity(HuaweiSolarEntity, ButtonEntity):
"""Button to stop a running forcible charge or discharge."""
_attr_entity_category = EntityCategory.CONFIG
_attr_translation_key = "stop_forcible_charge"
_attr_icon = "mdi:battery-off"
def __init__(
self,
device: SUN2000Device,
device_info: DeviceInfo,
configuration_update_coordinator: HuaweiSolarUpdateCoordinator | None,
) -> None:
"""Initialize the button entity."""
self.device = device
self._attr_device_info = device_info
self._attr_unique_id = f"{device.serial_number}_stop_forcible_charge"
self._configuration_update_coordinator = configuration_update_coordinator
async def async_press(self) -> None:
"""Stop the forcible charge or discharge."""
await self.device.set(
rn.STORAGE_FORCIBLE_CHARGE_DISCHARGE_WRITE,
rv.StorageForcibleChargeDischarge.STOP,
)
await self.device.set(rn.STORAGE_FORCIBLE_DISCHARGE_POWER, 0)
await self.device.set(
rn.STORAGE_FORCED_CHARGING_AND_DISCHARGING_PERIOD,
0,
)
await self.device.set(
rn.STORAGE_FORCIBLE_CHARGE_DISCHARGE_SETTING_MODE,
rv.StorageForcibleChargeDischargeTargetMode.TIME,
)
if self._configuration_update_coordinator:
await self._configuration_update_coordinator.async_request_refresh()