Skip to content

Sensor platform crashes with 'NoneType' object has no attribute 'get' when energy_sensor/power_sensor options are set #459

Description

@kaiokaio2k

Describe the bug
The sensor platform fails to set up entirely, throwing an unhandled exception,
even though the energy_sensor and power_sensor options were correctly
configured via the options flow (confirmed by re-opening the options flow,
which shows the correct suggested_value for energy_data_format and
energy_data_scale).

Traceback
Error while setting up midea_ac platform for sensor:
'NoneType' object has no attribute 'get'

File ".../custom_components/midea_ac/sensor.py", line 69, in _get_energy_config
config = config_entry.options.get(key)
format = device.EnergyDataFormat.get_from_name(
config.get(CONF_ENERGY_DATA_FORMAT).upper())
AttributeError: 'NoneType' object has no attribute 'get'

Impact
Because this exception is raised while building the entities list (before
add_entities() is called), it aborts the ENTIRE sensor platform setup, not
just the energy/power sensors. This also removes unrelated sensors like
indoor_temperature and outdoor_temperature for the device.

Reproduction

  1. Set up a device that supports enable_energy_usage_requests
  2. Configure energy_sensor and power_sensor options via the options flow
  3. Reload the integration
  4. Sensor platform fails to load entirely

Workaround applied locally
Made _get_energy_config defensive against a missing/None config:

def _get_energy_config(key: str) -> tuple[EnergyFormat, float]:
    config = config_entry.options.get(key) or {}
    format = device.EnergyDataFormat.get_from_name(
        config.get(CONF_ENERGY_DATA_FORMAT, "bcd").upper())
    scale = config.get(CONF_ENERGY_DATA_SCALE, 1.0)
    return format, scale

After this change, all sensors (including energy/power) load correctly.

Environment

  • Integration version: 2026.4.0 (also reproduced on 2026.7.1)
  • Device: Midea PortaSplit
  • Home Assistant Core: 2026.7.4

Additional context
Not sure if this is related to #453 (the _DEFAULT_OPTIONS mutation fix),
but the symptom here is the opposite: an expected key is missing/None for
a single, correctly-configured device, rather than unexpected keys leaking
in from other devices.

Metadata

Metadata

Assignees

No one assigned

    Labels

    need more infoFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions