Create reusable components instead of duplicating similar code patterns. When you find yourself writing similar classes, functions, or logic blocks, extract the common functionality into base classes, helper functions, or generic components that can be parameterized.
Create reusable components instead of duplicating similar code patterns. When you find yourself writing similar classes, functions, or logic blocks, extract the common functionality into base classes, helper functions, or generic components that can be parameterized.
Examples of duplication to avoid:
For instance, instead of creating separate button classes:
class SetSleepTimerButton(ButtonEntity):
# implementation
class ClearSleepTimerButton(ButtonEntity):
# similar implementation
Create a generic class with descriptions:
class GenericButton(ButtonEntity):
def __init__(self, description: ButtonEntityDescription):
self.entity_description = description
async def async_press(self) -> None:
await self.entity_description.press_fn()
This approach improves maintainability, reduces bugs, and makes code more readable by eliminating repetitive patterns.
Enter the URL of a public GitHub repository