mirror of
https://github.com/home-assistant/core.git
synced 2026-09-26 01:11:51 -04:00
Use runtime_data in smart_meter_texas integration (#167743)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
11c34c7ddf
commit
efb0e80577
@@ -5,20 +5,24 @@ import logging
|
||||
from smart_meter_texas import Account
|
||||
from smart_meter_texas.exceptions import SmartMeterTexasAuthError
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .const import DATA_COORDINATOR, DATA_SMART_METER, DOMAIN
|
||||
from .coordinator import SmartMeterTexasCoordinator, SmartMeterTexasData
|
||||
from .coordinator import (
|
||||
SmartMeterTexasConfigEntry,
|
||||
SmartMeterTexasCoordinator,
|
||||
SmartMeterTexasData,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: SmartMeterTexasConfigEntry
|
||||
) -> bool:
|
||||
"""Set up Smart Meter Texas from a config entry."""
|
||||
|
||||
username = entry.data[CONF_USERNAME]
|
||||
@@ -43,11 +47,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
# too long to update.
|
||||
coordinator = SmartMeterTexasCoordinator(hass, entry, smart_meter_texas_data)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = {
|
||||
DATA_COORDINATOR: coordinator,
|
||||
DATA_SMART_METER: smart_meter_texas_data,
|
||||
}
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
entry.async_create_background_task(
|
||||
hass, coordinator.async_refresh(), "smart_meter_texas-coordinator-refresh"
|
||||
@@ -58,10 +58,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(
|
||||
hass: HomeAssistant, entry: SmartMeterTexasConfigEntry
|
||||
) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
@@ -5,9 +5,6 @@ from datetime import timedelta
|
||||
SCAN_INTERVAL = timedelta(hours=1)
|
||||
DEBOUNCE_COOLDOWN = 1800 # Seconds
|
||||
|
||||
DATA_COORDINATOR = "coordinator"
|
||||
DATA_SMART_METER = "smart_meter_data"
|
||||
|
||||
DOMAIN = "smart_meter_texas"
|
||||
|
||||
METER_NUMBER = "meter_number"
|
||||
|
||||
@@ -52,15 +52,18 @@ class SmartMeterTexasData:
|
||||
return self.meters
|
||||
|
||||
|
||||
class SmartMeterTexasCoordinator(DataUpdateCoordinator[SmartMeterTexasData]):
|
||||
type SmartMeterTexasConfigEntry = ConfigEntry[SmartMeterTexasCoordinator]
|
||||
|
||||
|
||||
class SmartMeterTexasCoordinator(DataUpdateCoordinator[None]):
|
||||
"""Class to manage fetching Smart Meter Texas data."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: SmartMeterTexasConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: SmartMeterTexasConfigEntry,
|
||||
smart_meter_texas_data: SmartMeterTexasData,
|
||||
) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
@@ -74,10 +77,9 @@ class SmartMeterTexasCoordinator(DataUpdateCoordinator[SmartMeterTexasData]):
|
||||
hass, _LOGGER, cooldown=DEBOUNCE_COOLDOWN, immediate=True
|
||||
),
|
||||
)
|
||||
self._smart_meter_texas_data = smart_meter_texas_data
|
||||
self.smart_meter_texas_data = smart_meter_texas_data
|
||||
|
||||
async def _async_update_data(self) -> SmartMeterTexasData:
|
||||
async def _async_update_data(self) -> None:
|
||||
"""Fetch latest data."""
|
||||
_LOGGER.debug("Fetching latest data")
|
||||
await self._smart_meter_texas_data.read_meters()
|
||||
return self._smart_meter_texas_data
|
||||
await self.smart_meter_texas_data.read_meters()
|
||||
|
||||
@@ -9,32 +9,24 @@ from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_ADDRESS, UnitOfEnergy
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import (
|
||||
DATA_COORDINATOR,
|
||||
DATA_SMART_METER,
|
||||
DOMAIN,
|
||||
ELECTRIC_METER,
|
||||
ESIID,
|
||||
METER_NUMBER,
|
||||
)
|
||||
from .coordinator import SmartMeterTexasCoordinator
|
||||
from .const import ELECTRIC_METER, ESIID, METER_NUMBER
|
||||
from .coordinator import SmartMeterTexasConfigEntry, SmartMeterTexasCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: SmartMeterTexasConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Smart Meter Texas sensors."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
|
||||
meters = hass.data[DOMAIN][config_entry.entry_id][DATA_SMART_METER].meters
|
||||
coordinator = config_entry.runtime_data
|
||||
meters = coordinator.smart_meter_texas_data.meters
|
||||
|
||||
async_add_entities(
|
||||
[SmartMeterTexasSensor(meter, coordinator) for meter in meters], False
|
||||
|
||||
Reference in New Issue
Block a user