Use octoprint printer flag status to check if printer is printing (#59663)

This commit is contained in:
Ryan Fleming 2021-11-14 15:06:42 -05:00 committed by GitHub
parent 9f2ec5c906
commit 9c2bff3b3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 4 deletions

View file

@ -23,6 +23,17 @@ from .const import DOMAIN
_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(
hass: HomeAssistant,
@ -151,7 +162,11 @@ class OctoPrintEstimatedFinishTimeSensor(OctoPrintSensorBase):
def native_value(self):
"""Return sensor state."""
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
read_time = self.coordinator.data["last_read_time"]
@ -175,7 +190,11 @@ class OctoPrintStartTimeSensor(OctoPrintSensorBase):
"""Return sensor state."""
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
read_time = self.coordinator.data["last_read_time"]

View file

@ -11,7 +11,7 @@ async def test_sensors(hass):
"""Test the underlying sensors."""
printer = {
"state": {
"flags": {},
"flags": {"printing": True},
"text": "Operational",
},
"temperature": {"tool1": {"actual": 18.83136, "target": 37.83136}},
@ -82,7 +82,7 @@ async def test_sensors_no_target_temp(hass):
"""Test the underlying sensors."""
printer = {
"state": {
"flags": {},
"flags": {"printing": True, "paused": False},
"text": "Operational",
},
"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"
entry = entity_registry.async_get("sensor.octoprint_target_tool1_temp")
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"