Repository: XKNX/xknx
Pull request #964 (2022-06-03 08:38:54)

Set unit_of_measurement for DPTString to None
<!--
  You are awesome! Thanks for contributing to our project!
  Please, DO NOT DELETE ANY TEXT from this template!.
-->
## Description
<!--
Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context.
List any dependencies that are required for this change.
-->
The representation of a sensor in Home Assistant depends on the unit of measurement. If a sensor has an attribute 'unit_of_measurement' it will be treated as a numerical value and therefore be shown in a graph in the history. This does not work for strings. By removing the attribute the sensor will be shown in the logbook instead / the history depiction is changed from a graph to a "state list".

Wrong:
![grafik](https://user-images.githubusercontent.com/88034398/171814829-4d83a39b-a30d-4a39-85e5-ed4406e7d41d.png)

Fixed:
![grafik](https://user-images.githubusercontent.com/88034398/171815023-276fc9be-4011-4508-a1e6-755dce8bfece.png)

It was discussed in the HA forum:
https://community.home-assistant.io/t/knx-string-sensor/427315

Fixes # (issue)

## Type of change
<!--
Please tick the applicable options.
NOTE: Ticking multiple options most likely indicates
that your change is to big and it is suggested to split it into several smaller PRs.
-->

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

## Checklist:

- [ ] The documentation has been adjusted accordingly
- [x] Tests have been added that prove the fix is effective or that the feature works
- [ ] The changes are documented in the changelog (docs/changelog.md)

farmio: Great! Please run `black` and do commit its changes to make CI pass. 
codecov[bot]: # [Codecov](https://codecov.io/gh/XKNX/xknx/pull/964?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX) Report
> Merging [#964](https://codecov.io/gh/XKNX/xknx/pull/964?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX) (d870f70) into [main](https://codecov.io/gh/XKNX/xknx/commit/a402c4609a8f3d5d96d83af271db52cb9006d6ec?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX) (a402c46) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/XKNX/xknx/pull/964/graphs/tree.svg?width=650&height=150&src=pr&token=irWbIygS84&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX)](https://codecov.io/gh/XKNX/xknx/pull/964?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX)

```diff
@@           Coverage Diff           @@
##             main     #964   +/-   ##
=======================================
  Coverage   95.99%   95.99%           
=======================================
  Files         131      131           
  Lines        7924     7924           
=======================================
  Hits         7607     7607           
  Misses        317      317           
```


| [Impacted Files](https://codecov.io/gh/XKNX/xknx/pull/964?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX) | Coverage Δ | |
|---|---|---|
| [xknx/dpt/dpt\_string.py](https://codecov.io/gh/XKNX/xknx/pull/964/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=XKNX#diff-eGtueC9kcHQvZHB0X3N0cmluZy5weQ==) | `100.00% <100.00%> (ø)` | |


Patch:
diff --git a/xknx/dpt/dpt_string.py b/xknx/dpt/dpt_string.py
index faf33207..2d4ae5a4 100644
--- a/xknx/dpt/dpt_string.py
+++ b/xknx/dpt/dpt_string.py
@@ -17,7 +17,7 @@ class DPTString(DPTBase):
     dpt_main_number = 16
     dpt_sub_number = 0
     value_type = "string"
-    unit = ""
+    unit = None
 
     _encoding = "ascii"
 


Test patch:
diff --git a/test/dpt_tests/dpt_string_test.py b/test/dpt_tests/dpt_string_test.py
index 0cf820e5..9db68bea 100644
--- a/test/dpt_tests/dpt_string_test.py
+++ b/test/dpt_tests/dpt_string_test.py
@@ -89,3 +89,7 @@ class TestDPTString:
         """Test parsing of KNX string with wrong elements length."""
         with pytest.raises(ConversionError):
             DPTString.from_knx(raw)
+
+    def test_no_unit_of_measurement(self):
+        """Test for no unit set for DPT 16"""
+        assert DPTString.unit is None