Skip to content

Commit cf16f9a

Browse files
authored
Add files via upload
Preparing for release 2.6.1
1 parent 3780626 commit cf16f9a

File tree

5 files changed

+74
-87
lines changed

5 files changed

+74
-87
lines changed

custom_components/apsystems_ecu_reader/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
_LOGGER = logging.getLogger(__name__)
3333

34-
PLATFORMS = ["sensor", "binary_sensor", "switch", "number"]
34+
PLATFORMS = ["sensor", "binary_sensor", "switch", "number", "button"]
3535

3636
class ECUREADER:
3737
"""ECU Reader"""
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Button platform for APsystems ECU Reader."""
2+
3+
from homeassistant.components.button import ButtonEntity, ButtonDeviceClass
4+
from homeassistant.helpers.entity import EntityCategory
5+
from homeassistant.helpers.update_coordinator import CoordinatorEntity
6+
7+
from .const import DOMAIN, ECU_REBOOT_ICON
8+
from .gui_helpers import pers_notification
9+
10+
11+
import logging
12+
13+
_LOGGER = logging.getLogger(__name__)
14+
15+
async def async_setup_entry(hass, config_entry, async_add_entities):
16+
"""Set up the button platform."""
17+
ecu = hass.data[DOMAIN]["ecu"]
18+
async_add_entities([RebootECUButton(ecu)])
19+
20+
21+
class RebootECUButton(ButtonEntity):
22+
"""Representation of a button to reboot the ECU."""
23+
24+
def __init__(self, ecu):
25+
"""Initialize the button."""
26+
self._ecu = ecu
27+
self._attr_icon = ECU_REBOOT_ICON
28+
self._attr_name = f"ECU {ecu.ecu.ecu_id} Reboot"
29+
self._attr_unique_id = f"ECU_{ecu.ecu.ecu_id}_reboot_button"
30+
self._attr_device_class = ButtonDeviceClass.RESTART
31+
32+
@property
33+
def device_info(self):
34+
"""Return the device info for the ECU."""
35+
return {
36+
"identifiers": {
37+
(DOMAIN, f"ecu_{self._ecu.ecu.ecu_id}"),
38+
},
39+
"name": f"ECU {self._ecu.ecu.ecu_id}",
40+
"manufacturer": "APsystems",
41+
"model": self._ecu.ecu.firmware,
42+
"sw_version": self._ecu.ecu.firmware,
43+
}
44+
45+
@property
46+
def entity_category(self):
47+
"""Return the category of the entity."""
48+
return EntityCategory.DIAGNOSTIC
49+
50+
async def async_press(self):
51+
"""Handle the button press."""
52+
try:
53+
response = await self._ecu.reboot_ecu()
54+
pers_notification(
55+
self.hass,
56+
f"Rebooted ECU {self._ecu.ecu.ecu_id}"
57+
)
58+
except Exception as e:
59+
pers_notification(
60+
self.hass,
61+
f"Failed to reboot ECU: {e}"
62+
)

custom_components/apsystems_ecu_reader/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"iot_class": "local_polling",
1010
"issue_tracker": "https://github.com/haedwin/homeassistant-apsystems_ecu_reader/issues",
1111
"loggers": ["custom_components.apsystems_ecu_reader"],
12-
"version": "2.6.0"
12+
"version": "2.6.1"
1313
}

custom_components/apsystems_ecu_reader/number.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,29 @@ def __init__(self, coordinator, ecu, inverter_id, inverter_data):
2424
"""Initialize the number entity."""
2525
super().__init__(coordinator)
2626
self._ecu = ecu
27-
self.inverter_id = inverter_id
28-
self.inverter_data = inverter_data
27+
self._uid = inverter_id
28+
self._inv_data = inverter_data
2929
self._attr_name = f"Inverter {inverter_id} Maxpwr"
3030
self._attr_unique_id = f"{DOMAIN}_inverter_{inverter_id}_maxpwr"
3131
self._attr_native_min_value = 20
3232
self._attr_native_max_value = 500
3333
self._attr_native_step = 1
34-
self._attr_native_value = inverter_data.get("number_value", 0)
34+
self._attr_native_value = self._inv_data.get("number_value", 0)
3535
self._attr_device_class = "power" # Set device class
3636
self._attr_mode = "slider" # Set mode to slider
3737

3838
@property
3939
def device_info(self):
4040
"""Return the device info."""
41+
parent = f"inverter_{self._uid}"
4142
return {
4243
"identifiers": {
43-
(DOMAIN, f"ecu_{self._ecu.ecu.ecu_id}"),
44+
(DOMAIN, parent),
4445
},
45-
"name": f"ECU {self._ecu.ecu.ecu_id}",
46+
"name": f"Inverter {self._uid}",
4647
"manufacturer": "APsystems",
47-
"model": self._ecu.ecu.firmware,
48-
"sw_version": self._ecu.ecu.firmware,
48+
"model": self._inv_data.get("model", "Unknown"),
49+
"via_device": (DOMAIN, f"ecu_{self._ecu.ecu.ecu_id}"),
4950
}
5051

5152
@property
@@ -55,7 +56,7 @@ def entity_category(self):
5556

5657
async def async_set_native_value(self, value: float):
5758
"""Update the current value."""
58-
await self._ecu.set_inverter_max_power(self.inverter_id, value)
59+
await self._ecu.set_inverter_max_power(self._uid, value)
5960
self._attr_native_value = value
6061
self.async_write_ha_state()
6162

custom_components/apsystems_ecu_reader/switch.py

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from homeassistant.helpers.restore_state import RestoreEntity
99
from homeassistant.helpers.update_coordinator import CoordinatorEntity
1010

11-
from .const import DOMAIN, POWER_ICON, ECU_REBOOT_ICON
11+
from .const import DOMAIN, POWER_ICON
1212
from .gui_helpers import pers_notification
1313

1414
_LOGGER = logging.getLogger(__name__)
@@ -22,7 +22,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
2222
for uid, inv_data in inverters.items():
2323
switches.append(APsystemsECUInverterSwitch(coordinator, ecu, uid, inv_data))
2424
switches.append(APsystemsZeroExportSwitch(coordinator, ecu))
25-
switches.append(RebootECUSwitch(ecu))
2625
switches.append(APsystemsAllInvertersSwitch(coordinator, ecu))
2726
async_add_entities(switches)
2827

@@ -198,78 +197,3 @@ async def async_turn_off(self, **kwargs):
198197
self.async_write_ha_state()
199198
except Exception as e:
200199
_LOGGER.error("Failed to turn off zero export: %s", e)
201-
202-
class RebootECUSwitch(SwitchEntity, RestoreEntity):
203-
"""Momentary switch to reboot the ECU."""
204-
205-
def __init__(self, ecu):
206-
"""Initialize the switch."""
207-
self._ecu = ecu
208-
self._state = False
209-
self._name = f"ECU {ecu.ecu.ecu_id} Reboot Switch"
210-
self._unique_id = f"ECU_{ecu.ecu.ecu_id}_reboot_switch"
211-
212-
@property
213-
def unique_id(self):
214-
"""Return the unique ID of the switch."""
215-
return self._unique_id
216-
217-
@property
218-
def name(self):
219-
"""Return the name of the switch."""
220-
return self._name
221-
222-
@property
223-
def icon(self):
224-
"""Return the icon to use in the UI."""
225-
return ECU_REBOOT_ICON
226-
227-
@property
228-
def device_info(self):
229-
"""Return the device info for the ECU."""
230-
return {
231-
"identifiers": {
232-
(DOMAIN, f"ecu_{self._ecu.ecu.ecu_id}"),
233-
},
234-
"name": f"ECU {self._ecu.ecu.ecu_id}",
235-
"manufacturer": "APsystems",
236-
"model": self._ecu.ecu.firmware,
237-
"sw_version": self._ecu.ecu.firmware,
238-
}
239-
240-
@property
241-
def entity_category(self):
242-
"""Return the category of the entity."""
243-
return EntityCategory.DIAGNOSTIC
244-
245-
@property
246-
def is_on(self):
247-
"""Return the state of the switch."""
248-
return self._state
249-
250-
async def async_turn_on(self, **kwargs):
251-
"""Reboot the ECU."""
252-
self._state = True
253-
self.async_write_ha_state()
254-
255-
try:
256-
await self._ecu.reboot_ecu()
257-
pers_notification(
258-
self.hass,
259-
f"Rebooted ECU {self._ecu.ecu.ecu_id}"
260-
)
261-
except Exception as e:
262-
_LOGGER.error("Failed to reboot ECU: %s", e)
263-
pers_notification(
264-
self.hass,
265-
f"Failed to reboot ECU: {e}"
266-
)
267-
268-
# Turn off the switch after 2 seconds
269-
await asyncio.sleep(2)
270-
self._state = False
271-
self.async_write_ha_state()
272-
273-
async def async_turn_off(self, **kwargs):
274-
"""Handle turning the switch off."""
275-
pass # Momentary switch, no manual turn-off

0 commit comments

Comments
 (0)