Repository: home-assistant/core
File: homeassistant/components/tibber/binary_sensor.py (Python)

Code before:


def _device_status_is_on(value: str | None) -> bool | None:
    """Map device status value to binary sensor state."""
    if value == "on":
        return True
    if value == "off":
        return False
    return None


DATA_API_BINARY_SENSORS: tuple[TibberBinarySensorEntityDescription, ...] = (
    TibberBinarySensorEntityDescription(
        key="connector.status",
        device_class=BinarySensorDeviceClass.PLUG,
        is_on_fn=_connector_status_is_on,
    ),
    TibberBinarySensorEntityDescription(
        key="charging.status",
        device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
        is_on_fn=_charging_status_is_on,
    ),
    TibberBinarySensorEntityDescription(
        key="onOff",
        device_class=BinarySensorDeviceClass.POWER,
        is_on_fn=_device_status_is_on,
    ),
)


async def async_setup_entry(
    hass: HomeAssistant,
    entry: TibberConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the Tibber binary sensors."""
    coordinator = entry.runtime_data.data_api_coordinator
    assert coordinator is not None

    entities: list[TibberDataAPIBinarySensor] = []
    api_binary_sensors = {sensor.key: sensor for sensor in DATA_API_BINARY_SENSORS}

    for device in coordinator.data.values():
        for sensor in device.sensors:
            description: TibberBinarySensorEntityDescription | None = (
                api_binary_sensors.get(sensor.id)
            )
            if description is None:
                continue
            entities.append(TibberDataAPIBinarySensor(coordinator, device, description))
    async_add_entities(entities)

Review comment (suggestion):
```suggestion
        is_on_fn={"on": True, "off": False}.get,
```

Code after:
    ),
)


async def async_setup_entry(
    hass: HomeAssistant,
    entry: TibberConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the Tibber binary sensors."""
    coordinator = entry.runtime_data.data_api_coordinator
    assert coordinator is not None

    entities: list[TibberDataAPIBinarySensor] = []
    api_binary_sensors = {sensor.key: sensor for sensor in DATA_API_BINARY_SENSORS}

    for device in coordinator.data.values():
        for sensor in device.sensors:
            description: TibberBinarySensorEntityDescription | None = (
                api_binary_sensors.get(sensor.id)
            )
            if description is None:
                continue
            entities.append(TibberDataAPIBinarySensor(coordinator, device, description))
    async_add_entities(entities)


class TibberDataAPIBinarySensor(
    CoordinatorEntity[TibberDataAPICoordinator], BinarySensorEntity
):
    """Representation of a Tibber Data API binary sensor."""

    _attr_has_entity_name = True
    entity_description: TibberBinarySensorEntityDescription

    def __init__(
        self,
        coordinator: TibberDataAPICoordinator,
        device: TibberDevice,
        entity_description: TibberBinarySensorEntityDescription,
    ) -> None:
        """Initialize the binary sensor."""
        super().__init__(coordinator)

        self._device_id: str = device.id
        self.entity_description = entity_description

        self._attr_unique_id = f"{device.id}_{entity_description.key}"

        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, device.external_id)},