From 289f66817a50890129b2271181913136fc61e79d Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Tue, 25 Mar 2025 13:52:09 +0200 Subject: [PATCH] Add blog post about changes to `UnitSystem` class and their implications (#2618) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add blog post about changes to UnitSystem class and their implications * Apply suggestions from code review Co-authored-by: Abílio Costa * Update blog/2025-03-24-frozen-unit-system.md Co-authored-by: Martin Hjelmare * Fix typo * Update filename --------- Co-authored-by: Abílio Costa Co-authored-by: Martin Hjelmare --- blog/2025-03-25-frozen-unit-system.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 blog/2025-03-25-frozen-unit-system.md diff --git a/blog/2025-03-25-frozen-unit-system.md b/blog/2025-03-25-frozen-unit-system.md new file mode 100644 index 00000000..251731a9 --- /dev/null +++ b/blog/2025-03-25-frozen-unit-system.md @@ -0,0 +1,22 @@ +--- +author: Shay Levy +authorURL: https://github.com/thecode +title: "UnitSystem dataclass is now frozen" +--- + +`UnitSystem` class is changed to a frozen data class, and all instances derived from it are now frozen. + +The following unit systems that derived from the `UnitSystem` class are now frozen: + +- `METRIC_SYSTEM` +- `US_CUSTOMARY_SYSTEM` + +The reason for this change is that Unit systems are constants that should not be modified. An integration that modifies these constants can break unit conversions and create undesired output for other components of Home Assistant. + +With a frozen data class an attempt to modify the UnitSystem constant will fail: + +```python +dataclasses.FrozenInstanceError: cannot assign to field 'temperature_unit' +``` + +This change was introduced in the [home assistant core PR #140954](https://github.com/home-assistant/core/pull/140954).