Skip to content

Commit

Permalink
Updated from template (using script)
Browse files Browse the repository at this point in the history
  • Loading branch information
3lv committed Nov 14, 2024
1 parent cd7e0f8 commit 23a8b4b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ SMioplus:

- Only specific entities for different stack levels:

> The following example is provided for illustrative purposes only and does not necessarily represent real entities.
> !The following example is provided for illustrative purposes only and does NOT necessarily represent real entities!

```yaml
SMioplus:
Expand All @@ -124,7 +124,7 @@ SMioplus:
### `configuration.yaml` entities

Possible entities:
```
```yaml
opto_cnt_rst_1: -> opto_cnt_rst_8: (type: button)
dac_1: -> dac_4: (type: number)
od_1: -> od_4: (type: number)
Expand Down
65 changes: 40 additions & 25 deletions custom_components/SMioplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CONF_TYPE = "type"
CONF_CHAN = "chan"
CONF_CHAN_RANGE = "chan_range"
CONF_CHANNELS = "channels"
CONF_UPDATE_INTERVAL = "update_interval" # In seconds
COM_NOGET = "__NOGET__"

Expand Down Expand Up @@ -57,6 +58,17 @@ async def SM_load_all_platforms(hass, stack=0):
)


def create_entity_config(config, entity, stack, type, chan, update_interval):
entity_config = {
**config,
CONF_NAME: NAME_PREFIX + str(stack) + "_" + entity + "_" + str(chan),
CONF_STACK: stack,
CONF_TYPE: type,
CONF_CHAN: chan,
CONF_UPDATE_INTERVAL: update_interval,
}
return entity_config

async def async_setup(hass, config):
hass.data[DOMAIN] = []
card_configs = config.get(DOMAIN)
Expand All @@ -70,37 +82,40 @@ async def async_setup(hass, config):
continue
for entity in card_config:
card_config[entity] = card_config[entity] or {}
channels = card_config[entity].get(CONF_CHANNELS)
chan_range = card_config[entity].get(CONF_CHAN_RANGE)
update_interval = card_config[entity].get(CONF_UPDATE_INTERVAL)
try:
[chan_start, chan_end] = chan_range.split("..", 1)
chan_start = int(chan_start)
chan_end = int(chan_end)
type = entity
for chan in range(chan_start, chan_end + 1, 1):
entity_config = card_config[entity].copy()
entity_config |= {
CONF_NAME: NAME_PREFIX + str(stack) + "_" + entity + "_" + str(chan),
CONF_STACK: stack,
CONF_TYPE: type,
CONF_CHAN: chan,
CONF_UPDATE_INTERVAL: update_interval,
}
for chan in channels.split(','):
chan = int(chan)
entity_config = create_entity_config(
card_config[entity], entity,
stack, type, chan, update_interval
);
await SM_load_platform(hass, entity_config)
except:
try:
[type, chan] = entity.rsplit("_", 1)
chan = int(chan)
[chan_start, chan_end] = chan_range.split("..", 1)
chan_start = int(chan_start)
chan_end = int(chan_end)
type = entity
for chan in range(chan_start, chan_end + 1, 1):
entity_config = create_entity_config(
card_config[entity], entity,
stack, type, chan, update_interval
);
await SM_load_platform(hass, entity_config)
except:
_LOGGER.error("%s doesn't respect type_channel format", entity)
continue
entity_config = card_config[entity].copy()
entity_config |= {
CONF_NAME: NAME_PREFIX + str(stack) + "_" + entity,
CONF_STACK: stack,
CONF_TYPE: type,
CONF_CHAN: chan,
CONF_UPDATE_INTERVAL: update_interval,
}
await SM_load_platform(hass, entity_config)
try:
[type, chan] = entity.rsplit("_", 1)
chan = int(chan)
except:
_LOGGER.error("%s doesn't respect type_channel format", entity)
continue
entity_config = create_entity_config(
card_config[entity], entity,
stack, type, chan, update_interval
);
await SM_load_platform(hass, entity_config)
return True

0 comments on commit 23a8b4b

Please sign in to comment.