Add mealie version to device info (#121443)
parent
67facdf3a5
commit
1fefd396b9
|
@ -2,22 +2,45 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from aiomealie import MealieAuthenticationError, MealieClient, MealieConnectionError
|
||||
|
||||
from .coordinator import MealieCoordinator
|
||||
from homeassistant.const import CONF_API_TOKEN, CONF_HOST, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MealieConfigEntry, MealieCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.CALENDAR]
|
||||
|
||||
|
||||
type MealieConfigEntry = ConfigEntry[MealieCoordinator]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: MealieConfigEntry) -> bool:
|
||||
"""Set up Mealie from a config entry."""
|
||||
client = MealieClient(
|
||||
entry.data[CONF_HOST],
|
||||
token=entry.data[CONF_API_TOKEN],
|
||||
session=async_get_clientsession(hass),
|
||||
)
|
||||
try:
|
||||
about = await client.get_about()
|
||||
except MealieAuthenticationError as error:
|
||||
raise ConfigEntryError("Authentication failed") from error
|
||||
except MealieConnectionError as error:
|
||||
raise ConfigEntryNotReady(error) from error
|
||||
|
||||
coordinator = MealieCoordinator(hass)
|
||||
assert entry.unique_id
|
||||
device_registry = dr.async_get(hass)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
identifiers={(DOMAIN, entry.unique_id)},
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
sw_version=about.version,
|
||||
)
|
||||
|
||||
coordinator = MealieCoordinator(hass, client)
|
||||
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from aiomealie import (
|
||||
MealieAuthenticationError,
|
||||
|
@ -13,36 +12,30 @@ from aiomealie import (
|
|||
MealplanEntryType,
|
||||
)
|
||||
|
||||
from homeassistant.const import CONF_API_TOKEN, CONF_HOST
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryError
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .const import LOGGER
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from . import MealieConfigEntry
|
||||
|
||||
WEEK = timedelta(days=7)
|
||||
|
||||
type MealieConfigEntry = ConfigEntry[MealieCoordinator]
|
||||
|
||||
|
||||
class MealieCoordinator(DataUpdateCoordinator[dict[MealplanEntryType, list[Mealplan]]]):
|
||||
"""Class to manage fetching Mealie data."""
|
||||
|
||||
config_entry: MealieConfigEntry
|
||||
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
def __init__(self, hass: HomeAssistant, client: MealieClient) -> None:
|
||||
"""Initialize coordinator."""
|
||||
super().__init__(
|
||||
hass, logger=LOGGER, name="Mealie", update_interval=timedelta(hours=1)
|
||||
)
|
||||
self.client = MealieClient(
|
||||
self.config_entry.data[CONF_HOST],
|
||||
token=self.config_entry.data[CONF_API_TOKEN],
|
||||
session=async_get_clientsession(hass),
|
||||
)
|
||||
self.client = client
|
||||
|
||||
async def _async_update_data(self) -> dict[MealplanEntryType, list[Mealplan]]:
|
||||
next_week = dt_util.now() + WEEK
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Base class for Mealie entities."""
|
||||
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
|
@ -20,5 +20,4 @@ class MealieEntity(CoordinatorEntity[MealieCoordinator]):
|
|||
self._attr_unique_id = f"{unique_id}_{key}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, unique_id)},
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
)
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
from collections.abc import Generator
|
||||
from unittest.mock import patch
|
||||
|
||||
from aiomealie import Mealplan, MealplanResponse, UserInfo
|
||||
from aiomealie import About, Mealplan, MealplanResponse, UserInfo
|
||||
from mashumaro.codecs.orjson import ORJSONDecoder
|
||||
import pytest
|
||||
|
||||
|
@ -29,7 +29,7 @@ def mock_mealie_client() -> Generator[AsyncMock]:
|
|||
"""Mock a Mealie client."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.mealie.coordinator.MealieClient",
|
||||
"homeassistant.components.mealie.MealieClient",
|
||||
autospec=True,
|
||||
) as mock_client,
|
||||
patch(
|
||||
|
@ -47,6 +47,9 @@ def mock_mealie_client() -> Generator[AsyncMock]:
|
|||
client.get_user_info.return_value = UserInfo.from_json(
|
||||
load_fixture("users_self.json", DOMAIN)
|
||||
)
|
||||
client.get_about.return_value = About.from_json(
|
||||
load_fixture("about.json", DOMAIN)
|
||||
)
|
||||
yield client
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"version": "v1.10.2"
|
||||
}
|
|
@ -26,7 +26,7 @@
|
|||
'primary_config_entry': <ANY>,
|
||||
'serial_number': None,
|
||||
'suggested_area': None,
|
||||
'sw_version': None,
|
||||
'sw_version': 'v1.10.2',
|
||||
'via_device_id': None,
|
||||
})
|
||||
# ---
|
||||
|
|
Loading…
Reference in New Issue