Skip to content

Derive the solar radiation device class from the resolved output unit - #1434

Open
dimatx wants to merge 1 commit into
bachya:devfrom
dimatx:dimatx-illuminance-device-class-fix
Open

dimatx wants to merge 1 commit into
bachya:devfrom
dimatx:dimatx-illuminance-device-class-fix

Conversation

@dimatx

@dimatx dimatx commented Sep 13, 2026

Copy link
Copy Markdown

DATA_POINT_SOLARRADIATION hardcodes device_class=irradiance, but its unit is
user-selectable via --output-unit-illuminance. Home Assistant accepts only W/m² and
BTU/(h⋅ft²) for irradiance, and only lx for illuminance, so every non-default
unit produces a pair HA rejects:

--output-unit-illuminance device_class emitted valid?
W/m² (default) irradiance
lx / klx / fc / kfc irradiance

MQTT discovery treats this as a schema error rather than a warning, so the sensor is
never created:

Error 'The unit of measurement `lx` is not valid together with device class `irradiance`'
when processing MQTT discovery message topic: 'homeassistant/sensor/<id>/solarradiation/config'

If the sensor already exists on the default unit, HA rejects the update instead and
keeps the old config, so the entity carries on reporting W/m² while lux values arrive
underneath.

Fix

Override the device class from the resolved unit, in the same style as
STATE_CLASS_OVERRIDES just below it:

UNIT_DEVICE_CLASS_OVERRIDES: dict[str, dict[str | None, str | None]] = {
    DATA_POINT_SOLARRADIATION: {
        UnitOfIlluminance.LUX: DeviceClass.ILLUMINANCE,
        UnitOfIlluminance.WATTS_PER_SQUARE_METER: DeviceClass.IRRADIANCE,
        UnitOfIlluminance.KILOLUX: None,
        UnitOfIlluminance.FOOT_CANDLES: None,
        UnitOfIlluminance.KILOFOOT_CANDLES: None,
    },
}
discovery.device_class = UNIT_DEVICE_CLASS_OVERRIDES.get(data_point_key, {}).get(
    discovery.unit_of_measurement, description.device_class
)

klx/fc/kfc map to None because HA has no device class that accepts them; omitting
device_class is valid and the sensor is created normally. Data points with no entry
keep their static device class, so the default output is unchanged.

Tests cover each unit, and the README's Illuminance section notes the resulting device
class. Verified against Home Assistant 2026.9.2.

Previously reported in #1297.

Home Assistant only accepts `lx` for the `illuminance` device class and
`W/m2`/`BTU/(h.ft2)` for `irradiance`. `DATA_POINT_SOLARRADIATION` hardcoded
`irradiance` in its `EntityDescription`, but the unit is user-selectable via
`--output-unit-illuminance`, so any non-default choice produced a combination
Home Assistant rejects:

  lx            -> irradiance   (invalid)
  klx/fc/kfc    -> irradiance   (invalid)

MQTT discovery treats this as a hard schema failure, so the sensor is never
created and an error is logged on every discovery message.

Derive the device class from the resolved unit instead, and omit it entirely
for units that map to no valid device class (`klx`, `fc`, `kfc`) - which Home
Assistant explicitly permits.

Fixes bachya#1297
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@bachya bachya left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (76451f9) to head (8b3715b).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev     #1434   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           33        33           
  Lines         1797      1798    +1     
=========================================
+ Hits          1797      1798    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants