hOn/custom_components/hon/button.py

59 lines
2 KiB
Python
Raw Normal View History

2023-03-03 18:23:30 +01:00
from homeassistant.components.button import ButtonEntityDescription, ButtonEntity
from homeassistant.config_entries import ConfigEntry
2023-04-10 07:09:54 +02:00
from pyhon import Hon
from pyhon.appliance import HonAppliance
2023-03-03 18:23:30 +01:00
from .const import DOMAIN
from .hon import HonCoordinator, HonEntity
BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = {
2023-04-08 06:15:33 +02:00
"OV": (
2023-04-07 13:52:55 +02:00
ButtonEntityDescription(
key="startProgram",
name="Start Program",
icon="mdi:power-cycle",
),
ButtonEntityDescription(
key="stopProgram",
name="Stop Program",
icon="mdi:power-off",
),
)
2023-03-03 18:23:30 +01:00
}
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
2023-04-10 07:09:54 +02:00
hon: Hon = hass.data[DOMAIN][entry.unique_id]
2023-03-03 18:23:30 +01:00
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
2023-04-10 07:09:54 +02:00
for device in hon.appliances:
2023-03-03 18:23:30 +01:00
if device.mac_address in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.mac_address]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.mac_address] = coordinator
await coordinator.async_config_entry_first_refresh()
2023-03-08 23:00:55 +01:00
if descriptions := BUTTONS.get(device.appliance_type):
2023-03-03 18:23:30 +01:00
for description in descriptions:
2023-03-05 19:19:52 +01:00
if not device.commands.get(description.key):
continue
2023-03-03 18:23:30 +01:00
appliances.extend([
HonButtonEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
class HonButtonEntity(HonEntity, ButtonEntity):
2023-04-10 07:09:54 +02:00
def __init__(self, hass, coordinator, entry, device: HonAppliance, description) -> None:
2023-03-03 18:23:30 +01:00
super().__init__(hass, entry, coordinator, device)
self._coordinator = coordinator
self._device = device
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
async def async_press(self) -> None:
await self._device.commands[self.entity_description.key].send()