Repository: home-assistant/core
File: homeassistant/components/route_b_smart_meter/sensor.py (Python)

Code before:
        key=ATTR_API_TOTAL_CONSUMPTION,
        translation_key=ATTR_API_TOTAL_CONSUMPTION,
        device_class=SensorDeviceClass.ENERGY,
        state_class=SensorStateClass.TOTAL,
        native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
        value_accessor=lambda data: data.total_consumption,
    ),
)



def _build_device_info(coordinator: BRouteUpdateCoordinator) -> DeviceInfo:
    """Build device information from coordinator data."""
    return DeviceInfo(
        identifiers={(DOMAIN, coordinator.bid)},
        name=f"Route B Smart Meter {coordinator.bid}",
        manufacturer=coordinator.device_info_data.manufacturer_code,
        serial_number=coordinator.device_info_data.serial_number,
        sw_version=coordinator.device_info_data.echonet_version,
    )


async def async_setup_entry(
    hass: HomeAssistant,
    entry: BRouteConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up Smart Meter B-route entry."""
    coordinator = entry.runtime_data

    async_add_entities(
        SmartMeterBRouteSensor(coordinator, description)
        for description in SENSOR_DESCRIPTIONS
    )


class SmartMeterBRouteSensor(CoordinatorEntity[BRouteUpdateCoordinator], SensorEntity):
    """Representation of a Smart Meter B-route sensor entity."""

    _attr_has_entity_name = True

    def __init__(
        self,
        coordinator: BRouteUpdateCoordinator,
        description: SensorEntityDescriptionWithValueAccessor,
    ) -> None:
        """Initialize Smart Meter B-route sensor entity."""
        super().__init__(coordinator)
        self.entity_description: SensorEntityDescriptionWithValueAccessor = description
        self._attr_unique_id = f"{coordinator.bid}_{description.key}"
        self._attr_device_info = _build_device_info(coordinator)

Review comment (suggestion):
```suggestion

_DEVICE_INFO_MAPPING: dict[
    Literal["manufacturer", "serial_number", "sw_version"],
    Callable[[BRouteUpdateCoordinator], str | None],
] = {
    "manufacturer": lambda coordinator: coordinator.device_info_data.manufacturer_code,
    "serial_number": lambda coordinator: coordinator.device_info_data.serial_number,
    "sw_version": lambda coordinator: coordinator.device_info_data.echonet_version,
}


def _build_device_info(coordinator: BRouteUpdateCoordinator) -> DeviceInfo:
    """Build device information from coordinator data."""
    device = DeviceInfo(
        identifiers={(DOMAIN, coordinator.bid)},
        name=f"Route B Smart Meter {coordinator.bid}",
    )
    for key, fn in _DEVICE_INFO_MAPPING.items():
        if (value := fn(coordinator)) is not None:
            device[key] = value
    return device
```

As codex has suggested we should only update values that are not None

Code after:
    SensorEntityDescriptionWithValueAccessor(
        key=ATTR_API_TOTAL_CONSUMPTION,
        translation_key=ATTR_API_TOTAL_CONSUMPTION,
        device_class=SensorDeviceClass.ENERGY,
        state_class=SensorStateClass.TOTAL,
        native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
        value_accessor=lambda data: data.total_consumption,
    ),
)

_DEVICE_INFO_MAPPING: dict[
    Literal["manufacturer", "serial_number", "sw_version"],
    Callable[[BRouteUpdateCoordinator], str | None],
] = {
    "manufacturer": lambda coordinator: coordinator.device_info_data.manufacturer_code,
    "serial_number": lambda coordinator: coordinator.device_info_data.serial_number,
    "sw_version": lambda coordinator: coordinator.device_info_data.echonet_version,
}


def _build_device_info(coordinator: BRouteUpdateCoordinator) -> DeviceInfo:
    """Build device information from coordinator data."""
    device = DeviceInfo(
        identifiers={(DOMAIN, coordinator.bid)},
        name=f"Route B Smart Meter {coordinator.bid}",
    )
    for key, fn in _DEVICE_INFO_MAPPING.items():
        if (value := fn(coordinator)) is not None:
            device[key] = value
    return device


async def async_setup_entry(
    hass: HomeAssistant,
    entry: BRouteConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up Smart Meter B-route entry."""
    coordinator = entry.runtime_data

    async_add_entities(
        SmartMeterBRouteSensor(coordinator, description)
        for description in SENSOR_DESCRIPTIONS
    )


class SmartMeterBRouteSensor(CoordinatorEntity[BRouteUpdateCoordinator], SensorEntity):
    """Representation of a Smart Meter B-route sensor entity."""

    _attr_has_entity_name = True