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
This commit is contained in:
Ryan Fleming 2021-11-09 23:52:29 -05:00 committed by GitHub
parent 86b12af3dc
commit 5e2d71dc90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 4 deletions

View File

@ -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

View File

@ -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"