From 006ca13caf292d2cf20b8062395ad84012f7f614 Mon Sep 17 00:00:00 2001 From: bvweerd Date: Tue, 17 Jun 2025 09:35:32 +0200 Subject: [PATCH 1/3] Fix missing data for diagnostics sensors --- README.md | 2 +- custom_components/simple_pid_controller/sensor.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 58b64c5..203ef2c 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ - [Manual Tuning](#1-manual-trial--error) - [Ziegler–Nichols Method](#2-zieglernichols-method) - [PID Calculation Frequency and Sample Time](#pid-calculation-frequency-and-sample-time) -- [More details and extended documentation]{#more-details-and-extended-documentation) +- [More details and extended documentation](#more-details-and-extended-documentation) - [Example PID Graph](#example-pid-graph) - [Support & Development](#support--development) - [Service Actions](#service-actions) diff --git a/custom_components/simple_pid_controller/sensor.py b/custom_components/simple_pid_controller/sensor.py index 8137637..087d5b0 100644 --- a/custom_components/simple_pid_controller/sensor.py +++ b/custom_components/simple_pid_controller/sensor.py @@ -32,7 +32,7 @@ async def async_setup_entry( ) -> None: """Set up PID output and diagnostic sensors.""" handle: PIDDeviceHandle = entry.runtime_data.handle - + # Init PID with default values pid = PID(1.0, 0.1, 0.05, setpoint=50) pid.sample_time = 10.0 @@ -199,6 +199,7 @@ def __init__( self._attr_entity_registry_enabled_default = False self._attr_state_class = SensorStateClass.MEASUREMENT self._key = key + self._handle = entry.runtime_data.handle @property def native_value(self): @@ -212,9 +213,9 @@ def native_value(self): error = input_value - setpoint value = { - "p": contributions[0], - "i": contributions[1], - "d": contributions[2], + "pid_p_contrib": contributions[0], + "pid_i_contrib": contributions[1], + "pid_d_contrib": contributions[2], "error": error, }.get(self._key) return round(value, 2) if value is not None else None From af143c44ed33f85fcbd0a8e0241ead5a90008a2e Mon Sep 17 00:00:00 2001 From: bvweerd Date: Tue, 17 Jun 2025 07:51:02 +0000 Subject: [PATCH 2/3] fix tests --- .../simple_pid_controller/sensor.py | 4 +-- tests/test_sensor.py | 26 +++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/custom_components/simple_pid_controller/sensor.py b/custom_components/simple_pid_controller/sensor.py index 087d5b0..4f26ec7 100644 --- a/custom_components/simple_pid_controller/sensor.py +++ b/custom_components/simple_pid_controller/sensor.py @@ -32,7 +32,7 @@ async def async_setup_entry( ) -> None: """Set up PID output and diagnostic sensors.""" handle: PIDDeviceHandle = entry.runtime_data.handle - + # Init PID with default values pid = PID(1.0, 0.1, 0.05, setpoint=50) pid.sample_time = 10.0 @@ -196,7 +196,7 @@ def __init__( BasePIDEntity.__init__(self, hass, entry, key, name) self._attr_entity_category = EntityCategory.DIAGNOSTIC - self._attr_entity_registry_enabled_default = False + # self._attr_entity_registry_enabled_default = False self._attr_state_class = SensorStateClass.MEASUREMENT self._key = key self._handle = entry.runtime_data.handle diff --git a/tests/test_sensor.py b/tests/test_sensor.py index b4f1579..509afaa 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -50,23 +50,33 @@ async def test_pid_contribution_native_value_rounding_and_none(hass, config_entr handle.last_contributions = (0.1234, 1.9876, 2.5555) coordinator = PIDDataCoordinator(hass, "test", lambda: 0, interval=1) - # Map contribution key to expected index + # Map contribution keys to expected values mapping = [ - ("p", round(0.1234, 2)), - ("i", round(1.9876, 2)), - ("d", round(2.5555, 2)), + ("pid_p_contrib", round(0.1234, 2)), + ("pid_i_contrib", round(1.9876, 2)), + ("pid_d_contrib", round(2.5555, 2)), + ("error", -25), + ("unknown_key", None), # Should return None ] + for key, expected in mapping: sensor = PIDContributionSensor( - hass, config_entry, key, f"pid_{key}_contrib", coordinator + hass, + config_entry, + key, + f"sensor.{config_entry.entry_id}_{key}", + coordinator, ) - # Override internal handle to use test handle - sensor._handle = handle + sensor._handle = handle # inject mock handle assert sensor.native_value == expected # Unknown key should return None sensor_none = PIDContributionSensor( - hass, config_entry, "x", "pid_x_contrib", coordinator + hass, + config_entry, + "x", + "sensor.{config_entry.entry_id}_pid_x_contrib", + coordinator, ) sensor_none._handle = handle assert sensor_none.native_value is None From 07316006449330cabd8be258b15e1e34b3a6ab56 Mon Sep 17 00:00:00 2001 From: bvweerd Date: Tue, 17 Jun 2025 10:04:25 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 58b64c5..9d17d01 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ - [Manual Tuning](#1-manual-trial--error) - [Ziegler–Nichols Method](#2-zieglernichols-method) - [PID Calculation Frequency and Sample Time](#pid-calculation-frequency-and-sample-time) -- [More details and extended documentation]{#more-details-and-extended-documentation) +- [More details and extended documentation](#extended-documentation) - [Example PID Graph](#example-pid-graph) - [Support & Development](#support--development) - [Service Actions](#service-actions) @@ -183,7 +183,7 @@ By using a single **Sample Time** for both scheduling and calculation—and unde --- -## 📚 More details and extended documentation +## 📚 Extended documentation The integration is based on simple-pid [https://pypi.org/project/simple-pid/](https://pypi.org/project/simple-pid/)