Add blog post about changes to UnitSystem class and their implications (#2618)

* Add blog post about changes to UnitSystem class and their implications

* Apply suggestions from code review

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>

* Update blog/2025-03-24-frozen-unit-system.md

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix typo

* Update filename

---------

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Shay Levy 2025-03-25 13:52:09 +02:00 committed by GitHub
parent 5327735cea
commit 289f66817a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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