Skip to content

Commit

Permalink
update_interval + readme
Browse files Browse the repository at this point in the history
  • Loading branch information
3lv committed Sep 22, 2024
1 parent 1b79d49 commit a229c8b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,27 @@ SMioplus:
- stack: 3
```

- Only specific entities:
- Only specific entities for different stack levels:

```yaml
SMioplus:
- stack: 0
relay_1:
relay_2:
opto_4:
- stack: 1
od_3:
relay_1:
relay_3:
opto_1:
update_interval: 0.1
- stack: 2
relay:
chan_range: "1..8"
opto_cnt:
chan_range: "2..6"
update_interval: 1
```

### `configuration.yaml` entities

Find possible entities in [data.py](https://github.com/SequentMicrosystems/SMioplus-ha/blob/master/custom_components/SMioplus/data.py)

Entity options:
- `chan_range: "start..end"` (specify inclusive channel range)
- `update_interval: seconds` (specify the update interval for `sensor` and `binary_sensor`, default 30s)
12 changes: 7 additions & 5 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_UPDATE_INTERVAL = "update_interval" # In seconds
COM_NOGET = "__NOGET__"


Expand Down Expand Up @@ -69,22 +70,22 @@ async def async_setup(hass, config):
continue
for entity in card_config:
card_config[entity] = card_config[entity] or {}
chan_range = card_config[entity].get(CONF_CHAN_RANGE, "")
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):
#_LOGGER.error("DEBUG chan: %d", chan)
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_CHAN: chan,
CONF_UPDATE_INTERVAL: update_interval,
}
_LOGGER.error("DEBUG chan from config: %d", entity_config[CONF_CHAN])
await SM_load_platform(hass, entity_config)
except:
try:
Expand All @@ -98,7 +99,8 @@ async def async_setup(hass, config):
CONF_NAME: NAME_PREFIX + str(stack) + "_" + entity,
CONF_STACK: stack,
CONF_TYPE: type,
CONF_CHAN: chan
CONF_CHAN: chan,
CONF_UPDATE_INTERVAL: update_interval,
}
await SM_load_platform(hass, entity_config)
return True
14 changes: 7 additions & 7 deletions custom_components/SMioplus/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from . import (
DOMAIN, CONF_STACK, CONF_TYPE, CONF_CHAN, CONF_NAME,
CONF_UPDATE_INTERVAL,
COM_NOGET,
SM_MAP, SM_API
)
Expand All @@ -27,23 +28,23 @@ async def async_setup_platform(hass, config, add_devices, discovery_info=None):
type=discovery_info.get(CONF_TYPE)
if SM_MAP[type]["com"]["get"] == COM_NOGET:
add_devices([Number_NOGET(
name=discovery_info.get(CONF_NAME, ""),
hass=hass,
name=discovery_info.get(CONF_NAME),
stack=discovery_info.get(CONF_STACK, 0),
type=discovery_info.get(CONF_TYPE),
chan=discovery_info.get(CONF_CHAN),
hass=hass
)])
else:
add_devices([Number(
name=discovery_info.get(CONF_NAME, ""),
hass=hass,
name=discovery_info.get(CONF_NAME),
stack=discovery_info.get(CONF_STACK, 0),
type=discovery_info.get(CONF_TYPE),
chan=discovery_info.get(CONF_CHAN),
hass=hass
)])

class Number(NumberEntity):
def __init__(self, name, stack, type, chan, hass):
def __init__(self, hass, name, stack, type, chan):
generated_name = DOMAIN + str(stack) + "_" + type + "_" + str(chan)
self._unique_id = generate_entity_id("number.{}", generated_name, hass=hass)
self._name = name or generated_name
Expand All @@ -59,8 +60,7 @@ def __init__(self, name, stack, type, chan, hass):
self._step = SM_MAP[self._type]["step"]
self._value = 0
self.__SM__init()
### CUSTOM_SETUP START
### CUSTOM_SETUP END
### CUSTOM_SETUP START

def __SM__init(self):
com = SM_MAP[self._type]["com"]
Expand Down
31 changes: 25 additions & 6 deletions custom_components/SMioplus/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

from homeassistant.components.sensor import SensorEntity
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.event import async_track_time_interval
from datetime import timedelta

from . import (
DOMAIN, CONF_STACK, CONF_TYPE, CONF_CHAN, CONF_NAME,
CONF_UPDATE_INTERVAL,
SM_MAP, SM_API
)
SM_MAP = SM_MAP["sensor"]
Expand All @@ -25,29 +28,31 @@ async def async_setup_platform(hass, config, add_devices, discovery_info=None):
if discovery_info == None:
return
add_devices([Sensor(
name=discovery_info.get(CONF_NAME, ""),
stack=discovery_info.get(CONF_STACK, 0),
hass=hass,
name=discovery_info.get(CONF_NAME),
stack=discovery_info.get(CONF_STACK),
type=discovery_info.get(CONF_TYPE),
chan=discovery_info.get(CONF_CHAN),
hass=hass
update_interval=discovery_info.get(CONF_UPDATE_INTERVAL) or 30,
)])

class Sensor(SensorEntity):
def __init__(self, name, stack, type, chan, hass):
def __init__(self, hass, name, stack, type, chan, update_interval):
generated_name = DOMAIN + str(stack) + "_" + type + "_" + str(chan)
self._unique_id = generate_entity_id("sensor.{}", generated_name, hass=hass)
self._name = name or generated_name
self._stack = int(stack)
self._type = type
self._chan = int(chan)
self._update_interval = float(update_interval)
self._short_timeout = .05
self._icons = DEFAULT_ICONS | SM_MAP[self._type].get("icon", {})
self._icon = self._icons["off"]
self._uom = SM_MAP[self._type].get("uom", "")
self._value = 0
self._remove_hooks = []
self.__SM__init()
### CUSTOM_SETUP START
### CUSTOM_SETUP END
### CUSTOM_SETUP START

def __SM__init(self):
com = SM_MAP[self._type]["com"]
Expand All @@ -71,6 +76,20 @@ def _aux_SM_get(*args):
return _SM_get(self._stack, *args)
self._SM_get = _aux_SM_get

async def async_added_to_hass(self):
new_hook = async_track_time_interval(
self.hass, self.async_update_ha_state, timedelta(seconds=self._update_interval)
)
self._remove_hooks.append(new_hook)

async def async_will_remove_from_hass(self):
for remove_hook in self._remove_hooks:
remove_hook()

@property
def should_poll(self):
return False

def update(self):
time.sleep(self._short_timeout)
try:
Expand Down
7 changes: 1 addition & 6 deletions custom_components/SMioplus/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ def __init__(self, name, stack, type, chan, hass):
self._icon = self._icons["off"]
self.__SM__init()
self._is_on = self._SM_get(self._chan)
### CUSTOM_SETUP START
if self._type == "opto_cnt":
#self._SM.rstOptoCount(self._stack, self._chan)
self._SM.cfgOptoEdgeCount(self._stack, self._chan, 1)
### CUSTOM_SETUP END
### CUSTOM_SETUP START

def __SM__init(self):
com = SM_MAP[self._type]["com"]
Expand Down Expand Up @@ -92,7 +88,6 @@ def _aux_SM_set(*args):

def update(self):
time.sleep(self._short_timeout)
self._SM.cfgOptoEdgeCount(self._stack, self._chan, 1)
try:
self._is_on = self._SM_get(self._chan)
except Exception as ex:
Expand Down

0 comments on commit a229c8b

Please sign in to comment.