enkama / hass-variables

Home Assistant variables component
92 stars 12 forks source link

Bug - New Sensor Variable "No Device or Entity" when configuring with specific Measurement/Device Class #136

Closed dafunkydan closed 3 weeks ago

dafunkydan commented 1 month ago

When adding a new Sensor Variable with v3.4.7, configured with

Device Class: PM25
State Class: MEASUREMENT
Unit of Measurement: µg/m³

the Entity can't get created. In UI it shows up as "No Device or Entity". Reload doesn't help either. The corresponding Error Log says:

Error adding entity sensor.ac_cleaner_1_pm25_daily_1st_hour_average for domain sensor with platform variable

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 595, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 714, in _async_add_entity
    entity.add_to_platform_start(
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 244, in add_to_platform_start
    self._sensor_option_unit_of_measurement = self._get_initial_suggested_unit()
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 415, in _get_initial_suggested_unit
    if not self._is_valid_suggested_unit(suggested_unit_of_measurement):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 383, in _is_valid_suggested_unit
    raise ValueError(
ValueError: Entity <class 'custom_components.variable.sensor.Variable'> suggest an incorrect unit of measurement: µg/m³.

What helped me was a hacky procedure: