Store runtime data inside the config entry in Dremel 3D Printer (#119441)
* Store runtime data inside the config entry in Dremel 3D Printer * add typing for config entrypull/119482/head
parent
c70cfbb535
commit
10b32e6a24
|
@ -5,18 +5,19 @@ from __future__ import annotations
|
|||
from dremel3dpy import Dremel3DPrinter
|
||||
from requests.exceptions import ConnectTimeout, HTTPError
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
|
||||
from .const import CAMERA_MODEL, DOMAIN
|
||||
from .coordinator import Dremel3DPrinterDataUpdateCoordinator
|
||||
from .const import CAMERA_MODEL
|
||||
from .coordinator import Dremel3DPrinterDataUpdateCoordinator, DremelConfigEntry
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.CAMERA, Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, config_entry: DremelConfigEntry
|
||||
) -> bool:
|
||||
"""Set up Dremel 3D Printer from a config entry."""
|
||||
try:
|
||||
api = await hass.async_add_executor_job(
|
||||
|
@ -30,7 +31,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
|
||||
coordinator = Dremel3DPrinterDataUpdateCoordinator(hass, api)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
|
||||
config_entry.runtime_data = coordinator
|
||||
platforms = list(PLATFORMS)
|
||||
if api.get_model() != CAMERA_MODEL:
|
||||
platforms.remove(Platform.CAMERA)
|
||||
|
@ -38,12 +39,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: DremelConfigEntry) -> bool:
|
||||
"""Unload Dremel config entry."""
|
||||
platforms = list(PLATFORMS)
|
||||
api: Dremel3DPrinter = hass.data[DOMAIN][entry.entry_id].api
|
||||
if api.get_model() != CAMERA_MODEL:
|
||||
if entry.runtime_data.api.get_model() != CAMERA_MODEL:
|
||||
platforms.remove(Platform.CAMERA)
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, platforms):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, platforms)
|
||||
|
|
|
@ -12,11 +12,10 @@ from homeassistant.components.binary_sensor import (
|
|||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import DremelConfigEntry
|
||||
from .entity import Dremel3DPrinterEntity
|
||||
|
||||
|
||||
|
@ -43,14 +42,12 @@ BINARY_SENSOR_TYPES: tuple[Dremel3DPrinterBinarySensorEntityDescription, ...] =
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: DremelConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the available Dremel binary sensors."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
async_add_entities(
|
||||
Dremel3DPrinterBinarySensor(coordinator, description)
|
||||
Dremel3DPrinterBinarySensor(config_entry.runtime_data, description)
|
||||
for description in BINARY_SENSOR_TYPES
|
||||
)
|
||||
|
||||
|
|
|
@ -8,12 +8,11 @@ from dataclasses import dataclass
|
|||
from dremel3dpy import Dremel3DPrinter
|
||||
|
||||
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import DremelConfigEntry
|
||||
from .entity import Dremel3DPrinterEntity
|
||||
|
||||
|
||||
|
@ -45,13 +44,12 @@ BUTTON_TYPES: tuple[Dremel3DPrinterButtonEntityDescription, ...] = (
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: DremelConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Dremel 3D Printer control buttons."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
async_add_entities(
|
||||
Dremel3DPrinterButtonEntity(coordinator, description)
|
||||
Dremel3DPrinterButtonEntity(config_entry.runtime_data, description)
|
||||
for description in BUTTON_TYPES
|
||||
)
|
||||
|
||||
|
|
|
@ -4,12 +4,10 @@ from __future__ import annotations
|
|||
|
||||
from homeassistant.components.camera import CameraEntityDescription
|
||||
from homeassistant.components.mjpeg import MjpegCamera
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import Dremel3DPrinterDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from .coordinator import Dremel3DPrinterDataUpdateCoordinator, DremelConfigEntry
|
||||
from .entity import Dremel3DPrinterEntity
|
||||
|
||||
CAMERA_TYPE = CameraEntityDescription(
|
||||
|
@ -20,12 +18,11 @@ CAMERA_TYPE = CameraEntityDescription(
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: DremelConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up a MJPEG IP Camera for the 3D45 Model. The 3D20 and 3D40 models don't have built in cameras."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
async_add_entities([Dremel3D45Camera(coordinator, CAMERA_TYPE)])
|
||||
async_add_entities([Dremel3D45Camera(config_entry.runtime_data, CAMERA_TYPE)])
|
||||
|
||||
|
||||
class Dremel3D45Camera(Dremel3DPrinterEntity, MjpegCamera):
|
||||
|
|
|
@ -10,11 +10,13 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||
|
||||
from .const import DOMAIN, LOGGER
|
||||
|
||||
type DremelConfigEntry = ConfigEntry[Dremel3DPrinterDataUpdateCoordinator]
|
||||
|
||||
|
||||
class Dremel3DPrinterDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
||||
"""Class to manage fetching Dremel 3D Printer data."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: DremelConfigEntry
|
||||
|
||||
def __init__(self, hass: HomeAssistant, api: Dremel3DPrinter) -> None:
|
||||
"""Initialize Dremel 3D Printer data update coordinator."""
|
||||
|
|
|
@ -14,7 +14,6 @@ from homeassistant.components.sensor import (
|
|||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
EntityCategory,
|
||||
|
@ -28,7 +27,8 @@ from homeassistant.helpers.typing import StateType
|
|||
from homeassistant.util.dt import utcnow
|
||||
from homeassistant.util.variance import ignore_variance
|
||||
|
||||
from .const import ATTR_EXTRUDER, ATTR_PLATFORM, DOMAIN
|
||||
from .const import ATTR_EXTRUDER, ATTR_PLATFORM
|
||||
from .coordinator import DremelConfigEntry
|
||||
from .entity import Dremel3DPrinterEntity
|
||||
|
||||
|
||||
|
@ -234,14 +234,13 @@ SENSOR_TYPES: tuple[Dremel3DPrinterSensorEntityDescription, ...] = (
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: DremelConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the available Dremel 3D Printer sensors."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
async_add_entities(
|
||||
Dremel3DPrinterSensor(coordinator, description) for description in SENSOR_TYPES
|
||||
Dremel3DPrinterSensor(config_entry.runtime_data, description)
|
||||
for description in SENSOR_TYPES
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue