-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
66 lines (55 loc) · 2.32 KB
/
button.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
from .const import DOMAIN
from .modbus_data_coordinator import ModbusDataCoordinator, ModbusInfo
from homeassistant.components.button import ButtonEntity
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.entity import EntityCategory
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities) -> None:
device: dr.DeviceEntry = hass.data[DOMAIN][entry.entry_id].get("device")
coordinator: ModbusDataCoordinator = hass.data[DOMAIN][entry.entry_id].get("coordinator")
sensors = [
ResetErrorButton(coordinator, device),
]
async_add_entities(sensors, update_before_add=False)
class PowerboxButton(CoordinatorEntity, ButtonEntity, ModbusInfo):
def __init__(self, coordinator: ModbusDataCoordinator, device: dr.DeviceEntry):
self._device = device
self._attr_unique_id = f"{self._device.name.lower()}_{self.id}"
self._attr_name = self.name
self.entity_id = f"button.{self._attr_unique_id}"
super().__init__(coordinator)
@property
def device_info(self) -> DeviceInfo:
return {
"name": self._device.name,
"manufacturer": self._device.manufacturer,
"model": self._device.model,
"identifiers": self._device.identifiers,
"connections": self._device.connections,
"sw_version": self._device.sw_version,
"hw_version": self._device.hw_version,
}
def press(self) -> None:
"""Press the button."""
raise NotImplementedError()
class ResetErrorButton(PowerboxButton):
def __init__(self, coordinator: ModbusDataCoordinator, device: dr.DeviceEntry):
self._attr_entity_category = EntityCategory.CONFIG
super().__init__(coordinator, device)
@property
def id(self):
return "reset_error"
@property
def name(self):
return "Fehlerspeicher zurücksetzen"
@property
def icon(self):
return "mdi:restart-alert"
@property
def address(self) -> int:
return 559
def press(self):
self.coordinator.write(self.address, 1)