2021-06-29 08:02:49 +00:00
|
|
|
"""Coordinator for The Internet Printing Protocol (IPP) integration."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2021-06-29 08:02:49 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from pyipp import IPP, IPPError, Printer as IPPPrinter
|
|
|
|
|
2025-02-09 20:43:46 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL, CONF_VERIFY_SSL
|
2021-06-29 08:02:49 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
2025-02-09 20:43:46 +00:00
|
|
|
from .const import CONF_BASE_PATH, DOMAIN
|
2021-06-29 08:02:49 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=60)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2025-02-09 20:43:46 +00:00
|
|
|
type IPPConfigEntry = ConfigEntry[IPPDataUpdateCoordinator]
|
|
|
|
|
2021-06-29 08:02:49 +00:00
|
|
|
|
|
|
|
class IPPDataUpdateCoordinator(DataUpdateCoordinator[IPPPrinter]):
|
|
|
|
"""Class to manage fetching IPP data from single endpoint."""
|
|
|
|
|
2025-02-09 20:43:46 +00:00
|
|
|
config_entry: IPPConfigEntry
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant, config_entry: IPPConfigEntry) -> None:
|
2021-06-29 08:02:49 +00:00
|
|
|
"""Initialize global IPP data updater."""
|
2025-02-09 20:43:46 +00:00
|
|
|
self.device_id = config_entry.unique_id or config_entry.entry_id
|
2021-06-29 08:02:49 +00:00
|
|
|
self.ipp = IPP(
|
2025-02-09 20:43:46 +00:00
|
|
|
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]),
|
2021-06-29 08:02:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
2025-02-09 20:43:46 +00:00
|
|
|
config_entry=config_entry,
|
2021-06-29 08:02:49 +00:00
|
|
|
name=DOMAIN,
|
|
|
|
update_interval=SCAN_INTERVAL,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _async_update_data(self) -> IPPPrinter:
|
|
|
|
"""Fetch data from IPP."""
|
|
|
|
try:
|
|
|
|
return await self.ipp.printer()
|
|
|
|
except IPPError as error:
|
|
|
|
raise UpdateFailed(f"Invalid response from API: {error}") from error
|