mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Add tools to octoprint when the printer comes back online (#59666)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
4fecd5d8af
commit
9db2d8768b
@ -1,4 +1,6 @@
|
|||||||
"""Support for monitoring OctoPrint 3D printers."""
|
"""Support for monitoring OctoPrint 3D printers."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import cast
|
from typing import cast
|
||||||
@ -175,7 +177,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = {"coordinator": coordinator, "client": client}
|
hass.data[DOMAIN][entry.entry_id] = {
|
||||||
|
"coordinator": coordinator,
|
||||||
|
"client": client,
|
||||||
|
}
|
||||||
|
|
||||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ from homeassistant.components.sensor import (
|
|||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS
|
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
@ -47,13 +47,23 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
assert device_id is not None
|
assert device_id is not None
|
||||||
|
|
||||||
entities: list[SensorEntity] = []
|
known_tools = set()
|
||||||
if coordinator.data["printer"]:
|
|
||||||
printer_info = coordinator.data["printer"]
|
@callback
|
||||||
types = ["actual", "target"]
|
def async_add_tool_sensors() -> None:
|
||||||
for tool in printer_info.temperatures:
|
if not coordinator.data["printer"]:
|
||||||
for temp_type in types:
|
return
|
||||||
entities.append(
|
|
||||||
|
new_tools = []
|
||||||
|
for tool in [
|
||||||
|
tool
|
||||||
|
for tool in coordinator.data["printer"].temperatures
|
||||||
|
if tool.name not in known_tools
|
||||||
|
]:
|
||||||
|
assert device_id is not None
|
||||||
|
known_tools.add(tool.name)
|
||||||
|
for temp_type in ("actual", "target"):
|
||||||
|
new_tools.append(
|
||||||
OctoPrintTemperatureSensor(
|
OctoPrintTemperatureSensor(
|
||||||
coordinator,
|
coordinator,
|
||||||
tool.name,
|
tool.name,
|
||||||
@ -61,13 +71,20 @@ async def async_setup_entry(
|
|||||||
device_id,
|
device_id,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
if new_tools:
|
||||||
_LOGGER.debug("Printer appears to be offline, skipping temperature sensors")
|
async_add_entities(new_tools)
|
||||||
|
|
||||||
entities.append(OctoPrintStatusSensor(coordinator, device_id))
|
config_entry.async_on_unload(coordinator.async_add_listener(async_add_tool_sensors))
|
||||||
entities.append(OctoPrintJobPercentageSensor(coordinator, device_id))
|
|
||||||
entities.append(OctoPrintEstimatedFinishTimeSensor(coordinator, device_id))
|
if coordinator.data["printer"]:
|
||||||
entities.append(OctoPrintStartTimeSensor(coordinator, device_id))
|
async_add_tool_sensors()
|
||||||
|
|
||||||
|
entities: list[SensorEntity] = [
|
||||||
|
OctoPrintStatusSensor(coordinator, device_id),
|
||||||
|
OctoPrintJobPercentageSensor(coordinator, device_id),
|
||||||
|
OctoPrintEstimatedFinishTimeSensor(coordinator, device_id),
|
||||||
|
OctoPrintStartTimeSensor(coordinator, device_id),
|
||||||
|
]
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user