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

Update lights when coordinator data changes and add configuration for scanning intervals #9

Merged
merged 1 commit into from
Aug 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions custom_components/luxor/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

from .const import (
CONF_HOST,
CONF_GROUP_INTERVAL,
CONF_THEME_INTERVAL,
DOMAIN,
PLATFORMS,
DEFAULT_GROUP_INTERVAL,
DEFAULT_THEME_INTERVAL,
)


Expand Down Expand Up @@ -42,6 +46,8 @@ async def async_step_user(self, user_input=None):
user_input = {}
# Provide defaults for form
user_input[CONF_HOST] = ""
user_input[CONF_GROUP_INTERVAL] = DEFAULT_GROUP_INTERVAL
user_input[CONF_THEME_INTERVAL] = DEFAULT_THEME_INTERVAL

return await self._show_config_form(user_input)

Expand All @@ -52,6 +58,8 @@ async def _show_config_form(self, user_input): # pylint: disable=unused-argumen
data_schema=vol.Schema(
{
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): str,
vol.Required(CONF_GROUP_INTERVAL, default=user_input[CONF_GROUP_INTERVAL]): vol.All(vol.Coerce(int), vol.Range(min=5)),
vol.Required(CONF_THEME_INTERVAL, default=user_input[CONF_THEME_INTERVAL]): vol.All(vol.Coerce(int), vol.Range(min=60))
}
),
errors=self._errors,
Expand Down
4 changes: 4 additions & 0 deletions custom_components/luxor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@

# Configuration and options
CONF_HOST = "host"
CONF_GROUP_INTERVAL = "group_interval"
CONF_THEME_INTERVAL = "theme_interval"

# Defaults
DEFAULT_NAME = DOMAIN
DEFAULT_GROUP_INTERVAL = 60
DEFAULT_THEME_INTERVAL = 600

# How long to wait to actually do the refresh after requesting it.
# We wait some time so if we control multiple lights, we batch requests.
Expand Down
31 changes: 12 additions & 19 deletions custom_components/luxor/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
DOMAIN,
LIGHT,
REQUEST_REFRESH_DELAY,
CONF_GROUP_INTERVAL,
DEFAULT_GROUP_INTERVAL,
)

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=60)


async def async_setup_entry(hass, entry, async_add_entities):
controller = hass.data[DOMAIN][entry.entry_id]

Expand All @@ -39,7 +38,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
_LOGGER,
name="{}_{}".format(DOMAIN, LIGHT),
update_method=partial(async_fetch_lights, controller),
update_interval=SCAN_INTERVAL,
update_interval=timedelta(seconds=entry.data.get(CONF_GROUP_INTERVAL, DEFAULT_GROUP_INTERVAL)),
request_refresh_debouncer=Debouncer(
hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=True
),
Expand Down Expand Up @@ -77,9 +76,7 @@ def async_update_lights(
for grp_id, data in controller.lights.items():
if grp_id in current_entities:
continue
light = current_entities[grp_id] = create_light(
data.grp, data.name, data.inten, data.colr
)
light = current_entities[grp_id] = create_light(data.grp)
new_items.append(light)

hass.async_create_task(async_remove_entities(controller, current_entities))
Expand Down Expand Up @@ -107,14 +104,11 @@ def brightness_to_intensity(brightness: int):
class LuxorLight(CoordinatorEntity, LightEntity):
supported_color_modes = {COLOR_MODE_BRIGHTNESS}

def __init__(self, coordinator, controller, group_id, name, intensity, color):
def __init__(self, coordinator, controller, group_id):
super().__init__(coordinator)
self.controller = controller
self.group_id = group_id
self.intensity = intensity
self.color = color
self._attr_unique_id = "{}{}".format(name, group_id)
self._attr_name = name
self._attr_unique_id = "LUXOR_LIGHT_{}".format(group_id)

async def async_turn_on(
self, brightness=255, **kwargs
Expand All @@ -126,8 +120,6 @@ async def async_turn_on(
luxor_openapi_asyncio.IlluminateGroupRequest(self.group_id, intensity)
)

if api_response.status == 0:
self.intensity = intensity
await self.coordinator.async_request_refresh()

async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
Expand All @@ -136,18 +128,19 @@ async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
luxor_openapi_asyncio.IlluminateGroupRequest(self.group_id, 0)
)

if api_response.status == 0:
self.intensity = 0

await self.coordinator.async_request_refresh()

@property
def name(self):
return self.controller.lights[self.group_id].name

@property
def brightness(self):
return intensity_to_brightness(self.intensity)
return intensity_to_brightness(self.controller.lights[self.group_id].inten)

@property
def is_on(self):
return self.intensity > 0
return self.controller.lights[self.group_id].inten > 0

@property
def device_info(self):
Expand Down
7 changes: 3 additions & 4 deletions custom_components/luxor/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
DOMAIN,
SCENE,
REQUEST_REFRESH_DELAY,
CONF_THEME_INTERVAL,
DEFAULT_THEME_INTERVAL,
)

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=600)


async def async_setup_entry(hass, entry, async_add_entities):
controller = hass.data[DOMAIN][entry.entry_id]

Expand All @@ -36,7 +35,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
_LOGGER,
name="{}_{}".format(DOMAIN, SCENE),
update_method=partial(async_fetch_scenes, controller),
update_interval=SCAN_INTERVAL,
update_interval=timedelta(seconds=entry.data.get(CONF_THEME_INTERVAL, DEFAULT_THEME_INTERVAL)),
request_refresh_debouncer=Debouncer(
hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=True
),
Expand Down
4 changes: 3 additions & 1 deletion custom_components/luxor/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"title": "Luxor",
"description": "Integration with FXLuminaire's Luxor lighting system.",
"data": {
"host": "Host"
"host": "Host",
"group_interval": "Light Update Interval (sec)",
"theme_interval": "Scene Update Interval (sec)"
}
}
},
Expand Down