mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use octoprint printer flag status to check if printer is printing (#59663)
This commit is contained in:
parent
9f2ec5c906
commit
9c2bff3b3b
@ -23,6 +23,17 @@ from .const import DOMAIN
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
JOB_PRINTING_STATES = ["Printing from SD", "Printing"]
|
||||||
|
|
||||||
|
|
||||||
|
def _is_printer_printing(printer: OctoprintPrinterInfo) -> bool:
|
||||||
|
return (
|
||||||
|
printer
|
||||||
|
and printer.state
|
||||||
|
and printer.state.flags
|
||||||
|
and printer.state.flags.printing
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
@ -151,7 +162,11 @@ class OctoPrintEstimatedFinishTimeSensor(OctoPrintSensorBase):
|
|||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return sensor state."""
|
"""Return sensor state."""
|
||||||
job: OctoprintJobInfo = self.coordinator.data["job"]
|
job: OctoprintJobInfo = self.coordinator.data["job"]
|
||||||
if not job or not job.progress.print_time_left or job.state != "Printing":
|
if (
|
||||||
|
not job
|
||||||
|
or not job.progress.print_time_left
|
||||||
|
or not _is_printer_printing(self.coordinator.data["printer"])
|
||||||
|
):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
read_time = self.coordinator.data["last_read_time"]
|
read_time = self.coordinator.data["last_read_time"]
|
||||||
@ -175,7 +190,11 @@ class OctoPrintStartTimeSensor(OctoPrintSensorBase):
|
|||||||
"""Return sensor state."""
|
"""Return sensor state."""
|
||||||
job: OctoprintJobInfo = self.coordinator.data["job"]
|
job: OctoprintJobInfo = self.coordinator.data["job"]
|
||||||
|
|
||||||
if not job or not job.progress.print_time or job.state != "Printing":
|
if (
|
||||||
|
not job
|
||||||
|
or not job.progress.print_time
|
||||||
|
or not _is_printer_printing(self.coordinator.data["printer"])
|
||||||
|
):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
read_time = self.coordinator.data["last_read_time"]
|
read_time = self.coordinator.data["last_read_time"]
|
||||||
|
@ -11,7 +11,7 @@ async def test_sensors(hass):
|
|||||||
"""Test the underlying sensors."""
|
"""Test the underlying sensors."""
|
||||||
printer = {
|
printer = {
|
||||||
"state": {
|
"state": {
|
||||||
"flags": {},
|
"flags": {"printing": True},
|
||||||
"text": "Operational",
|
"text": "Operational",
|
||||||
},
|
},
|
||||||
"temperature": {"tool1": {"actual": 18.83136, "target": 37.83136}},
|
"temperature": {"tool1": {"actual": 18.83136, "target": 37.83136}},
|
||||||
@ -82,7 +82,7 @@ async def test_sensors_no_target_temp(hass):
|
|||||||
"""Test the underlying sensors."""
|
"""Test the underlying sensors."""
|
||||||
printer = {
|
printer = {
|
||||||
"state": {
|
"state": {
|
||||||
"flags": {},
|
"flags": {"printing": True, "paused": False},
|
||||||
"text": "Operational",
|
"text": "Operational",
|
||||||
},
|
},
|
||||||
"temperature": {"tool1": {"actual": 18.83136, "target": None}},
|
"temperature": {"tool1": {"actual": 18.83136, "target": None}},
|
||||||
@ -107,3 +107,39 @@ async def test_sensors_no_target_temp(hass):
|
|||||||
assert state.name == "OctoPrint target tool1 temp"
|
assert state.name == "OctoPrint target tool1 temp"
|
||||||
entry = entity_registry.async_get("sensor.octoprint_target_tool1_temp")
|
entry = entity_registry.async_get("sensor.octoprint_target_tool1_temp")
|
||||||
assert entry.unique_id == "target tool1 temp-uuid"
|
assert entry.unique_id == "target tool1 temp-uuid"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_sensors_paused(hass):
|
||||||
|
"""Test the underlying sensors."""
|
||||||
|
printer = {
|
||||||
|
"state": {
|
||||||
|
"flags": {"printing": False},
|
||||||
|
"text": "Operational",
|
||||||
|
},
|
||||||
|
"temperature": {"tool1": {"actual": 18.83136, "target": None}},
|
||||||
|
}
|
||||||
|
job = {
|
||||||
|
"job": {},
|
||||||
|
"progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000},
|
||||||
|
"state": "Paused",
|
||||||
|
}
|
||||||
|
with patch(
|
||||||
|
"homeassistant.util.dt.utcnow", return_value=datetime(2020, 2, 20, 9, 10, 0)
|
||||||
|
):
|
||||||
|
await init_integration(hass, "sensor", printer=printer, job=job)
|
||||||
|
|
||||||
|
entity_registry = er.async_get(hass)
|
||||||
|
|
||||||
|
state = hass.states.get("sensor.octoprint_start_time")
|
||||||
|
assert state is not None
|
||||||
|
assert state.state == "unknown"
|
||||||
|
assert state.name == "OctoPrint Start Time"
|
||||||
|
entry = entity_registry.async_get("sensor.octoprint_start_time")
|
||||||
|
assert entry.unique_id == "Start Time-uuid"
|
||||||
|
|
||||||
|
state = hass.states.get("sensor.octoprint_estimated_finish_time")
|
||||||
|
assert state is not None
|
||||||
|
assert state.state == "unknown"
|
||||||
|
assert state.name == "OctoPrint Estimated Finish Time"
|
||||||
|
entry = entity_registry.async_get("sensor.octoprint_estimated_finish_time")
|
||||||
|
assert entry.unique_id == "Estimated Finish Time-uuid"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user