Explicitly pass in the config_entry in ipp coordinator (#138138)

explicitly pass in the config_entry in coordinator
This commit is contained in:
Michael 2025-02-09 21:43:46 +01:00 committed by GitHub
parent 427013124c
commit 08dbd83a55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 47 deletions

View File

@ -2,39 +2,17 @@
from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_HOST,
CONF_PORT,
CONF_SSL,
CONF_VERIFY_SSL,
Platform,
)
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from .const import CONF_BASE_PATH
from .coordinator import IPPDataUpdateCoordinator
from .coordinator import IPPConfigEntry, IPPDataUpdateCoordinator
PLATFORMS = [Platform.SENSOR]
type IPPConfigEntry = ConfigEntry[IPPDataUpdateCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: IPPConfigEntry) -> bool:
"""Set up IPP from a config entry."""
# config flow sets this to either UUID, serial number or None
if (device_id := entry.unique_id) is None:
device_id = entry.entry_id
coordinator = IPPDataUpdateCoordinator(
hass,
host=entry.data[CONF_HOST],
port=entry.data[CONF_PORT],
base_path=entry.data[CONF_BASE_PATH],
tls=entry.data[CONF_SSL],
verify_ssl=entry.data[CONF_VERIFY_SSL],
device_id=device_id,
)
coordinator = IPPDataUpdateCoordinator(hass, entry)
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator
@ -44,6 +22,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: IPPConfigEntry) -> bool:
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: IPPConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -7,45 +7,42 @@ import logging
from pyipp import IPP, IPPError, Printer as IPPPrinter
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL, CONF_VERIFY_SSL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
from .const import CONF_BASE_PATH, DOMAIN
SCAN_INTERVAL = timedelta(seconds=60)
_LOGGER = logging.getLogger(__name__)
type IPPConfigEntry = ConfigEntry[IPPDataUpdateCoordinator]
class IPPDataUpdateCoordinator(DataUpdateCoordinator[IPPPrinter]):
"""Class to manage fetching IPP data from single endpoint."""
def __init__(
self,
hass: HomeAssistant,
*,
host: str,
port: int,
base_path: str,
tls: bool,
verify_ssl: bool,
device_id: str,
) -> None:
config_entry: IPPConfigEntry
def __init__(self, hass: HomeAssistant, config_entry: IPPConfigEntry) -> None:
"""Initialize global IPP data updater."""
self.device_id = device_id
self.device_id = config_entry.unique_id or config_entry.entry_id
self.ipp = IPP(
host=host,
port=port,
base_path=base_path,
tls=tls,
verify_ssl=verify_ssl,
session=async_get_clientsession(hass, verify_ssl),
host=config_entry.data[CONF_HOST],
port=config_entry.data[CONF_PORT],
base_path=config_entry.data[CONF_BASE_PATH],
tls=config_entry.data[CONF_SSL],
verify_ssl=config_entry.data[CONF_VERIFY_SSL],
session=async_get_clientsession(hass, config_entry.data[CONF_VERIFY_SSL]),
)
super().__init__(
hass,
_LOGGER,
config_entry=config_entry,
name=DOMAIN,
update_interval=SCAN_INTERVAL,
)

View File

@ -6,7 +6,7 @@ from typing import Any
from homeassistant.core import HomeAssistant
from . import IPPConfigEntry
from .coordinator import IPPConfigEntry
async def async_get_config_entry_diagnostics(

View File

@ -20,7 +20,6 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import IPPConfigEntry
from .const import (
ATTR_COMMAND_SET,
ATTR_INFO,
@ -32,6 +31,7 @@ from .const import (
ATTR_STATE_REASON,
ATTR_URI_SUPPORTED,
)
from .coordinator import IPPConfigEntry
from .entity import IPPEntity