Skip to content

Commit

Permalink
Add basic diag service
Browse files Browse the repository at this point in the history
  • Loading branch information
dala318 committed Nov 6, 2024
1 parent e36599e commit 949a009
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ I found it useful to setup a simple history graph chart comparing the values fro

Where from top to bottom my named entities are:

* nordpool_diff: duration 3 in search_length 10, accept_cost 2.0
* nordpool_diff: duration 2 in search_length 5, accept_cost 2.0 and accept_rate 0.7
* nordpool_planner: duration 3 in search_length 10, accept_cost 2.0
* nordpool_planner: duration 2 in search_length 5, accept_cost 2.0 and accept_rate 0.7
* nordpool average: just a template sensor extracting the nordpool attribute average to an entity for easier tracking and comparisons "{{ state_attr('sensor.nordpool_kwh_se3_sek_3_10_025', 'average') | float }}"
* nordpool
* nordpool_diff:
27 changes: 27 additions & 0 deletions custom_components/nordpool_planner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None:
self.low_cost_state = NordpoolPlannerState()
self.high_cost_state = NordpoolPlannerState()

def as_dict(self):
"""For diagnostics serialization."""
res = self.__dict__.copy()
for k, i in res.copy().items():
if "_number_entity" in k:
res[k] = {"id": i, "value": self.get_number_entity_value(i)}
return res

async def async_setup(self):
"""Post initialization setup."""
# Ensure an update is done on every hour
Expand Down Expand Up @@ -513,6 +521,10 @@ def __init__(self, unique_id: str) -> None:
self._unique_id = unique_id
self._np = None

def as_dict(self):
"""For diagnostics serialization."""
return self.__dict__

@property
def unique_id(self) -> str:
"""Get the unique id."""
Expand Down Expand Up @@ -675,6 +687,10 @@ def __str__(self) -> str:
"""Get string representation of class."""
return f"start_at={self.starts_at} cost_at={self.cost_at:.2} now_cost_rate={self.now_cost_rate:.2}"

def as_dict(self):
"""For diagnostics serialization."""
return self.__dict__


class NordpoolPlannerEntity(Entity):
"""Base class for nordpool planner entities."""
Expand All @@ -688,6 +704,17 @@ def __init__(
self._planner = planner
self._attr_device_info = planner.get_device_info()

def as_dict(self):
"""For diagnostics serialization."""
return {
k: v
for k, v in self.__dict__.items()
if not (
k.startswith("_")
or k in ["hass", "platform", "registry_entry", "device_entry"]
)
}

@property
def should_poll(self):
"""No need to poll. Coordinator notifies entity of updates."""
Expand Down
38 changes: 38 additions & 0 deletions custom_components/nordpool_planner/diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Diagnostics support for Nordpool Planner."""

from __future__ import annotations

# import json
import logging
from typing import Any

# from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from . import NordpoolPlanner
from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

# TO_REDACT = []


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: ConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
diag_data = {
# "config_entry": async_redact_data(config_entry, TO_REDACT),
# "planner": async_redact_data(hass.data[DOMAIN][config_entry.entry_id], TO_REDACT),
"config_entry": config_entry,
"planner": hass.data[DOMAIN][config_entry.entry_id],
}
# planner: NordpoolPlanner = hass.data[DOMAIN][config_entry.entry_id]
# if planner is not None:
# # diag_data["planner"] = planner.__dict__
# # diag_data["prices"] = planner._prices_entity.__dict__
# else:
# _LOGGER.warning("NordpoolPlanner is not available")

return diag_data

0 comments on commit 949a009

Please sign in to comment.