Skip to content

Commit

Permalink
Get hex_value based on type and handle missing values (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 authored May 4, 2023
1 parent 9cd4dd5 commit e25b31f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
1 change: 0 additions & 1 deletion custom_components/goxlr_utility/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class GoXLRUtilityLightEntityDescription(LightEntityDescription):

item_type: ItemType = ItemType.ACCENT
item_key: str = ""
hex: Callable = round


@dataclass
Expand Down
45 changes: 28 additions & 17 deletions custom_components/goxlr_utility/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async def async_setup_entry(
name="Accent",
icon="mdi:television-ambient-light",
item_type=ItemType.ACCENT,
hex=lambda data, _: data.lighting.simple.accent.colour_one,
),
]

Expand All @@ -50,9 +49,6 @@ async def async_setup_entry(
else None,
item_type=ItemType.BUTTON_ACTIVE,
item_key=key,
hex=lambda data, item_key: getattr(
data.lighting.buttons, item_key
).colours.colour_one,
),
GoXLRUtilityLightEntityDescription(
key=f"light_button_{key}_inactive",
Expand All @@ -62,9 +58,6 @@ async def async_setup_entry(
else None,
item_type=ItemType.BUTTON_INACTIVE,
item_key=key,
hex=lambda data, item_key: getattr(
data.lighting.buttons, item_key
).colours.colour_two,
),
]
)
Expand All @@ -81,9 +74,6 @@ async def async_setup_entry(
else None,
item_type=ItemType.FADER_TOP,
item_key=key,
hex=lambda data, item_key: getattr(
data.lighting.faders, item_key
).colours.colour_one,
),
GoXLRUtilityLightEntityDescription(
key=f"light_fader_{key}_bottom",
Expand All @@ -93,9 +83,6 @@ async def async_setup_entry(
else None,
item_type=ItemType.FADER_BOTTOM,
item_key=key,
hex=lambda data, item_key: getattr(
data.lighting.faders, item_key
).colours.colour_two,
),
]
)
Expand Down Expand Up @@ -143,10 +130,34 @@ def is_on(self) -> bool:
@property
def rgb_color(self) -> tuple[int, int, int] | None:
"""Return the rgb color value [int, int, int]."""
hex_value = self.entity_description.hex(
self.coordinator.data,
self.entity_description.item_key,
)
hex_value: str | None = None
if self.entity_description.item_type == ItemType.ACCENT:
hex_value = self.coordinator.data.lighting.simple.accent.colour_one
elif self.entity_description.item_type == ItemType.BUTTON_ACTIVE:
item = getattr(
self.coordinator.data.lighting.buttons,
self.entity_description.item_key,
)
hex_value = item.colours.colour_one if item else None
elif self.entity_description.item_type == ItemType.BUTTON_INACTIVE:
item = getattr(
self.coordinator.data.lighting.buttons,
self.entity_description.item_key,
)
hex_value = item.colours.colour_two if item else None
elif self.entity_description.item_type == ItemType.FADER_TOP:
item = getattr(
self.coordinator.data.lighting.faders,
self.entity_description.item_key,
)
hex_value = item.colours.colour_one if item else None
elif self.entity_description.item_type == ItemType.FADER_BOTTOM:
item = getattr(
self.coordinator.data.lighting.faders,
self.entity_description.item_key,
)
hex_value = item.colours.colour_two if item else None

return (
cast(tuple[int, int, int], tuple(color_util.rgb_hex_to_rgb_list(hex_value)))
if hex_value
Expand Down

0 comments on commit e25b31f

Please sign in to comment.