Skip to content

Commit

Permalink
Merge pull request #106 from klejejs/feat/add-debug-file-generation-a…
Browse files Browse the repository at this point in the history
…ction

Add action for generating debug files
  • Loading branch information
klejejs authored Dec 5, 2024
2 parents be5fb0a + 3651f88 commit b4f48f9
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
11 changes: 9 additions & 2 deletions custom_components/thermia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.typing import ConfigType
from ThermiaOnlineAPI import Thermia

from .const import CONF_PASSWORD, CONF_USERNAME, DOMAIN
from .const import CONF_PASSWORD, CONF_USERNAME, DEBUG_ACTION_NAME, DOMAIN
from .coordinator import ThermiaDataUpdateCoordinator
from .services import ThermiaServicesSetup

PLATFORMS: list[str] = ["binary_sensor", "sensor", "switch", "water_heater"]

Expand Down Expand Up @@ -49,6 +50,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
config_entry.async_on_unload(
config_entry.add_update_listener(async_reload_entry))

ThermiaServicesSetup(hass, coordinator)

return True


Expand All @@ -63,6 +66,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
]
)
)

if hass.services.has_service(DOMAIN, DEBUG_ACTION_NAME):
hass.services.async_remove(DOMAIN, DEBUG_ACTION_NAME)

if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

Expand Down
3 changes: 3 additions & 0 deletions custom_components/thermia/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
MDI_INFORMATION_OUTLINE_ICON = "mdi:information-outline"
MDI_TIMER_COG_OUTLINE_ICON = "mdi:timer-cog-outline"
MDI_TEMPERATURE_ICON = "mdi:thermometer"

DEBUG_ACTION_NAME = "debug"
DEFAULT_DEBUG_FILENAME = "thermia_debug.txt"
92 changes: 92 additions & 0 deletions custom_components/thermia/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Thermia services setup class."""

from __future__ import annotations

import logging

from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.service import async_extract_entity_ids
from homeassistant.helpers.template import device_attr

from .const import DEBUG_ACTION_NAME, DEFAULT_DEBUG_FILENAME, DOMAIN
from .coordinator import ThermiaDataUpdateCoordinator

_LOGGER = logging.getLogger(__name__)


class ThermiaServicesSetup:
"""Set up Thermia services."""

def __init__(self, hass: HomeAssistant, coordinator: ThermiaDataUpdateCoordinator):
self.hass = hass
self.coordinator = coordinator

self.setup_services()

def setup_services(self):
"""Set up Thermia services."""
self.hass.services.async_register(
DOMAIN, DEBUG_ACTION_NAME, self.async_handle_heat_pump_debug
)

async def async_handle_heat_pump_debug(self, call: ServiceCall):
"""Handle debug service call."""
entity_ids = await async_extract_entity_ids(self.hass, call)

entity_ids = list(
filter(
lambda entity_id: entity_id.startswith("water_heater."),
entity_ids
)
)

if len(entity_ids) == 0 or len(entity_ids) > 1:
raise ServiceValidationError(
"Exactly one water heater entity should be provided"
)

entity_id = entity_ids[0]

device_identifiers = device_attr(self.hass, entity_id, "identifiers")

if device_identifiers is None:
raise ServiceValidationError(
f"Cannot find device identifiers for entity {entity_id}"
)

device_identifiers = list(device_identifiers)

if len(device_identifiers) != 1 and len(device_identifiers[0]) != 2:
raise ServiceValidationError(
f"Invalid device identifiers for entity {entity_id}"
)

heat_pump_id = device_identifiers[0][1]

heat_pump = next(
(
heat_pump
for heat_pump in self.coordinator.data.heat_pumps
if heat_pump.id == heat_pump_id
),
None,
)

if heat_pump is None:
raise ServiceValidationError(
"Cannot find heat pump by unique_id"
)

debug_data = await self.hass.async_add_executor_job(lambda: heat_pump.debug())

def create_debug_file():
with open(DEFAULT_DEBUG_FILENAME, "w", encoding="utf-8") as report_file:
report_file.write(debug_data)

_LOGGER.info(
f"Thermia debug file was created and data was written to {
DEFAULT_DEBUG_FILENAME}"
)

await self.hass.async_add_executor_job(create_debug_file)
7 changes: 7 additions & 0 deletions custom_components/thermia/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
debug:
name: Generate debug report
description: Create a debug report for your Thermia heat pump. This can be useful for troubleshooting and should be added to a GitHub issue when asking for help.
target:
entity:
integration: thermia
domain: water_heater

0 comments on commit b4f48f9

Please sign in to comment.