From 6890a988f684b0d3e1ec44e7fbe5aa0d15e36bb0 Mon Sep 17 00:00:00 2001 From: Hannu Teulahti Date: Mon, 9 Dec 2024 16:51:28 +0200 Subject: [PATCH] add temperature_probe and round temperatures (#450) * add temperature_probe * round temperatures * WIP: add test for probe --------- Co-authored-by: Marc-Olivier Arsenault --- custom_components/moonraker/sensor.py | 8 +++++--- tests/conftest.py | 4 ++++ tests/test_sensor.py | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/custom_components/moonraker/sensor.py b/custom_components/moonraker/sensor.py index 70af335..44d8891 100755 --- a/custom_components/moonraker/sensor.py +++ b/custom_components/moonraker/sensor.py @@ -332,6 +332,7 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities): temperature_keys = [ "temperature_sensor", "temperature_fan", + "temperature_probe", "tmc2240", "bme280", "htu21d", @@ -351,9 +352,10 @@ async def async_setup_optional_sensors(coordinator, entry, async_add_entities): status_key=obj, name=split_obj[1].removesuffix("_temp").replace("_", " ").title() + " Temp", - value_fn=lambda sensor: sensor.coordinator.data["status"][ - sensor.status_key - ]["temperature"], + value_fn=lambda sensor: round( + sensor.coordinator.data["status"][sensor.status_key]["temperature"], + 2, + ), subscriptions=[(obj, "temperature")], icon="mdi:thermometer", unit=UnitOfTemperature.CELSIUS, diff --git a/tests/conftest.py b/tests/conftest.py index 38cb6af..489e1ef 100755 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -120,6 +120,9 @@ def get_data_fixture(): "speed": 0.1234, "rpm": 4500, }, + "temperature_probe eddy_temp": { + "temperature": 32.43, + }, "temperature_host host_temp": { "temperature": 32.43, }, @@ -293,6 +296,7 @@ def get_printer_objects_list_fixture(): "bed_mesh", "screws_tilt_adjust", "temperature_sensor mcu_temp", + "temperature_probe eddy_temp", "stepper_enable", "motion_report", "query_endstops", diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 840ac8f..6cbbeb6 100755 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -71,6 +71,7 @@ async def test_sensor_services_update(hass, get_data): "sensor, value", [ ("mainsail_mcu_temp", "32.43"), + ("mainsail_eddy_temp", "32.43"), ("mainsail_bed_target", "60.0"), ("mainsail_bed_temperature", "60.01"), ("mainsail_extruder_target", "205.0"), @@ -215,6 +216,20 @@ async def test_opt_sensor_missing(hass, get_data, get_printer_objects_list): assert state is None +async def test_opt_probe_missing(hass, get_data, get_printer_objects_list): + """Test.""" + get_data["status"].pop("temperature_probe eddy_temp", None) + get_printer_objects_list["objects"].remove("temperature_probe eddy_temp") + + config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get("sensor.mainsail_eddy_temp") + assert state is None + + async def test_eta(hass): """Test.""" config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")