mirror of
https://github.com/home-assistant/core.git
synced 2026-04-18 16:06:42 +01:00
Use runtime_data in ourgroceries integration (#167043)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,21 +6,19 @@ from aiohttp import ClientError
|
|||||||
from ourgroceries import OurGroceries
|
from ourgroceries import OurGroceries
|
||||||
from ourgroceries.exceptions import InvalidLoginException
|
from ourgroceries.exceptions import InvalidLoginException
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import OurGroceriesConfigEntry, OurGroceriesDataUpdateCoordinator
|
||||||
from .coordinator import OurGroceriesDataUpdateCoordinator
|
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.TODO]
|
PLATFORMS: list[Platform] = [Platform.TODO]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: OurGroceriesConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Set up OurGroceries from a config entry."""
|
"""Set up OurGroceries from a config entry."""
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
data = entry.data
|
data = entry.data
|
||||||
og = OurGroceries(data[CONF_USERNAME], data[CONF_PASSWORD])
|
og = OurGroceries(data[CONF_USERNAME], data[CONF_PASSWORD])
|
||||||
try:
|
try:
|
||||||
@@ -32,16 +30,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
coordinator = OurGroceriesDataUpdateCoordinator(hass, entry, og)
|
coordinator = OurGroceriesDataUpdateCoordinator(hass, entry, og)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data[DOMAIN][entry.entry_id] = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(
|
||||||
|
hass: HomeAssistant, entry: OurGroceriesConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|||||||
@@ -19,13 +19,19 @@ SCAN_INTERVAL = 60
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
type OurGroceriesConfigEntry = ConfigEntry[OurGroceriesDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class OurGroceriesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, dict]]):
|
class OurGroceriesDataUpdateCoordinator(DataUpdateCoordinator[dict[str, dict]]):
|
||||||
"""Class to manage fetching OurGroceries data."""
|
"""Class to manage fetching OurGroceries data."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: OurGroceriesConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, hass: HomeAssistant, config_entry: ConfigEntry, og: OurGroceries
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: OurGroceriesConfigEntry,
|
||||||
|
og: OurGroceries,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize global OurGroceries data updater."""
|
"""Initialize global OurGroceries data updater."""
|
||||||
self.og = og
|
self.og = og
|
||||||
|
|||||||
@@ -9,22 +9,20 @@ from homeassistant.components.todo import (
|
|||||||
TodoListEntity,
|
TodoListEntity,
|
||||||
TodoListEntityFeature,
|
TodoListEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import OurGroceriesConfigEntry, OurGroceriesDataUpdateCoordinator
|
||||||
from .coordinator import OurGroceriesDataUpdateCoordinator
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: OurGroceriesConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the OurGroceries todo platform config entry."""
|
"""Set up the OurGroceries todo platform config entry."""
|
||||||
coordinator: OurGroceriesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
OurGroceriesTodoListEntity(coordinator, sl["id"], sl["name"])
|
OurGroceriesTodoListEntity(coordinator, sl["id"], sl["name"])
|
||||||
for sl in coordinator.lists
|
for sl in coordinator.lists
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from unittest.mock import AsyncMock, patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.ourgroceries import DOMAIN
|
from homeassistant.components.ourgroceries.const import DOMAIN
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|||||||
Reference in New Issue
Block a user