diff --git a/homeassistant/components/octoprint/sensor.py b/homeassistant/components/octoprint/sensor.py index af8ce9bc3c0..2a3e8c773ff 100644 --- a/homeassistant/components/octoprint/sensor.py +++ b/homeassistant/components/octoprint/sensor.py @@ -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"] diff --git a/tests/components/octoprint/test_sensor.py b/tests/components/octoprint/test_sensor.py index 136674f3465..ccc7dfacaf2 100644 --- a/tests/components/octoprint/test_sensor.py +++ b/tests/components/octoprint/test_sensor.py @@ -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"