Home Assistant Unofficial Reference 2024.12.1
diagnostics.py
Go to the documentation of this file.
1 """Diagnostics support for EnergyZero."""
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 EnergyZeroDataUpdateCoordinator
12 from .const import DOMAIN
13 from .coordinator import EnergyZeroData
14 
15 
16 def get_gas_price(data: EnergyZeroData, 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: EnergyZeroDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
39 
40  return {
41  "entry": {
42  "title": entry.title,
43  },
44  "energy": {
45  "current_hour_price": coordinator.data.energy_today.current_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_price,
50  "max_price": coordinator.data.energy_today.extreme_prices[1],
51  "min_price": coordinator.data.energy_today.extreme_prices[0],
52  "highest_price_time": coordinator.data.energy_today.highest_price_time,
53  "lowest_price_time": coordinator.data.energy_today.lowest_price_time,
54  "percentage_of_max": coordinator.data.energy_today.pct_of_max_price,
55  "hours_priced_equal_or_lower": coordinator.data.energy_today.hours_priced_equal_or_lower,
56  },
57  "gas": {
58  "current_hour_price": get_gas_price(coordinator.data, 0),
59  "next_hour_price": get_gas_price(coordinator.data, 1),
60  },
61  }
dict[str, Any] async_get_config_entry_diagnostics(HomeAssistant hass, ConfigEntry entry)
Definition: diagnostics.py:36
float|None get_gas_price(EnergyZeroData data, int hours)
Definition: diagnostics.py:16