mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Improve diagnostics code of EnergyZero integration (#133019)
This commit is contained in:
parent
a9d71e0a5f
commit
0006672489
@ -33,25 +33,28 @@ async def async_get_config_entry_diagnostics(
|
|||||||
hass: HomeAssistant, entry: EnergyZeroConfigEntry
|
hass: HomeAssistant, entry: EnergyZeroConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
|
coordinator_data = entry.runtime_data.data
|
||||||
|
energy_today = coordinator_data.energy_today
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"entry": {
|
"entry": {
|
||||||
"title": entry.title,
|
"title": entry.title,
|
||||||
},
|
},
|
||||||
"energy": {
|
"energy": {
|
||||||
"current_hour_price": entry.runtime_data.data.energy_today.current_price,
|
"current_hour_price": energy_today.current_price,
|
||||||
"next_hour_price": entry.runtime_data.data.energy_today.price_at_time(
|
"next_hour_price": energy_today.price_at_time(
|
||||||
entry.runtime_data.data.energy_today.utcnow() + timedelta(hours=1)
|
energy_today.utcnow() + timedelta(hours=1)
|
||||||
),
|
),
|
||||||
"average_price": entry.runtime_data.data.energy_today.average_price,
|
"average_price": energy_today.average_price,
|
||||||
"max_price": entry.runtime_data.data.energy_today.extreme_prices[1],
|
"max_price": energy_today.extreme_prices[1],
|
||||||
"min_price": entry.runtime_data.data.energy_today.extreme_prices[0],
|
"min_price": energy_today.extreme_prices[0],
|
||||||
"highest_price_time": entry.runtime_data.data.energy_today.highest_price_time,
|
"highest_price_time": energy_today.highest_price_time,
|
||||||
"lowest_price_time": entry.runtime_data.data.energy_today.lowest_price_time,
|
"lowest_price_time": energy_today.lowest_price_time,
|
||||||
"percentage_of_max": entry.runtime_data.data.energy_today.pct_of_max_price,
|
"percentage_of_max": energy_today.pct_of_max_price,
|
||||||
"hours_priced_equal_or_lower": entry.runtime_data.data.energy_today.hours_priced_equal_or_lower,
|
"hours_priced_equal_or_lower": energy_today.hours_priced_equal_or_lower,
|
||||||
},
|
},
|
||||||
"gas": {
|
"gas": {
|
||||||
"current_hour_price": get_gas_price(entry.runtime_data.data, 0),
|
"current_hour_price": get_gas_price(coordinator_data, 0),
|
||||||
"next_hour_price": get_gas_price(entry.runtime_data.data, 1),
|
"next_hour_price": get_gas_price(coordinator_data, 1),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,12 @@ from __future__ import annotations
|
|||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from typing import Final
|
from typing import TYPE_CHECKING, Final
|
||||||
|
|
||||||
from energyzero import Electricity, Gas, VatOption
|
from energyzero import Electricity, Gas, VatOption
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import (
|
from homeassistant.core import (
|
||||||
HomeAssistant,
|
HomeAssistant,
|
||||||
ServiceCall,
|
ServiceCall,
|
||||||
@ -22,6 +22,9 @@ from homeassistant.exceptions import ServiceValidationError
|
|||||||
from homeassistant.helpers import selector
|
from homeassistant.helpers import selector
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from . import EnergyZeroConfigEntry
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import EnergyZeroDataUpdateCoordinator
|
from .coordinator import EnergyZeroDataUpdateCoordinator
|
||||||
|
|
||||||
@ -88,7 +91,7 @@ def __get_coordinator(
|
|||||||
) -> EnergyZeroDataUpdateCoordinator:
|
) -> EnergyZeroDataUpdateCoordinator:
|
||||||
"""Get the coordinator from the entry."""
|
"""Get the coordinator from the entry."""
|
||||||
entry_id: str = call.data[ATTR_CONFIG_ENTRY]
|
entry_id: str = call.data[ATTR_CONFIG_ENTRY]
|
||||||
entry: ConfigEntry | None = hass.config_entries.async_get_entry(entry_id)
|
entry: EnergyZeroConfigEntry | None = hass.config_entries.async_get_entry(entry_id)
|
||||||
|
|
||||||
if not entry:
|
if not entry:
|
||||||
raise ServiceValidationError(
|
raise ServiceValidationError(
|
||||||
@ -107,8 +110,7 @@ def __get_coordinator(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
coordinator: EnergyZeroDataUpdateCoordinator = entry.runtime_data
|
return entry.runtime_data
|
||||||
return coordinator
|
|
||||||
|
|
||||||
|
|
||||||
async def __get_prices(
|
async def __get_prices(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user