Skip to main content

Adopting a new way to name entities

· 3 min read

We are working on improving and standardizing our entity naming. This will allow us to show, in the UI, entities in the right context in the future while removing some error-prone magic mangling of entity names from the code base.

The short story is:

  • Devices have their name as they have today, for example, "Dishwasher".
  • Entities will have their own name (without device, area). Or, they may optionally set the name to None (in that case they inherit the device name).
  • Device, Area, and Entity names all start with a capital letter, the rest of the words are lower case (unless it's a word that represents a brand/name/abbreviation of course).
  • Every entity which has been migrated to follow these rules should set the has_entity_name property to True.

During the migration period, we use the has_entity_name property to create "backward compatible" friendly names. In the future, we can show deprecation warnings for entities that don't set this property, and later, remove it entirely.

The frontend is going to be adjusted for this. It will be able to show the entities/devices in various ways, what suits the context the most.

Example

This illustrates how devices and entities should be named according to the new recommendations (type: entity name / state object's friendly_name / entity_id). Developers only need to set device name and entity name, the friendly_name and entity_id are automatically generated.

  • Device: Dishwasher
    • Switch: None / Dishwasher / switch.dishwasher
    • Sensor: Power usage / Dishwasher Power usage / sensor.dishwasher_power_usage
  • Device: Laundry machine
    • Switch: None / Laundry machine / switch.laundry_machine
    • Sensor: Power usage / Laundry machine Power usage / sensor.laundry_machine_power_usage

Background

Home Assistant models the home of a user into three levels:

  • Area (eg. Living Room)
  • Device (eg. Switch)
  • Entity (eg. Power usage)

Entities in Home Assistant are the data points provided by a device and can represent specific controls (a switch of the device, a light of the device).

Because Home Assistant used to only have entities in the first many years of its existence, a lot of functionality is based around entities in Home Assistant, the main one being the UI.

Let’s say you have 2 Shelly switches that report power usage named Dishwasher and Laundry Machine. Both devices have a switch entity and a power sensor. The devices and entities would previously look like this (type: entity name / state object friendly_name / entity_id):

  • Device: Dishwasher
    • Switch: Dishwasher Switch / Dishwasher Switch / switch.dishwasher_switch
    • Sensor: Dishwasher Power usage / Dishwasher Power usage / sensor.dishwasher_power_usage
  • Device: Laundry machine
    • Switch: Laundry machine Switch / Laundry machine Switch / switch.laundry_machine_switch
    • Sensor: Laundry machine Power usage / Laundry machine Power usage / sensor.laundry_machine_power_usage

Why is this a problem?

There is not a single source of truth for a device name because entities included the device name in their name.

Because we “solved” a problem for the UI and the entity_id by including the device name in the entity name, we now have this solution applied to all places where we use entities and have to work around this solution.

This naming scheme makes it unnecessarily difficult to migrate the UI towards hierarchical views of areas->devices->entities instead of long lists of entities.

Waiting for config entry platforms

· One min read

Before 2022.8, it was impossible to await config entry platforms forwards without a deadlock if one of the platforms loaded by the config entry was not already loaded.

Integrations need to be refactored to replace calls to hass.config_entries.async_setup_platforms with awaiting hass.config_entries.async_forward_entry_setups to ensure that Home Assistant does not inadvertently reload the integration while entities and platforms are still being set up.

hass.config_entries.async_setup_platforms is scheduled to be removed in 2022.12.

Store is now a Generic class

· One min read

As of Home Assistant Core 2022.8, a Store (from homeassistant/helpers/storage.py) is defined as a Generic Store(Generic[_T]). It is recommended that the type of data being stored be defined in the Store definition. It should be JSON-serialisable (dict or list), for example:

  • Standard definition using a dict: self._store = Store[dict[str, int]](hass, STORAGE_VERSION, STORAGE_KEY)
  • Using a TypedDict: self._store = Store[EnergyPreferences](hass, STORAGE_VERSION, STORAGE_KEY)
  • Accessing an existing Store: store: Store[dict[str, Any]] = hass.data[DOMAIN][DATA_STORE]
  • Inherited Store: class MyCustomStorage(Store[list[int]]):

For more information about generics, see PEP 483

Weather entity refactoring to support unit conversions

· One min read

WeatherEntity now supports temperature unit conversion following a similar pattern as the unit conversion supported by NumberEntity and SensorEntity.

Precipitation, pressure, temperature, visibility and wind speed are automatically converted according to the unit system configured by the users. In addition, users can override units for specific weather entities.

To make this possible, custom component integrations should be updated to override properties native_precipitation_unit, native_pressure, native_pressure_unit, native_temperature, native_temperature_unit,native_visibility, native_visibility_unit, native_wind_speed and native_wind_speed_unit, instead of precipitation_unit, pressure, pressure_unit, temperature, temperature_unit,visibility, visibility_unit, wind_speed and wind_speed_unit.

The same renaming has been done for the corresponding _attr_* attributes as well as for members of the Forecast typed dict

In Home Assistant Core 2023.1, overriding precipitation_unit, pressure, pressure_unit, temperature, temperature_unit,visibility, visibility_unit, wind_speed, wind_speed_unit, setting _attr_precipitation_unit, _attr_pressure, _attr_pressure_unit, _attr_temperature, _attr_temperature_unit, _attr_visibility, _attr_visibility_unit, _attr_wind_speed, _attr_wind_speed_unit and setting precipitation, pressure, temperature, templow, wind_speed on instances of Forecast is no longer supported.

Number entity refactoring to support unit conversion

· One min read

NumberEntity now supports temperature unit conversion following a similar pattern as the unit conversion supported by SensorEntity.

Temperature conversion will automatically be done for number entities with device class set to temperature to the temperature unit configured by the user.

To make this possible, custom component integrations should be updated to override properties native_max_value, native_min_value, native_step, native_unit_of_measurement, native_value instead of max_value, min_value, step, unit_of_measurement, value and to override methods async_set_native_value and set_native_value instead of async_set_value and set_value.

The same renaming has been done for _attr_* attributes as well as members of NumberEntityDescription.

In Home Assistant Core 2023.1, overriding async_set_value, max_value, min_value, set_value, step, unit_of_measurement, value, setting _attr_max_value, _attr_min_value, _attr_unit_of_measurement, _attr_step, _attr_value and setting max_value, min_value, unit_of_measurement, step on instances of NumberEntityDescription is no longer supported.

Avoiding reloading config entries while they are setting up

· One min read

Before 2022.7, it was possible to trigger a reload of a config entry while it was still setting up. Reloading during config entry setup often led to unexpected failure modes, which required restarting Home Assistant to get the config entry back in a good state. Attempting a reload during setup now raises the OperationNotAllowed exception.

Deprecating Data Entry Flow constants

· One min read

As of Home Assistant Core 2022.7, all RESULT_TYPE_* constants for data entry flow result types are deprecated:

  • RESULT_TYPE_FORM
  • RESULT_TYPE_CREATE_ENTRY
  • RESULT_TYPE_ABORT
  • RESULT_TYPE_EXTERNAL_STEP
  • RESULT_TYPE_EXTERNAL_STEP_DONE
  • RESULT_TYPE_SHOW_PROGRESS
  • RESULT_TYPE_SHOW_PROGRESS_DONE
  • RESULT_TYPE_MENU

Use the new FlowResultType enum instead.

Support context in update coordinator

· One min read

Starting with Home Assistant 2022.7, the update coordinator supports keeping track of context for each listening entity. This can be used to limit the requests against APIs based on enabled entities.

This could be a breaking change for custom components that rely on update coordinators and inspect the internal variable self._listeners and/or overload the method async_remove_listener() to detect when there is no listeners anymore. Switch to using async_update_listeners() to trigger updates on all listeners, and overload _unschedule_refresh() to detect when there is no listeners.

See the updated integration fetching documentation for more information on current use.

Logbook API removal of `entity_matches_only` flag

· One min read

Before 2022.6, the entity_matches_only flag prevented the logbook from providing context data in exchange for performance improvement when querying specific entities. Selecting the context data for particular entities is no longer an intensive process with the new logbook design. No immediate action is required as the flag will be ignored, and removing the flag from any active code paths can be done at your leisure.