2023-04-11 20:08:47 +00:00
|
|
|
import logging
|
|
|
|
|
2023-04-25 21:39:18 +00:00
|
|
|
import pkg_resources
|
2023-05-11 21:59:01 +00:00
|
|
|
|
|
|
|
from homeassistant.components import persistent_notification
|
2023-03-03 17:23:30 +00:00
|
|
|
from homeassistant.components.button import ButtonEntityDescription, ButtonEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-05-10 16:13:05 +00:00
|
|
|
from homeassistant.const import EntityCategory
|
2023-04-10 05:09:54 +00:00
|
|
|
from pyhon import Hon
|
|
|
|
from pyhon.appliance import HonAppliance
|
2023-03-03 17:23:30 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .hon import HonCoordinator, HonEntity
|
|
|
|
|
2023-04-11 20:08:47 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2023-03-03 17:23:30 +00:00
|
|
|
BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = {
|
2023-04-16 11:55:08 +00:00
|
|
|
"IH": (
|
2023-04-15 02:27:40 +00:00
|
|
|
ButtonEntityDescription(
|
|
|
|
key="startProgram",
|
|
|
|
name="Start Program",
|
|
|
|
icon="mdi:pot-steam",
|
2023-04-23 00:01:14 +00:00
|
|
|
translation_key="induction_hob",
|
2023-04-15 02:27:40 +00:00
|
|
|
),
|
|
|
|
),
|
2023-03-03 17:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
|
2023-04-10 05:09:54 +00:00
|
|
|
hon: Hon = hass.data[DOMAIN][entry.unique_id]
|
2023-03-03 17:23:30 +00:00
|
|
|
coordinators = hass.data[DOMAIN]["coordinators"]
|
|
|
|
appliances = []
|
2023-04-10 05:09:54 +00:00
|
|
|
for device in hon.appliances:
|
2023-04-15 20:03:50 +00:00
|
|
|
if device.unique_id in coordinators:
|
|
|
|
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
|
2023-03-03 17:23:30 +00:00
|
|
|
else:
|
|
|
|
coordinator = HonCoordinator(hass, device)
|
2023-04-15 20:03:50 +00:00
|
|
|
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
|
2023-03-03 17:23:30 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
2023-03-08 22:00:55 +00:00
|
|
|
if descriptions := BUTTONS.get(device.appliance_type):
|
2023-03-03 17:23:30 +00:00
|
|
|
for description in descriptions:
|
2023-03-05 18:19:52 +00:00
|
|
|
if not device.commands.get(description.key):
|
|
|
|
continue
|
2023-04-10 17:51:16 +00:00
|
|
|
appliances.extend(
|
|
|
|
[HonButtonEntity(hass, coordinator, entry, device, description)]
|
2023-03-03 17:23:30 +00:00
|
|
|
)
|
2023-04-11 20:08:47 +00:00
|
|
|
appliances.extend([HonFeatureRequestButton(hass, coordinator, entry, device)])
|
2023-03-03 17:23:30 +00:00
|
|
|
|
|
|
|
async_add_entities(appliances)
|
|
|
|
|
|
|
|
|
|
|
|
class HonButtonEntity(HonEntity, ButtonEntity):
|
2023-04-10 17:51:16 +00:00
|
|
|
def __init__(
|
|
|
|
self, hass, coordinator, entry, device: HonAppliance, description
|
|
|
|
) -> None:
|
2023-03-03 17:23:30 +00: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()
|
2023-04-11 20:08:47 +00:00
|
|
|
|
2023-05-08 17:17:08 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
2023-05-13 20:09:41 +00:00
|
|
|
return (
|
|
|
|
super().available
|
|
|
|
and self._device.get("remoteCtrValid") == "1"
|
|
|
|
and self._device.get("attributes.lastConnEvent.category") != "DISCONNECTED"
|
|
|
|
)
|
2023-05-08 17:17:08 +00:00
|
|
|
|
2023-04-11 20:08:47 +00:00
|
|
|
|
|
|
|
class HonFeatureRequestButton(HonEntity, ButtonEntity):
|
|
|
|
def __init__(self, hass, coordinator, entry, device: HonAppliance) -> None:
|
|
|
|
super().__init__(hass, entry, coordinator, device)
|
2023-05-11 21:59:01 +00:00
|
|
|
self._hass = hass
|
2023-04-11 20:08:47 +00:00
|
|
|
|
|
|
|
self._device = device
|
|
|
|
self._attr_unique_id = f"{super().unique_id}_log_device_info"
|
|
|
|
self._attr_icon = "mdi:information"
|
2023-05-11 21:59:01 +00:00
|
|
|
self._attr_name = "Show Device Info"
|
2023-04-11 20:08:47 +00:00
|
|
|
self._attr_entity_category = EntityCategory.DIAGNOSTIC
|
|
|
|
self._attr_entity_registry_enabled_default = False
|
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
2023-04-25 21:39:18 +00:00
|
|
|
pyhon_version = pkg_resources.get_distribution("pyhon").version
|
2023-05-09 10:41:41 +00:00
|
|
|
info = f"Device Info:\n{self._device.diagnose()}pyhOnVersion: {pyhon_version}"
|
2023-05-11 21:59:01 +00:00
|
|
|
title = f"{self._device.nick_name} Device Info"
|
|
|
|
persistent_notification.create(self._hass, f"```\n```{info}```\n```", title)
|
|
|
|
_LOGGER.info(info.replace(" ", "\u200B "))
|