2023-04-11 22:08:47 +02:00
|
|
|
import logging
|
2023-06-25 17:33:30 +02:00
|
|
|
from pathlib import Path
|
2023-04-11 22:08:47 +02:00
|
|
|
|
2023-05-11 23:59:01 +02:00
|
|
|
from homeassistant.components import persistent_notification
|
2023-03-03 18:23:30 +01:00
|
|
|
from homeassistant.components.button import ButtonEntityDescription, ButtonEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-06-19 16:47:30 +02:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2023-07-23 21:52:42 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2023-04-10 07:09:54 +02:00
|
|
|
from pyhon.appliance import HonAppliance
|
2023-03-03 18:23:30 +01:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
2023-05-25 01:30:33 +02:00
|
|
|
from .hon import HonEntity
|
2023-07-23 21:52:42 +02:00
|
|
|
from .typedefs import HonButtonType
|
2023-03-03 18:23:30 +01:00
|
|
|
|
2023-04-11 22:08:47 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2023-03-03 18:23:30 +01:00
|
|
|
BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = {
|
2023-04-16 13:55:08 +02:00
|
|
|
"IH": (
|
2023-04-15 04:27:40 +02:00
|
|
|
ButtonEntityDescription(
|
|
|
|
key="startProgram",
|
|
|
|
name="Start Program",
|
|
|
|
icon="mdi:pot-steam",
|
2023-04-23 02:01:14 +02:00
|
|
|
translation_key="induction_hob",
|
2023-04-15 04:27:40 +02:00
|
|
|
),
|
|
|
|
),
|
2023-05-21 20:51:20 +02:00
|
|
|
"REF": (
|
|
|
|
ButtonEntityDescription(
|
|
|
|
key="startProgram",
|
|
|
|
name="Program Start",
|
|
|
|
icon="mdi:play",
|
|
|
|
translation_key="start_program",
|
|
|
|
),
|
|
|
|
ButtonEntityDescription(
|
|
|
|
key="stopProgram",
|
|
|
|
name="Program Stop",
|
|
|
|
icon="mdi:stop",
|
|
|
|
translation_key="stop_program",
|
|
|
|
),
|
|
|
|
),
|
2023-03-03 18:23:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-23 21:52:42 +02:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
entities: list[HonButtonType] = []
|
2023-05-25 01:30:33 +02:00
|
|
|
for device in hass.data[DOMAIN][entry.unique_id].appliances:
|
|
|
|
for description in BUTTONS.get(device.appliance_type, []):
|
|
|
|
if not device.commands.get(description.key):
|
|
|
|
continue
|
|
|
|
entity = HonButtonEntity(hass, entry, device, description)
|
|
|
|
await entity.coordinator.async_config_entry_first_refresh()
|
|
|
|
entities.append(entity)
|
2023-06-25 17:33:30 +02:00
|
|
|
entities.append(HonDeviceInfo(hass, entry, device))
|
|
|
|
entities.append(HonDataArchive(hass, entry, device))
|
2023-05-25 01:30:33 +02:00
|
|
|
await entities[-1].coordinator.async_config_entry_first_refresh()
|
|
|
|
async_add_entities(entities)
|
2023-03-03 18:23:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HonButtonEntity(HonEntity, ButtonEntity):
|
2023-05-28 00:30:08 +02:00
|
|
|
entity_description: ButtonEntityDescription
|
2023-03-03 18:23:30 +01:00
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
|
|
|
await self._device.commands[self.entity_description.key].send()
|
2023-04-11 22:08:47 +02:00
|
|
|
|
2023-05-08 19:17:08 +02:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
2023-05-13 22:09:41 +02:00
|
|
|
return (
|
|
|
|
super().available
|
2023-06-13 00:14:51 +02:00
|
|
|
and int(self._device.get("remoteCtrValid", "1")) == 1
|
2023-05-13 22:09:41 +02:00
|
|
|
and self._device.get("attributes.lastConnEvent.category") != "DISCONNECTED"
|
|
|
|
)
|
2023-05-08 19:17:08 +02:00
|
|
|
|
2023-04-11 22:08:47 +02:00
|
|
|
|
2023-06-25 17:33:30 +02:00
|
|
|
class HonDeviceInfo(HonEntity, ButtonEntity):
|
2023-07-23 21:52:42 +02:00
|
|
|
def __init__(
|
|
|
|
self, hass: HomeAssistantType, entry: ConfigEntry, device: HonAppliance
|
|
|
|
) -> None:
|
2023-05-25 01:30:33 +02:00
|
|
|
super().__init__(hass, entry, device)
|
2023-04-11 22:08:47 +02:00
|
|
|
|
2023-06-25 17:33:30 +02:00
|
|
|
self._attr_unique_id = f"{super().unique_id}_show_device_info"
|
2023-04-11 22:08:47 +02:00
|
|
|
self._attr_icon = "mdi:information"
|
2023-05-11 23:59:01 +02:00
|
|
|
self._attr_name = "Show Device Info"
|
2023-04-11 22:08:47 +02:00
|
|
|
self._attr_entity_category = EntityCategory.DIAGNOSTIC
|
2023-06-29 22:19:29 +02:00
|
|
|
if "beta" not in self.coordinator.info.hon_version:
|
|
|
|
self._attr_entity_registry_enabled_default = False
|
2023-04-11 22:08:47 +02:00
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
2023-06-29 22:19:29 +02:00
|
|
|
versions = "versions:\n"
|
|
|
|
versions += f" hon: {self.coordinator.info.hon_version}\n"
|
|
|
|
versions += f" pyhOn: {self.coordinator.info.pyhon_version}\n"
|
|
|
|
info = f"{self._device.diagnose}{versions}"
|
2023-05-11 23:59:01 +02:00
|
|
|
title = f"{self._device.nick_name} Device Info"
|
2023-05-19 01:27:44 +02:00
|
|
|
persistent_notification.create(
|
|
|
|
self._hass, f"````\n```\n{info}\n```\n````", title
|
|
|
|
)
|
2023-05-11 23:59:01 +02:00
|
|
|
_LOGGER.info(info.replace(" ", "\u200B "))
|
2023-06-25 17:33:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HonDataArchive(HonEntity, ButtonEntity):
|
2023-07-23 21:52:42 +02:00
|
|
|
def __init__(
|
|
|
|
self, hass: HomeAssistantType, entry: ConfigEntry, device: HonAppliance
|
|
|
|
) -> None:
|
2023-06-25 17:33:30 +02:00
|
|
|
super().__init__(hass, entry, device)
|
|
|
|
|
|
|
|
self._attr_unique_id = f"{super().unique_id}_create_data_archive"
|
|
|
|
self._attr_icon = "mdi:archive-arrow-down"
|
|
|
|
self._attr_name = "Create Data Archive"
|
|
|
|
self._attr_entity_category = EntityCategory.DIAGNOSTIC
|
2023-06-29 22:19:29 +02:00
|
|
|
if "beta" not in self.coordinator.info.hon_version:
|
|
|
|
self._attr_entity_registry_enabled_default = False
|
2023-06-25 17:33:30 +02:00
|
|
|
|
|
|
|
async def async_press(self) -> None:
|
2023-07-23 21:52:42 +02:00
|
|
|
if (config_dir := self._hass.config.config_dir) is None:
|
|
|
|
raise ValueError("Missing Config Dir")
|
|
|
|
path = Path(config_dir) / "www"
|
2023-06-25 17:33:30 +02:00
|
|
|
data = await self._device.data_archive(path)
|
|
|
|
title = f"{self._device.nick_name} Data Archive"
|
|
|
|
text = (
|
|
|
|
f'<a href="/local/{data}" target="_blank">{data}</a> <br/><br/> '
|
|
|
|
f"Use this data for [GitHub Issues of Haier hOn](https://github.com/Andre0512/hon).<br/>"
|
|
|
|
f"Or add it to the [hon-test-data collection](https://github.com/Andre0512/hon-test-data)."
|
|
|
|
)
|
|
|
|
persistent_notification.create(self._hass, text, title)
|