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

Code before:

    def __init__(
        self, client: WaterFurnaceData, description: SensorEntityDescription
    ) -> None:
        """Initialize the sensor."""
        self.client = client
        self.entity_description = description

        # This ensures that the sensors are isolated per waterfurnace unit
        self.entity_id = ENTITY_ID_FORMAT.format(
            f"wf_{slugify(self.client.unit)}_{slugify(description.key)}"
        )
        self._attr_unique_id = f"{self.client.unit}_{description.key}"

    @property
    def device_info(self) -> DeviceInfo:
        """Return device information."""
        device_info = DeviceInfo(
            identifiers={(DOMAIN, self.client.unit)},
            manufacturer="WaterFurnace",
        )

        if self.client.client.devices:
            for device in self.client.client.devices:
                if device.gwid == self.client.unit:
                    if device.description:
                        # Eg. Series 7
                        device_info["model"] = device.description
                    if device.awlabctypedesc:
                        # Eg. Series 7, 5 Ton
                        device_info["name"] = device.awlabctypedesc

                    break

        # Fallback name if no information found
        if "name" not in device_info:
            device_info["name"] = "WaterFurnace System"

        return device_info

    async def async_added_to_hass(self) -> None:
        """Register callbacks."""
        self.async_on_remove(
            async_dispatcher_connect(
                self.hass, UPDATE_TOPIC, self.async_update_callback
            )
        )

    @callback
    def async_update_callback(self):
        """Update state."""

Review comment (performance):
It feels kinda weird to have to iterate over all devices to get a device for which we are adding an entity. Future improvement might be, make the devices a `dict` and store the gwid as the key. this way it becomes a dict lookup which is way more efficient

Code after:
    """Implementing the Waterfurnace sensor."""

    _attr_should_poll = False
    _attr_has_entity_name = True

    def __init__(
        self, client: WaterFurnaceData, description: SensorEntityDescription
    ) -> None:
        """Initialize the sensor."""
        self.client = client
        self.entity_description = description

        # This ensures that the sensors are isolated per waterfurnace unit
        self.entity_id = ENTITY_ID_FORMAT.format(
            f"wf_{slugify(self.client.unit)}_{slugify(description.key)}"
        )
        self._attr_unique_id = f"{self.client.unit}_{description.key}"
        device_info = DeviceInfo(
            identifiers={(DOMAIN, self.client.unit)},
            manufacturer="WaterFurnace",
            name="WaterFurnace System",
        )

        if self.client.device_metadata:
            if self.client.device_metadata.description:
                # Eg. Series 7
                device_info["model"] = self.client.device_metadata.description
            if self.client.device_metadata.awlabctypedesc:
                # Eg. Series 7, 5 Ton
                device_info["name"] = self.client.device_metadata.awlabctypedesc

        self._attr_device_info = device_info

    async def async_added_to_hass(self) -> None:
        """Register callbacks."""
        self.async_on_remove(
            async_dispatcher_connect(
                self.hass, UPDATE_TOPIC, self.async_update_callback
            )
        )

    @callback
    def async_update_callback(self):
        """Update state."""
        if self.client.data is not None:
            self._attr_native_value = getattr(
                self.client.data, self.entity_description.key, None
            )
            self.async_write_ha_state()