Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement base entity class for Twinkly #134382

Merged
merged 3 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions homeassistant/components/twinkly/entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Base entity for Twinkly."""

from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DEV_MODEL, DEV_NAME, DOMAIN
from .coordinator import TwinklyCoordinator


class TwinklyEntity(CoordinatorEntity[TwinklyCoordinator]):
"""Defines a base Twinkly entity."""

_attr_has_entity_name = True

def __init__(self, coordinator: TwinklyCoordinator) -> None:
"""Initialize."""
super().__init__(coordinator)
device_info = coordinator.data.device_info
mac = device_info["mac"]
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, mac)},
connections={(CONNECTION_NETWORK_MAC, mac)},
manufacturer="LEDWORKS",
model=device_info[DEV_MODEL],
name=device_info[DEV_NAME],
sw_version=coordinator.software_version,
)
25 changes: 4 additions & 21 deletions homeassistant/components/twinkly/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@
LightEntityFeature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import TwinklyConfigEntry, TwinklyCoordinator
from .const import (
DEV_LED_PROFILE,
DEV_MODEL,
DEV_NAME,
DEV_PROFILE_RGB,
DEV_PROFILE_RGBW,
DOMAIN,
)
from .const import DEV_LED_PROFILE, DEV_PROFILE_RGB, DEV_PROFILE_RGBW
from .entity import TwinklyEntity

_LOGGER = logging.getLogger(__name__)

Expand All @@ -43,18 +35,17 @@ async def async_setup_entry(
async_add_entities([entity], update_before_add=True)


class TwinklyLight(CoordinatorEntity[TwinklyCoordinator], LightEntity):
class TwinklyLight(TwinklyEntity, LightEntity):
"""Implementation of the light for the Twinkly service."""

_attr_has_entity_name = True
_attr_name = None
_attr_translation_key = "light"

def __init__(self, coordinator: TwinklyCoordinator) -> None:
"""Initialize a TwinklyLight entity."""
super().__init__(coordinator)
device_info = coordinator.data.device_info
self._attr_unique_id = mac = device_info["mac"]
self._attr_unique_id = device_info["mac"]

if device_info.get(DEV_LED_PROFILE) == DEV_PROFILE_RGBW:
self._attr_supported_color_modes = {ColorMode.RGBW}
Expand All @@ -68,14 +59,6 @@ def __init__(self, coordinator: TwinklyCoordinator) -> None:
self._attr_supported_color_modes = {ColorMode.BRIGHTNESS}
self._attr_color_mode = ColorMode.BRIGHTNESS
self.client = coordinator.client
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, mac)},
connections={(CONNECTION_NETWORK_MAC, mac)},
manufacturer="LEDWORKS",
model=device_info[DEV_MODEL],
name=device_info[DEV_NAME],
sw_version=coordinator.software_version,
)
if coordinator.supports_effects:
self._attr_supported_features = LightEntityFeature.EFFECT
self._update_attr()
Expand Down