2022-08-30 00:45:27 +00:00
|
|
|
"""The PrusaLink integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-12-08 16:50:36 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2023-08-15 15:21:49 +00:00
|
|
|
import asyncio
|
2022-08-30 00:45:27 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2022-09-28 08:42:19 +00:00
|
|
|
from time import monotonic
|
2023-12-19 17:07:27 +00:00
|
|
|
from typing import TypeVar
|
2022-08-30 00:45:27 +00:00
|
|
|
|
2023-12-19 17:07:27 +00:00
|
|
|
from pyprusalink import JobInfo, LegacyPrinterStatus, PrinterStatus, PrusaLink
|
|
|
|
from pyprusalink.types import InvalidAuth, PrusaLinkError
|
2022-08-30 00:45:27 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-12-19 17:07:27 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
|
|
|
Platform,
|
|
|
|
)
|
2022-09-28 08:42:19 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2023-12-20 18:10:12 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryError
|
2023-12-19 17:07:27 +00:00
|
|
|
from homeassistant.helpers import issue_registry as ir
|
2022-08-30 00:45:27 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2022-08-30 00:45:27 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
UpdateFailed,
|
|
|
|
)
|
|
|
|
|
2023-12-20 18:10:12 +00:00
|
|
|
from .config_flow import ConfigFlow
|
2022-08-30 00:45:27 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
2022-09-28 08:42:19 +00:00
|
|
|
PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.CAMERA, Platform.SENSOR]
|
2022-08-30 00:45:27 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-12-20 18:10:12 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up PrusaLink from a config entry."""
|
|
|
|
if entry.version == 1 and entry.minor_version < 2:
|
|
|
|
raise ConfigEntryError("Please upgrade your printer's firmware.")
|
2023-12-19 17:07:27 +00:00
|
|
|
|
2022-08-30 00:45:27 +00:00
|
|
|
api = PrusaLink(
|
|
|
|
async_get_clientsession(hass),
|
2023-12-20 18:10:12 +00:00
|
|
|
entry.data[CONF_HOST],
|
|
|
|
entry.data[CONF_USERNAME],
|
|
|
|
entry.data[CONF_PASSWORD],
|
2022-08-30 00:45:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
coordinators = {
|
2023-12-19 17:07:27 +00:00
|
|
|
"legacy_status": LegacyStatusCoordinator(hass, api),
|
|
|
|
"status": StatusCoordinator(hass, api),
|
2022-08-30 00:45:27 +00:00
|
|
|
"job": JobUpdateCoordinator(hass, api),
|
|
|
|
}
|
|
|
|
for coordinator in coordinators.values():
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinators
|
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-12-20 18:10:12 +00:00
|
|
|
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2023-12-19 17:07:27 +00:00
|
|
|
"""Migrate old entry."""
|
2023-12-20 18:10:12 +00:00
|
|
|
if config_entry.version > ConfigFlow.VERSION:
|
|
|
|
# This means the user has downgraded from a future version
|
|
|
|
return False
|
|
|
|
|
|
|
|
new_data = dict(config_entry.data)
|
|
|
|
if config_entry.version == 1:
|
|
|
|
if config_entry.minor_version < 2:
|
|
|
|
# Add username and password
|
|
|
|
# "maker" is currently hardcoded in the firmware
|
|
|
|
# https://github.com/prusa3d/Prusa-Firmware-Buddy/blob/bfb0ffc745ee6546e7efdba618d0e7c0f4c909cd/lib/WUI/wui_api.h#L19
|
|
|
|
username = "maker"
|
|
|
|
password = config_entry.data[CONF_API_KEY]
|
|
|
|
|
|
|
|
api = PrusaLink(
|
|
|
|
async_get_clientsession(hass),
|
|
|
|
config_entry.data[CONF_HOST],
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
await api.get_info()
|
|
|
|
except InvalidAuth:
|
|
|
|
# We are unable to reach the new API which usually means
|
|
|
|
# that the user is running an outdated firmware version
|
|
|
|
ir.async_create_issue(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
"firmware_5_1_required",
|
|
|
|
is_fixable=False,
|
|
|
|
severity=ir.IssueSeverity.ERROR,
|
|
|
|
translation_key="firmware_5_1_required",
|
|
|
|
translation_placeholders={
|
|
|
|
"entry_title": config_entry.title,
|
|
|
|
"prusa_mini_firmware_update": "https://help.prusa3d.com/article/firmware-updating-mini-mini_124784",
|
|
|
|
"prusa_mk4_xl_firmware_update": "https://help.prusa3d.com/article/how-to-update-firmware-mk4-xl_453086",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
# There is a check in the async_setup_entry to prevent the setup if minor_version < 2
|
|
|
|
# Currently we can't reload the config entry
|
|
|
|
# if the migration returns False.
|
|
|
|
# Return True here to workaround that.
|
|
|
|
return True
|
|
|
|
|
|
|
|
new_data[CONF_USERNAME] = username
|
|
|
|
new_data[CONF_PASSWORD] = password
|
|
|
|
|
|
|
|
ir.async_delete_issue(hass, DOMAIN, "firmware_5_1_required")
|
|
|
|
config_entry.minor_version = 2
|
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(config_entry, data=new_data)
|
|
|
|
|
2023-12-19 17:07:27 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-08-30 00:45:27 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
2023-12-19 17:07:27 +00:00
|
|
|
T = TypeVar("T", PrinterStatus, LegacyPrinterStatus, JobInfo)
|
2022-08-30 00:45:27 +00:00
|
|
|
|
|
|
|
|
2023-12-19 17:07:27 +00:00
|
|
|
class PrusaLinkUpdateCoordinator(DataUpdateCoordinator[T], ABC):
|
2022-08-30 00:45:27 +00:00
|
|
|
"""Update coordinator for the printer."""
|
|
|
|
|
|
|
|
config_entry: ConfigEntry
|
2022-09-28 08:42:19 +00:00
|
|
|
expect_change_until = 0.0
|
2022-08-30 00:45:27 +00:00
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant, api: PrusaLink) -> None:
|
|
|
|
"""Initialize the update coordinator."""
|
|
|
|
self.api = api
|
|
|
|
|
|
|
|
super().__init__(
|
2022-09-28 08:42:19 +00:00
|
|
|
hass, _LOGGER, name=DOMAIN, update_interval=self._get_update_interval(None)
|
2022-08-30 00:45:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def _async_update_data(self) -> T:
|
|
|
|
"""Update the data."""
|
|
|
|
try:
|
2023-08-15 15:21:49 +00:00
|
|
|
async with asyncio.timeout(5):
|
2022-09-28 08:42:19 +00:00
|
|
|
data = await self._fetch_data()
|
2022-08-30 00:45:27 +00:00
|
|
|
except InvalidAuth:
|
|
|
|
raise UpdateFailed("Invalid authentication") from None
|
|
|
|
except PrusaLinkError as err:
|
|
|
|
raise UpdateFailed(str(err)) from err
|
|
|
|
|
2022-09-28 08:42:19 +00:00
|
|
|
self.update_interval = self._get_update_interval(data)
|
|
|
|
return data
|
|
|
|
|
2022-08-30 00:45:27 +00:00
|
|
|
@abstractmethod
|
|
|
|
async def _fetch_data(self) -> T:
|
|
|
|
"""Fetch the actual data."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2022-09-28 08:42:19 +00:00
|
|
|
@callback
|
|
|
|
def expect_change(self) -> None:
|
|
|
|
"""Expect a change."""
|
|
|
|
self.expect_change_until = monotonic() + 30
|
|
|
|
|
|
|
|
def _get_update_interval(self, data: T) -> timedelta:
|
|
|
|
"""Get new update interval."""
|
|
|
|
if self.expect_change_until > monotonic():
|
|
|
|
return timedelta(seconds=5)
|
|
|
|
|
|
|
|
return timedelta(seconds=30)
|
|
|
|
|
2022-08-30 00:45:27 +00:00
|
|
|
|
2023-12-19 17:07:27 +00:00
|
|
|
class StatusCoordinator(PrusaLinkUpdateCoordinator[PrinterStatus]):
|
2022-08-30 00:45:27 +00:00
|
|
|
"""Printer update coordinator."""
|
|
|
|
|
2023-12-19 17:07:27 +00:00
|
|
|
async def _fetch_data(self) -> PrinterStatus:
|
2022-08-30 00:45:27 +00:00
|
|
|
"""Fetch the printer data."""
|
2023-12-19 17:07:27 +00:00
|
|
|
return await self.api.get_status()
|
2022-08-30 00:45:27 +00:00
|
|
|
|
2022-09-28 08:42:19 +00:00
|
|
|
|
2023-12-19 17:07:27 +00:00
|
|
|
class LegacyStatusCoordinator(PrusaLinkUpdateCoordinator[LegacyPrinterStatus]):
|
|
|
|
"""Printer legacy update coordinator."""
|
|
|
|
|
|
|
|
async def _fetch_data(self) -> LegacyPrinterStatus:
|
|
|
|
"""Fetch the printer data."""
|
|
|
|
return await self.api.get_legacy_printer()
|
2022-09-28 08:42:19 +00:00
|
|
|
|
2022-08-30 00:45:27 +00:00
|
|
|
|
|
|
|
class JobUpdateCoordinator(PrusaLinkUpdateCoordinator[JobInfo]):
|
|
|
|
"""Job update coordinator."""
|
|
|
|
|
|
|
|
async def _fetch_data(self) -> JobInfo:
|
|
|
|
"""Fetch the printer data."""
|
|
|
|
return await self.api.get_job()
|
|
|
|
|
|
|
|
|
|
|
|
class PrusaLinkEntity(CoordinatorEntity[PrusaLinkUpdateCoordinator]):
|
|
|
|
"""Defines a base PrusaLink entity."""
|
|
|
|
|
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self) -> DeviceInfo:
|
|
|
|
"""Return device information about this PrusaLink device."""
|
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self.coordinator.config_entry.entry_id)},
|
|
|
|
name=self.coordinator.config_entry.title,
|
|
|
|
manufacturer="Prusa",
|
2023-12-19 17:07:27 +00:00
|
|
|
configuration_url=self.coordinator.api.client.host,
|
2022-08-30 00:45:27 +00:00
|
|
|
)
|