Home Assistant Unofficial Reference 2024.12.1
diagnostics.py
Go to the documentation of this file.
1 """Diagnostics support for easyEnergy."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import Any
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 
11 from . import EasyEnergyDataUpdateCoordinator
12 from .const import DOMAIN
13 from .coordinator import EasyEnergyData
14 
15 
16 def get_gas_price(data: EasyEnergyData, hours: int) -> float | None:
17  """Get the gas price for a given hour.
18 
19  Args:
20  data: The data object.
21  hours: The number of hours to add to the current time.
22 
23  Returns:
24  The gas market price value.
25 
26  """
27  if not data.gas_today:
28  return None
29  return data.gas_today.price_at_time(
30  data.gas_today.utcnow() + timedelta(hours=hours)
31  )
32 
33 
35  hass: HomeAssistant, entry: ConfigEntry
36 ) -> dict[str, Any]:
37  """Return diagnostics for a config entry."""
38  coordinator: EasyEnergyDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
39 
40  return {
41  "entry": {
42  "title": entry.title,
43  },
44  "energy_usage": {
45  "current_hour_price": coordinator.data.energy_today.current_usage_price,
46  "next_hour_price": coordinator.data.energy_today.price_at_time(
47  coordinator.data.energy_today.utcnow() + timedelta(hours=1)
48  ),
49  "average_price": coordinator.data.energy_today.average_usage_price,
50  "max_price": coordinator.data.energy_today.extreme_usage_prices[1],
51  "min_price": coordinator.data.energy_today.extreme_usage_prices[0],
52  "highest_price_time": coordinator.data.energy_today.highest_usage_price_time,
53  "lowest_price_time": coordinator.data.energy_today.lowest_usage_price_time,
54  "percentage_of_max": coordinator.data.energy_today.pct_of_max_usage,
55  },
56  "energy_return": {
57  "current_hour_price": coordinator.data.energy_today.current_return_price,
58  "next_hour_price": coordinator.data.energy_today.price_at_time(
59  coordinator.data.energy_today.utcnow() + timedelta(hours=1), "return"
60  ),
61  "average_price": coordinator.data.energy_today.average_return_price,
62  "max_price": coordinator.data.energy_today.extreme_return_prices[1],
63  "min_price": coordinator.data.energy_today.extreme_return_prices[0],
64  "highest_price_time": coordinator.data.energy_today.highest_return_price_time,
65  "lowest_price_time": coordinator.data.energy_today.lowest_return_price_time,
66  "percentage_of_max": coordinator.data.energy_today.pct_of_max_return,
67  },
68  "gas": {
69  "current_hour_price": get_gas_price(coordinator.data, 0),
70  "next_hour_price": get_gas_price(coordinator.data, 1),
71  },
72  }
float|None get_gas_price(EasyEnergyData data, int hours)
Definition: diagnostics.py:16
dict[str, Any] async_get_config_entry_diagnostics(HomeAssistant hass, ConfigEntry entry)
Definition: diagnostics.py:36