forked from INDEVOLT/homeassistant-indevolt
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathswitch.py
More file actions
144 lines (118 loc) · 4.29 KB
/
switch.py
File metadata and controls
144 lines (118 loc) · 4.29 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
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
"""Switch platform for Indevolt integration."""
from __future__ import annotations
from dataclasses import dataclass, field
import logging
from typing import Final
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import IndevoltCoordinator, IndevoltConfigEntry
from .entity import IndevoltEntity
_LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 0
@dataclass(frozen=True, kw_only=True)
class IndevoltSwitchEntityDescription(SwitchEntityDescription):
"""Custom entity description class for Indevolt switch entities."""
read_key: str
write_key: str
on_value: int = 1
off_value: int = 0
generation: list[int] = field(default_factory=lambda: [1, 2])
SWITCHES: Final = (
IndevoltSwitchEntityDescription(
key="grid_charging",
translation_key="grid_charging",
generation=[2],
read_key="2618",
write_key="1143",
on_value = 1001,
off_value = 1000,
),
IndevoltSwitchEntityDescription(
key="light",
translation_key="light",
generation=[2],
read_key="7171",
write_key="7265",
),
IndevoltSwitchEntityDescription(
key="bypass",
translation_key="bypass",
generation=[2],
read_key="680",
write_key="7266",
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: IndevoltConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the switch platform for Indevolt."""
coordinator = entry.runtime_data
device_gen = coordinator.device_info_data.get("generation", 1)
# Initialize switch values (first fetch)
initial_keys = [
description.read_key
for description in SWITCHES
if device_gen in description.generation
]
coordinator.set_initial_sensor_keys(initial_keys)
await coordinator.async_config_entry_first_refresh()
# Add switch entities based on device generation
async_add_entities(
[
IndevoltSwitchEntity(coordinator=coordinator, description=description)
for description in SWITCHES
if device_gen in description.generation
]
)
class IndevoltSwitchEntity(IndevoltEntity, SwitchEntity):
"""Represents a switch entity for Indevolt devices."""
entity_description: IndevoltSwitchEntityDescription
def __init__(
self,
coordinator: IndevoltCoordinator,
description: IndevoltSwitchEntityDescription,
) -> None:
"""Initialize the Indevolt switch entity."""
super().__init__(coordinator, context=description.read_key)
self.entity_description = description
self._attr_unique_id = f"{self.serial_number}_{description.key}"
@property
def is_on(self) -> bool | None:
"""Return true if switch is on."""
if not self.coordinator.data:
return None
raw_value = self.coordinator.data.get(self.entity_description.read_key)
if raw_value is None:
return None
# If on_value is specified, check for exact match
if self.entity_description.on_value is not None:
return raw_value == self.entity_description.on_value
# Otherwise, ON means anything except off_value
return raw_value != self.entity_description.off_value
async def async_turn_on(self, **kwargs) -> None:
"""Turn the switch on."""
try:
await self.coordinator.async_push_data(self.entity_description.write_key, 1)
await self.coordinator.async_request_refresh()
except Exception as err:
_LOGGER.error(
"Failed to turn on %s: %s",
self.entity_description.key,
err,
)
raise
async def async_turn_off(self, **kwargs) -> None:
"""Turn the switch off."""
try:
await self.coordinator.async_push_data(self.entity_description.write_key, 0)
await self.coordinator.async_request_refresh()
except Exception as err:
_LOGGER.error(
"Failed to turn off %s: %s",
self.entity_description.key,
err,
)
raise