From 5e2d71dc9037b9c9186bff846eaf74490209f3cf Mon Sep 17 00:00:00 2001 From: Ryan Fleming Date: Tue, 9 Nov 2021 23:52:29 -0500 Subject: [PATCH] Use unknown state for octoprint temperature sensors with None value (#59130) * Mark octoprint temperature sensors as unavaible when value is not supplied * Check for none explictly * Do not mark the entity as unavailable * Swap to using er.get_async --- homeassistant/components/octoprint/sensor.py | 9 +++-- tests/components/octoprint/test_sensor.py | 35 +++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/octoprint/sensor.py b/homeassistant/components/octoprint/sensor.py index 70151d86022..af8ce9bc3c0 100644 --- a/homeassistant/components/octoprint/sensor.py +++ b/homeassistant/components/octoprint/sensor.py @@ -211,12 +211,15 @@ class OctoPrintTemperatureSensor(OctoPrintSensorBase): for temp in printer.temperatures: if temp.name == self._api_tool: - return round( + val = ( temp.actual_temp if self._temp_type == "actual" - else temp.target_temp, - 2, + else temp.target_temp ) + if val is None: + return None + + return round(val, 2) return None diff --git a/tests/components/octoprint/test_sensor.py b/tests/components/octoprint/test_sensor.py index c3a02c1bab5..136674f3465 100644 --- a/tests/components/octoprint/test_sensor.py +++ b/tests/components/octoprint/test_sensor.py @@ -2,6 +2,8 @@ from datetime import datetime from unittest.mock import patch +from homeassistant.helpers import entity_registry as er + from . import init_integration @@ -24,7 +26,7 @@ async def test_sensors(hass): ): await init_integration(hass, "sensor", printer=printer, job=job) - entity_registry = await hass.helpers.entity_registry.async_get_registry() + entity_registry = er.async_get(hass) state = hass.states.get("sensor.octoprint_job_percentage") assert state is not None @@ -74,3 +76,34 @@ async def test_sensors(hass): 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" + + +async def test_sensors_no_target_temp(hass): + """Test the underlying sensors.""" + printer = { + "state": { + "flags": {}, + "text": "Operational", + }, + "temperature": {"tool1": {"actual": 18.83136, "target": None}}, + } + with patch( + "homeassistant.util.dt.utcnow", return_value=datetime(2020, 2, 20, 9, 10, 0) + ): + await init_integration(hass, "sensor", printer=printer) + + entity_registry = er.async_get(hass) + + state = hass.states.get("sensor.octoprint_actual_tool1_temp") + assert state is not None + assert state.state == "18.83" + assert state.name == "OctoPrint actual tool1 temp" + entry = entity_registry.async_get("sensor.octoprint_actual_tool1_temp") + assert entry.unique_id == "actual tool1 temp-uuid" + + state = hass.states.get("sensor.octoprint_target_tool1_temp") + assert state is not None + assert state.state == "unknown" + 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"