Create data archive

This commit is contained in:
Andre Basche 2023-06-25 17:33:30 +02:00
parent 4b1f500f90
commit e5e351272b
2 changed files with 33 additions and 9 deletions

View file

@ -1,18 +1,17 @@
import logging import logging
from pathlib import Path
import voluptuous as vol import voluptuous as vol
from pyhon import Hon
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.helpers import config_validation as cv, aiohttp_client from homeassistant.helpers import config_validation as cv, aiohttp_client
from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.typing import HomeAssistantType
from pyhon import Hon
from .const import DOMAIN, PLATFORMS from .const import DOMAIN, PLATFORMS
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
HON_SCHEMA = vol.Schema( HON_SCHEMA = vol.Schema(
{ {
vol.Required(CONF_EMAIL): cv.string, vol.Required(CONF_EMAIL): cv.string,
@ -29,7 +28,10 @@ CONFIG_SCHEMA = vol.Schema(
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
session = aiohttp_client.async_get_clientsession(hass) session = aiohttp_client.async_get_clientsession(hass)
hon = await Hon( hon = await Hon(
entry.data["email"], entry.data["password"], session=session entry.data["email"],
entry.data["password"],
session=session,
test_data_path=Path(hass.config.config_dir),
).create() ).create()
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.unique_id] = hon hass.data[DOMAIN][entry.unique_id] = hon

View file

@ -1,4 +1,5 @@
import logging import logging
from pathlib import Path
import pkg_resources import pkg_resources
from homeassistant.components import persistent_notification from homeassistant.components import persistent_notification
@ -61,7 +62,8 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non
entity = HonButtonEntity(hass, entry, device, description) entity = HonButtonEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh() await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity) entities.append(entity)
entities.append(HonFeatureRequestButton(hass, entry, device)) entities.append(HonDeviceInfo(hass, entry, device))
entities.append(HonDataArchive(hass, entry, device))
await entities[-1].coordinator.async_config_entry_first_refresh() await entities[-1].coordinator.async_config_entry_first_refresh()
async_add_entities(entities) async_add_entities(entities)
@ -82,21 +84,41 @@ class HonButtonEntity(HonEntity, ButtonEntity):
) )
class HonFeatureRequestButton(HonEntity, ButtonEntity): class HonDeviceInfo(HonEntity, ButtonEntity):
def __init__(self, hass, entry, device: HonAppliance) -> None: def __init__(self, hass, entry, device: HonAppliance) -> None:
super().__init__(hass, entry, device) super().__init__(hass, entry, device)
self._attr_unique_id = f"{super().unique_id}_log_device_info" self._attr_unique_id = f"{super().unique_id}_show_device_info"
self._attr_icon = "mdi:information" self._attr_icon = "mdi:information"
self._attr_name = "Show Device Info" self._attr_name = "Show Device Info"
self._attr_entity_category = EntityCategory.DIAGNOSTIC self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_entity_registry_enabled_default = False
async def async_press(self) -> None: async def async_press(self) -> None:
pyhon_version = pkg_resources.get_distribution("pyhon").version pyhon_version = pkg_resources.get_distribution("pyhon").version
info = f"{self._device.diagnose()}pyhOnVersion: {pyhon_version}" info = f"{self._device.diagnose}pyhOnVersion: {pyhon_version}"
title = f"{self._device.nick_name} Device Info" title = f"{self._device.nick_name} Device Info"
persistent_notification.create( persistent_notification.create(
self._hass, f"````\n```\n{info}\n```\n````", title self._hass, f"````\n```\n{info}\n```\n````", title
) )
_LOGGER.info(info.replace(" ", "\u200B ")) _LOGGER.info(info.replace(" ", "\u200B "))
class HonDataArchive(HonEntity, ButtonEntity):
def __init__(self, hass, entry, device: HonAppliance) -> None:
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
async def async_press(self) -> None:
path = Path(self._hass.config.config_dir) / "www"
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)