Skip to content

Commit

Permalink
Added support for individual LED control (koying#8)
Browse files Browse the repository at this point in the history
* Added support for induvidual LED controls

* Fixed unique_ids from generating incorrectly

* cleanups for PR

* fixed an issue with full devices not initiating

* FIX: add device id to name

* CHG: add leds via config flow

Co-authored-by: Chris Browet <[email protected]>
  • Loading branch information
FeikoJoosten and koying authored Aug 17, 2021
1 parent 94f0850 commit d9d0c83
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 94 deletions.
35 changes: 31 additions & 4 deletions custom_components/openrgb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from homeassistant.exceptions import ConfigEntryNotReady

from .const import (
CONF_ADD_LEDS,
DEFAULT_ADD_LEDS,
DEFAULT_CLIENT_ID,
DEFAULT_PORT,
DOMAIN,
Expand All @@ -39,6 +41,7 @@
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_CLIENT_ID, default=DEFAULT_CLIENT_ID): cv.string,
vol.Optional(CONF_ADD_LEDS, default=DEFAULT_ADD_LEDS): cv.boolean,
}
)
},
Expand Down Expand Up @@ -139,6 +142,7 @@ def connection_failed():
ENTRY_IS_SETUP: set(),
"entities": {},
"pending": {},
"devices": {},
"unlistener": undo_listener,
"connection_failed": connection_failed,
"connection_recovered": connection_recovered,
Expand All @@ -157,8 +161,24 @@ async def async_load_devices(device_list):
device_type_list[ha_type].append(device)

entity_id = orgb_entity_id(device)
if entity_id not in hass.data[DOMAIN]["entities"]:
hass.data[DOMAIN]["entities"][entity_id] = None
device_unique_id = device.metadata.serial
# Some devices don't have a serial defined, so fall back to OpenRGB id
if not device_unique_id:
device_unique_id = entity_id

if entity_id not in hass.data[DOMAIN]["devices"]:
hass.data[DOMAIN]["devices"][entity_id] = []

# Stores the entire device as an entity
if device_unique_id not in hass.data[DOMAIN]["entities"]:
hass.data[DOMAIN]["entities"][device_unique_id] = None

if CONF_ADD_LEDS in config and config[CONF_ADD_LEDS]:
# Stores each LED of the device as an entity
for led in device.leds:
led_unique_id = f"{device_unique_id}_led_{led.id}"
if led_unique_id not in hass.data[DOMAIN]["entities"]:
hass.data[DOMAIN]["entities"][led_unique_id] = None

for ha_type, dev_ids in device_type_list.items():
config_entries_key = f"{ha_type}.openrgb"
Expand Down Expand Up @@ -216,13 +236,20 @@ async def async_poll_devices_update(event_time):
newlist_ids = []
for device in device_list:
newlist_ids.append(orgb_entity_id(device))
for dev_id in list(hass.data[DOMAIN]["entities"]):
for dev_id in list(hass.data[DOMAIN]["devices"]):
# Clean up stale devices, or alert them that new info is available.
if dev_id not in newlist_ids:
async_dispatcher_send(hass, SIGNAL_DELETE_ENTITY, dev_id)
hass.data[DOMAIN]["entities"].pop(dev_id)

for led_id in hass.data[DOMAIN]["devices"][dev_id]:
async_dispatcher_send(hass, SIGNAL_DELETE_ENTITY, led_id)

hass.data[DOMAIN]["devices"].pop(dev_id)
else:
async_dispatcher_send(hass, SIGNAL_UPDATE_ENTITY, dev_id)

for led_id in hass.data[DOMAIN]["devices"][dev_id]:
async_dispatcher_send(hass, SIGNAL_UPDATE_ENTITY, led_id)

autolog(">>>")

Expand Down
10 changes: 9 additions & 1 deletion custom_components/openrgb/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from homeassistant.const import CONF_CLIENT_ID, CONF_HOST, CONF_PORT
from homeassistant.core import callback

from .const import CONN_TIMEOUT, DEFAULT_CLIENT_ID, DEFAULT_PORT, DOMAIN
from .const import CONF_ADD_LEDS, CONN_TIMEOUT, DEFAULT_ADD_LEDS, DEFAULT_CLIENT_ID, DEFAULT_PORT, DOMAIN

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -47,6 +47,7 @@ def __init__(self):
self._host = None
self._port = DEFAULT_PORT
self._client_id = DEFAULT_CLIENT_ID
self._add_leds = DEFAULT_ADD_LEDS
self._is_import = False

async def async_step_import(self, user_input=None):
Expand All @@ -65,12 +66,14 @@ async def async_step_user(self, user_input=None):
vol.Required(CONF_HOST, default=self._host): str,
vol.Required(CONF_PORT, default=self._port): int,
vol.Required(CONF_CLIENT_ID, default=self._client_id): str,
vol.Required(CONF_ADD_LEDS, default=self._add_leds): bool,
}

if user_input is not None:
self._host = str(user_input[CONF_HOST])
self._port = user_input[CONF_PORT]
self._client_id = user_input[CONF_CLIENT_ID]
self._add_leds = user_input[CONF_ADD_LEDS]

try:
await asyncio.wait_for(
Expand All @@ -87,6 +90,7 @@ async def async_step_user(self, user_input=None):
CONF_HOST: self._host,
CONF_PORT: self._port,
CONF_CLIENT_ID: self._client_id,
CONF_ADD_LEDS: self._add_leds,
},
)

Expand Down Expand Up @@ -118,6 +122,7 @@ def __init__(self, config_entry):
self._host = config_entry.data[CONF_HOST] if CONF_HOST in config_entry.data else None
self._port = config_entry.data[CONF_PORT] if CONF_PORT in config_entry.data else DEFAULT_PORT
self._client_id = config_entry.data[CONF_CLIENT_ID] if CONF_CLIENT_ID in config_entry.data else DEFAULT_CLIENT_ID
self._add_leds = config_entry.data[CONF_ADD_LEDS] if CONF_ADD_LEDS in config_entry.data else DEFAULT_ADD_LEDS

async def async_step_init(self, user_input=None):
"""Manage the options."""
Expand All @@ -130,11 +135,13 @@ async def async_step_user(self, user_input=None):
self._host = str(user_input[CONF_HOST])
self._port = user_input[CONF_PORT]
self._client_id = user_input[CONF_CLIENT_ID]
self._add_leds = user_input[CONF_ADD_LEDS]

data_schema = {
vol.Required(CONF_HOST, default=self._host): str,
vol.Required(CONF_PORT, default=self._port): int,
vol.Required(CONF_CLIENT_ID, default=self._client_id): str,
vol.Required(CONF_ADD_LEDS, default=self._add_leds): bool,
}

if user_input is not None:
Expand All @@ -150,6 +157,7 @@ async def async_step_user(self, user_input=None):
CONF_HOST: self._host,
CONF_PORT: self._port,
CONF_CLIENT_ID: self._client_id,
CONF_ADD_LEDS: self._add_leds,
},
)

Expand Down
3 changes: 3 additions & 0 deletions custom_components/openrgb/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

TRACK_INTERVAL = timedelta(seconds=30)

CONF_ADD_LEDS = "add_leds"

DEFAULT_PORT = 6742
DEFAULT_CLIENT_ID = "Home Assistant"
DEFAULT_ADD_LEDS = False

CONN_TIMEOUT = 5.0
Loading

0 comments on commit d9d0c83

Please sign in to comment.