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

Code before:
    """Representation of a SwitchBot Cloud sensor entity."""

    def __init__(
        self,
        api: SwitchBotAPI,
        device: Device,
        coordinator: SwitchBotCoordinator,
        description: SensorEntityDescription,
    ) -> None:
        """Initialize SwitchBot Cloud sensor entity."""
        super().__init__(api, device, coordinator)
        self.entity_description = description
        self._attr_unique_id = f"{device.device_id}_{description.key}"

    def _set_attributes(self) -> None:
        """Set attributes from coordinator data."""
        if not self.coordinator.data:
            return
        if isinstance(self.entity_description, SwitchbotCloudSensorEntityDescription):
            self._attr_native_value = self.entity_description.value_fn(
                self.coordinator.data
            )
        else:
            self._attr_native_value = self.coordinator.data.get(
                self.entity_description.key
            )


class SwitchBotCloudRelaySwitch2PMSensor(SwitchBotCloudSensor):
    """Representation of a SwitchBot Cloud Relay Switch 2PM sensor entity."""

    def __init__(
        self,
        api: SwitchBotAPI,
        device: Device,
        coordinator: SwitchBotCoordinator,
        description: SensorEntityDescription,
        channel: str,
    ) -> None:
        """Initialize SwitchBot Cloud sensor entity."""
        super().__init__(api, device, coordinator, description)

        self.entity_description = description
        self._channel = channel
        self._attr_unique_id = f"{device.device_id}-{description.key}-{channel}"
        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, f"{device.device_name}-channel-{channel}")},
            manufacturer="SwitchBot",
            model=device.device_type,
            model_id="RelaySwitch2PM",
            name=f"{device.device_name} Channel {channel}",

Review comment (performance):
Calling `isinstance` is relatively expensive. Instead, as `value_fn`'s default is just returning the value, let's just turn all entity descriptions in a `SwitchbotCloudSensorEntityDescription` and do
```suggestion
        value = self.entity_description.value_fn(self.coordinator.data)
        self._attr_native_value = self.coordinator.data.value_fn(value)
```
This way we don't change this logic and it will just act like a modifier

Code after:

class SwitchBotCloudSensor(SwitchBotCloudEntity, SensorEntity):
    """Representation of a SwitchBot Cloud sensor entity."""

    entity_description: SwitchbotCloudSensorEntityDescription

    def __init__(
        self,
        api: SwitchBotAPI,
        device: Device,
        coordinator: SwitchBotCoordinator,
        description: SwitchbotCloudSensorEntityDescription,
    ) -> None:
        """Initialize SwitchBot Cloud sensor entity."""
        super().__init__(api, device, coordinator)
        self.entity_description = description
        self._attr_unique_id = f"{device.device_id}_{description.key}"

    def _set_attributes(self) -> None:
        """Set attributes from coordinator data."""
        if not self.coordinator.data:
            return
        value = self.coordinator.data.get(self.entity_description.key)

        self._attr_native_value = self.entity_description.value_fn(value)


class SwitchBotCloudRelaySwitch2PMSensor(SwitchBotCloudSensor):
    """Representation of a SwitchBot Cloud Relay Switch 2PM sensor entity."""

    def __init__(
        self,
        api: SwitchBotAPI,
        device: Device,
        coordinator: SwitchBotCoordinator,
        description: SwitchbotCloudSensorEntityDescription,
        channel: str,
    ) -> None:
        """Initialize SwitchBot Cloud sensor entity."""
        super().__init__(api, device, coordinator, description)

        self.entity_description = description
        self._channel = channel
        self._attr_unique_id = f"{device.device_id}-{description.key}-{channel}"
        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, f"{device.device_name}-channel-{channel}")},
            manufacturer="SwitchBot",
            model=device.device_type,
            model_id="RelaySwitch2PM",
            name=f"{device.device_name} Channel {channel}",
        )