Skip to content

Commit

Permalink
Pull attributes into the integration
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperb1iss committed Jul 25, 2024
1 parent ccd9568 commit 228e023
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
30 changes: 29 additions & 1 deletion custom_components/signalrgb/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any

from signalrgb.client import SignalRGBClient, SignalRGBException
from signalrgb.model import Effect

from homeassistant.components.light import (
ATTR_EFFECT,
Expand Down Expand Up @@ -55,6 +56,7 @@ def __init__(self, client: SignalRGBClient, config_entry: ConfigEntry) -> None:
self._last_active_effect: str | None = None
self._current_effect: str | None = None
self._effect_list: list[str] = []
self._effect_attributes: dict[str, Any] = {}

async def async_update(self) -> None:
"""Fetch new state data for this light."""
Expand All @@ -74,9 +76,11 @@ async def async_update(self) -> None:
if current_effect.attributes.name == ALL_OFF_EFFECT:
self._last_active_effect = None
self._attr_is_on = False
self._effect_attributes = {}
else:
self._current_effect = current_effect.attributes.name
self._attr_is_on = True
await self._update_effect_attributes(current_effect)

except SignalRGBException as err:
raise HomeAssistantError(f"Error communicating with API: {err}") from err
Expand All @@ -85,6 +89,25 @@ async def async_update(self) -> None:
"current_effect=%s is_on=%s", self._current_effect, self._attr_is_on
)

async def _update_effect_attributes(self, effect: Effect) -> None:
"""Update the effect attributes."""
self._effect_attributes = {
"effect_name": effect.attributes.name,
"effect_description": effect.attributes.description,
"effect_developer": effect.attributes.developer_effect,
"effect_publisher": effect.attributes.publisher,
"effect_uses_audio": effect.attributes.uses_audio,
"effect_uses_input": effect.attributes.uses_input,
"effect_uses_meters": effect.attributes.uses_meters,
"effect_uses_video": effect.attributes.uses_video,
"effect_parameters": effect.attributes.parameters,
}

@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return entity specific state attributes."""
return self._effect_attributes

@property
def effect_list(self) -> list[str]:
"""Return the list of supported effects."""
Expand Down Expand Up @@ -127,18 +150,23 @@ async def _apply_effect(self, effect: str) -> None:
LOGGER.debug("apply effect %s", effect)

try:
effect_obj: Effect = await self.hass.async_add_executor_job(
self._client.get_effect_by_name, effect
)
await self.hass.async_add_executor_job(
self._client.apply_effect_by_name, effect
self._client.apply_effect, effect_obj.id
)
except SignalRGBException as err:
LOGGER.error("Failed to apply effect %s: %s", effect, err)
raise HomeAssistantError(f"Failed to apply effect: {err}") from err

if effect == ALL_OFF_EFFECT:
self._last_active_effect = self._current_effect
self._effect_attributes = {}
else:
self._last_active_effect = self._current_effect
self._current_effect = effect
await self._update_effect_attributes(effect_obj)

self.async_write_ha_state()

Expand Down
18 changes: 12 additions & 6 deletions custom_components/signalrgb/manifest.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{
"domain": "signalrgb",
"name": "SignalRGB",
"codeowners": ["@hyperb1iss"],
"codeowners": [
"@hyperb1iss"
],
"config_flow": true,
"documentation": "https://github.com/hyperb1iss/signalrgb-homeassistant",
"iot_class": "assumed_state",
"issue_tracker": "https://github.com/hyperb1iss/signalrgb-homeassistant/issues",
"loggers": ["signalrgb"],
"requirements": ["signalrgb==0.9.5"],
"version": "0.2.0"
}
"loggers": [
"signalrgb"
],
"requirements": [
"signalrgb==0.9.6"
],
"version": "0.2.3",
"issue_tracker": "https://github.com/hyperb1iss/signalrgb-homeassistant/issues"
}

0 comments on commit 228e023

Please sign in to comment.