Repository: home-assistant/core
File: homeassistant/components/teslemetry/__init__.py (Python)

Code before:
        elif "state" in data:
            LOGGER.debug("Streaming received state from %s", vin)
            coordinator.data["state"] = data["state"]
            coordinator.async_set_updated_data(coordinator.data)

    return handle_vehicle_stream


def async_setup_energy_device(
    hass: HomeAssistant,
    entry: TeslemetryConfigEntry,
    energysite: TeslemetryEnergyData,
    device_registry: dr.DeviceRegistry,
) -> None:
    """Set up energy device with models, versions, and listeners."""
    # Add energy device models
    models = set()
    for gateway in energysite.info_coordinator.data.get("components_gateways", []):
        if gateway.get("part_name"):
            models.add(gateway["part_name"])
    for battery in energysite.info_coordinator.data.get("components_batteries", []):
        if battery.get("part_name"):
            models.add(battery["part_name"])
    if models:
        energysite.device["model"] = ", ".join(sorted(models))

    # Add software version
    if version := energysite.info_coordinator.data.get("version"):
        energysite.device["sw_version"] = version

    # Create the energy site device regardless of it having entities
    # This is so users with a Wall Connector but without a Powerwall can make service calls
    device_registry.async_get_or_create(
        config_entry_id=entry.entry_id, **energysite.device
    )

    # Register listener to update device sw_version when it changes
    entry.async_on_unload(
        energysite.info_coordinator.async_add_listener(
            create_energy_info_listener(
                hass, energysite.id, energysite.info_coordinator
            )
        )
    )


async def async_setup_stream(
    hass: HomeAssistant, entry: TeslemetryConfigEntry, vehicle: TeslemetryVehicleData
):
    """Set up the stream for a vehicle."""


Review comment (suggestion):
I think you can group this loop and remove a bit redundant code. Something in line such as:

```py
for component in [
    *components.get("components_gateways", []),
    *components.get("components_batteries", []),
]:
    if part_name := component.get("part_name"):
        models.add(part_name)

if models:
    energysite.device["model"] = ", ".join
```

Code after:
        elif "state" in data:
            LOGGER.debug("Streaming received state from %s", vin)
            coordinator.data["state"] = data["state"]
            coordinator.async_set_updated_data(coordinator.data)

    return handle_vehicle_stream


def async_setup_energy_device(
    hass: HomeAssistant,
    entry: TeslemetryConfigEntry,
    energysite: TeslemetryEnergyData,
    device_registry: dr.DeviceRegistry,
) -> None:
    """Set up energy device with models, versions, and listeners."""
    data = energysite.info_coordinator.data
    models = set()
    for component in (
        *data.get("components_gateways", []),
        *data.get("components_batteries", []),
    ):
        if part_name := component.get("part_name"):
            models.add(part_name)
    if models:
        energysite.device["model"] = ", ".join(sorted(models))

    if version := data.get("version"):
        energysite.device["sw_version"] = version

    device_registry.async_get_or_create(
        config_entry_id=entry.entry_id, **energysite.device
    )

    entry.async_on_unload(
        energysite.info_coordinator.async_add_listener(
            create_energy_info_listener(
                hass, energysite.id, energysite.info_coordinator
            )
        )
    )


async def async_setup_stream(
    hass: HomeAssistant, entry: TeslemetryConfigEntry, vehicle: TeslemetryVehicleData
):
    """Set up the stream for a vehicle."""

    await vehicle.stream_vehicle.get_config()
    entry.async_create_background_task(
        hass,
        vehicle.stream_vehicle.prefer_typed(True),