forked from wlcrs/huawei_solar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.py
145 lines (108 loc) · 4.88 KB
/
switch.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""This component provides switch entities for Huawei Solar."""
from __future__ import annotations
from dataclasses import dataclass
import logging
from typing import Generic, TypeVar
from huawei_solar import HuaweiSolarBridge, register_names as rn, register_values as rv
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HuaweiSolarEntity, HuaweiSolarUpdateCoordinator
from .const import CONF_ENABLE_PARAMETER_CONFIGURATION, DATA_UPDATE_COORDINATORS, DOMAIN
_LOGGER = logging.getLogger(__name__)
T = TypeVar("T")
@dataclass
class HuaweiSolarSwitchEntityDescription(Generic[T], SwitchEntityDescription):
"""Huawei Solar Switch Entity Description."""
ENERGY_STORAGE_SWITCH_DESCRIPTIONS: tuple[HuaweiSolarSwitchEntityDescription, ...] = (
HuaweiSolarSwitchEntityDescription(
key=rn.STORAGE_CHARGE_FROM_GRID_FUNCTION,
name="Charge From Grid",
icon="mdi:battery-charging-50",
entity_category=EntityCategory.CONFIG,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Huawei Solar Switch Entities Setup."""
if not entry.data.get(CONF_ENABLE_PARAMETER_CONFIGURATION, False):
_LOGGER.info("Skipping switch setup, as parameter configuration is not enabled")
return
update_coordinators = hass.data[DOMAIN][entry.entry_id][
DATA_UPDATE_COORDINATORS
] # type: list[HuaweiSolarUpdateCoordinator]
# When more than one inverter is present, then we suffix all sensors with '#1', '#2', ...
# The order for these suffixes is the order in which the user entered the slave-ids.
must_append_inverter_suffix = len(update_coordinators) > 1
entities_to_add: list[SwitchEntity] = []
for idx, update_coordinator in enumerate(update_coordinators):
slave_entities: list[HuaweiSolarSwitchEntity] = []
bridge = update_coordinator.bridge
device_infos = update_coordinator.device_infos
if bridge.battery_1_type != rv.StorageProductModel.NONE:
assert device_infos["connected_energy_storage"]
for entity_description in ENERGY_STORAGE_SWITCH_DESCRIPTIONS:
slave_entities.append(
await HuaweiSolarSwitchEntity.create(
bridge,
entity_description,
device_infos["connected_energy_storage"],
)
)
else:
_LOGGER.debug(
"No battery detected on slave %s. Skipping energy storage switch entities",
bridge.slave_id,
)
# Add suffix if multiple inverters are present
if must_append_inverter_suffix:
for entity in slave_entities:
entity.add_name_suffix(f" #{idx+1}")
entities_to_add.extend(slave_entities)
async_add_entities(entities_to_add)
class HuaweiSolarSwitchEntity(HuaweiSolarEntity, SwitchEntity):
"""Huawei Solar Switch Entity."""
entity_description: HuaweiSolarSwitchEntityDescription
def __init__(
self,
bridge: HuaweiSolarBridge,
description: HuaweiSolarSwitchEntityDescription,
device_info: DeviceInfo,
initial_value: bool,
) -> None:
"""Huawei Solar Switch Entity constructor.
Do not use directly. Use `.create` instead!
"""
self.bridge = bridge
self.entity_description = description
self._attr_device_info = device_info
self._attr_unique_id = f"{bridge.serial_number}_{description.key}"
self._attr_is_on = initial_value
@classmethod
async def create(
cls,
bridge: HuaweiSolarBridge,
description: HuaweiSolarSwitchEntityDescription,
device_info: DeviceInfo,
):
"""Huawei Solar Number Entity constructor.
This async constructor fills in the necessary min/max values
"""
# Assumption: these values are not updated outside of HA.
# This should hold true as they typically can only be set via the Modbus-interface,
# which only allows one client at a time.
initial_value = (await bridge.client.get(description.key)).value
return cls(bridge, description, device_info, initial_value)
async def async_turn_on(self, **kwargs) -> None:
"""Turn the setting on."""
if await self.bridge.set(self.entity_description.key, True):
self._attr_is_on = True
async def async_turn_off(self, **kwargs) -> None:
"""Turn the setting off."""
if await self.bridge.set(self.entity_description.key, False):
self._attr_is_on = False