Skip to content

Commit 23aa966

Browse files
authored
Update switch.py
1 parent 82d7bc5 commit 23aa966

File tree

1 file changed

+16
-76
lines changed
  • custom_components/apsystems_ecu_reader

1 file changed

+16
-76
lines changed
Lines changed: 16 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Support for APsystems ECU switches."""
2+
13
import logging
24

35
from homeassistant.components.switch import SwitchEntity
@@ -8,91 +10,26 @@
810

911
from .const import (
1012
DOMAIN,
11-
RELOAD_ICON,
1213
POWER_ICON,
1314
)
1415

1516
_LOGGER = logging.getLogger(__name__)
1617

17-
async def async_setup_entry(hass, config, add_entities, discovery_info=None):
18-
"""Set up the APsystems ECU switches."""
18+
async def async_setup_entry(hass, _, add_entities):
19+
"""Set up the APsystems Inverter switches."""
1920

2021
ecu = hass.data[DOMAIN].get("ecu")
2122
coordinator = hass.data[DOMAIN].get("coordinator")
22-
switches = [
23-
APsystemsECUQuerySwitch(coordinator, ecu, "query_device",
24-
label="Query Device", icon=RELOAD_ICON),
25-
]
23+
switches = []
2624
inverters = coordinator.data.get("inverters", {})
2725
for uid, inv_data in inverters.items():
2826
switches.append(APsystemsECUInverterSwitch(coordinator, ecu, uid, inv_data))
2927
add_entities(switches)
3028

31-
class APsystemsECUQuerySwitch(CoordinatorEntity, SwitchEntity):
32-
"""Representation of a switch."""
33-
def __init__(self, coordinator, ecu, field, label=None, icon=None):
34-
super().__init__(coordinator)
35-
self.coordinator = coordinator
36-
self._ecu = ecu
37-
self._field = field
38-
self._label = label
39-
if not label:
40-
self._label = field
41-
self._icon = icon
42-
self._name = f"ECU {self._ecu.ecu.ecu_id} {self._label}"
43-
self._state = True
44-
45-
@property
46-
def unique_id(self):
47-
"""Return the unique id of the switch."""
48-
return f"{self._ecu.ecu.ecu_id}_{self._field}"
49-
50-
@property
51-
def name(self):
52-
"""Return the name of the switch."""
53-
return self._name
54-
55-
@property
56-
def icon(self):
57-
"""Return the icon to use in the frontend, if any."""
58-
return self._icon
59-
60-
@property
61-
def device_info(self):
62-
"""Return the device info."""
63-
parent = f"ecu_{self._ecu.ecu.ecu_id}"
64-
return {
65-
"identifiers": {
66-
(DOMAIN, parent),
67-
}
68-
}
69-
70-
@property
71-
def entity_category(self):
72-
"""Return the category of the entity."""
73-
return EntityCategory.CONFIG
74-
75-
@property
76-
def is_on(self):
77-
"""Return the state of the switch."""
78-
return self._ecu.is_querying
79-
80-
def turn_off(self, *args, **kwargs):
81-
"""Turn off the switch."""
82-
self._ecu.set_querying_state(False)
83-
self._state = False
84-
self.schedule_update_ha_state()
85-
86-
def turn_on(self, *args, **kwargs):
87-
"""Turn on the switch."""
88-
self._ecu.set_querying_state(True)
89-
self._state = True
90-
self.schedule_update_ha_state()
91-
9229

9330
class APsystemsECUInverterSwitch(CoordinatorEntity, SwitchEntity):
9431
"""Representation of a switch for an individual inverter."""
95-
def __init__(self, coordinator, ecu, uid, inv_data, icon=None):
32+
def __init__(self, coordinator, ecu, uid, inv_data):
9633
super().__init__(coordinator)
9734
self.coordinator = coordinator
9835
self._ecu = ecu
@@ -101,7 +38,6 @@ def __init__(self, coordinator, ecu, uid, inv_data, icon=None):
10138
self._name = f"Inverter {uid} On/Off"
10239
self._state = True
10340

104-
10541
@property
10642
def unique_id(self):
10743
"""Return the unique id of the switch."""
@@ -114,17 +50,21 @@ def name(self):
11450

11551
@property
11652
def icon(self):
117-
"""Return the icon to use in the frontend"""
53+
"""Return the icon to use in the UI."""
11854
return POWER_ICON
11955

12056
@property
12157
def device_info(self):
12258
"""Return the device info."""
123-
parent = f"ecu_{self._ecu.ecu.ecu_id}"
59+
parent = f"inverter_{self._uid}"
12460
return {
12561
"identifiers": {
12662
(DOMAIN, parent),
127-
}
63+
},
64+
"name": f"Inverter {self._uid}",
65+
"manufacturer": "APsystems",
66+
"model": self._inv_data.get("model", "Unknown"),
67+
"via_device": (DOMAIN, f"ecu_{self._ecu.ecu.ecu_id}"),
12868
}
12969

13070
@property
@@ -134,17 +74,17 @@ def entity_category(self):
13474

13575
@property
13676
def is_on(self):
137-
""" Return the state of the switch """
77+
"""Return the state of the inverter switch."""
13878
return self._state
13979

14080
def turn_off(self, *args, **kwargs):
141-
"""Turn off the switch."""
81+
"""Turn off the inverter switch."""
14282
self._ecu.set_inverter_state(self._uid, False)
14383
self._state = False
14484
self.schedule_update_ha_state()
14585

14686
def turn_on(self, *args, **kwargs):
147-
"""Turn on the switch."""
87+
"""Turn on the inverter switch."""
14888
self._ecu.set_inverter_state(self._uid, True)
14989
self._state = True
15090
self.schedule_update_ha_state()

0 commit comments

Comments
 (0)