Repository: home-assistant/core
File: homeassistant/components/airos/button.py (Python)

Code before:
    device_class=ButtonDeviceClass.RESTART,
    translation_key="reboot_device",
    entity_registry_enabled_default=False,
)


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: AirOSConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the AirOS button from a config entry."""
    async_add_entities([AirOSRebootButton(config_entry.runtime_data)])


class AirOSRebootButton(AirOSEntity, ButtonEntity):
    """Button to reboot device."""

    entity_description: ButtonEntityDescription

    def __init__(self, coordinator: AirOSDataUpdateCoordinator) -> None:
        """Initialize the AirOS client button."""
        super().__init__(coordinator)
        self.entity_description = BUTTON_DESCRIPTION

        self._attr_unique_id = f"{coordinator.data.derived.mac}_reboot"

    async def async_press(self) -> None:
        """Handle the button press to reboot the device."""
        try:
            await self.coordinator.airos_device.login()
            result = await self.coordinator.airos_device.reboot()

        except AirOSException as err:
            _LOGGER.exception("Failed to send reboot request to device")
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="cannot_connect",
            ) from err
        else:
            if not result:
                _LOGGER.error("Device indicates it failed to initiate reboot")
                raise HomeAssistantError(
                    translation_domain=DOMAIN,
                    translation_key="reboot_failed",
                ) from None


Review comment (refactor):
```suggestion
    def __init__(self, coordinator: AirOSDataUpdateCoordinator, entity_description: ButtonEntityDescription) -> None:
        """Initialize the AirOS client button."""
        super().__init__(coordinator)
        self.entity_description = ButtonEntityDescription

        self._attr_unique_id = f"{coordinator.data.derived.mac}_{entity_description.key}"
```
I think this is a bit more modular approach, like the DRY-approach. So this way you can easily add more buttons in the future. :)

Code after:
    device_class=ButtonDeviceClass.RESTART,
    entity_registry_enabled_default=False,
)


async def async_setup_entry(
    hass: HomeAssistant,
    config_entry: AirOSConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the AirOS button from a config entry."""
    async_add_entities([AirOSRebootButton(config_entry.runtime_data, REBOOT_BUTTON)])


class AirOSRebootButton(AirOSEntity, ButtonEntity):
    """Button to reboot device."""

    entity_description: ButtonEntityDescription

    def __init__(
        self,
        coordinator: AirOSDataUpdateCoordinator,
        description: ButtonEntityDescription,
    ) -> None:
        """Initialize the AirOS client button."""
        super().__init__(coordinator)

        self.entity_description = description
        self._attr_unique_id = f"{coordinator.data.derived.mac}_{description.key}"

    async def async_press(self) -> None:
        """Handle the button press to reboot the device."""
        try:
            await self.coordinator.airos_device.login()
            result = await self.coordinator.airos_device.reboot()

        except AirOSException as err:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="cannot_connect",
            ) from err

        if not result:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="reboot_failed",
            ) from None