Skip to content

Commit

Permalink
move away from per-device polling, make the status update frequency a…
Browse files Browse the repository at this point in the history
…djustable down to sub-minute level, experimentally disable polling mode for duofern devices (only updating via callbacks).
  • Loading branch information
gluap committed Jan 9, 2024
1 parent 505fd3e commit a27915f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
12 changes: 7 additions & 5 deletions custom_components/duofern/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:

stick = DuofernStickThreaded(serial_port=serial_port, system_code=code, config_file_json=configfile,
ephemeral=False)
stick.updating_interval = 5*60

_registerServices(hass, stick, configEntries[0])
_registerUpdateHassFromStickCallback(hass, stick)
Expand Down Expand Up @@ -149,7 +150,7 @@ def _registerUpdateHassFromStickCallback(hass: HomeAssistant, stick: DuofernStic
def update_callback(id: str | None, key: Any, value: Any) -> None:
if id is not None:
try:
_LOGGER.info(f"Updatecallback for {id}")
_LOGGER.debug(f"Updatecallback for {id}: {key}: {value}")
device = hass.data[DOMAIN]['devices'][id] # Get device by id
if device.enabled:
try:
Expand Down Expand Up @@ -196,7 +197,8 @@ def get_device_id(hass_entity_id):
hass_device_id = call.data.get('device_id', None)
if get_all:
_LOGGER.info("Asking all devices for update")
device_ids = hass.data[DOMAIN]['stick'].duofern_parser.modules['by_code'].keys()
hass.data[DOMAIN]['stick'].status_request()
return
else:
_LOGGER.info("Asking specific devices for update")
device_ids = [get_device_id(i) for i in hass_device_id]
Expand Down Expand Up @@ -224,13 +226,13 @@ def get_device_id(hass_entity_id):
def set_update_interval(call: ServiceCall) -> None:
try:
period_minutes = call.data.get('period_minutes', None)
if period_minutes == int(0):
if period_minutes == 0:
period_minutes = None
_LOGGER.warning("set period_minutes to 0 - no updates will be triggered automatically")
except Exception:
_LOGGER.warning("something went wrong while reading period from parameters")
return
getDuofernStick(hass).updating_interval = period_minutes
getDuofernStick(hass).updating_interval = period_minutes*60 if period_minutes is not None else None

PAIRING_SCHEMA = vol.Schema({
vol.Optional('timeout', default=30): cv.positive_int,
Expand All @@ -242,7 +244,7 @@ def set_update_interval(call: ServiceCall) -> None:
})

UPDATE_INTERVAL_SCHEMA = vol.Schema({
vol.Optional('period_minutes', default=5): cv.positive_int,
vol.Optional('period_minutes', default=5): cv.positive_float,
})

hass.services.register(DOMAIN, 'start_pairing', start_pairing, PAIRING_SCHEMA)
Expand Down
7 changes: 1 addition & 6 deletions custom_components/duofern/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def __init__(self, duofernId: str, name: str, stick: DuofernStickThreaded):
self._stick = stick
self._openclose: Literal["up", "down", "stop"] = 'stop'
self._last_update_time = datetime.datetime.now()
self._stick.updating_interval = 5

@property
def name(self) -> str:
Expand Down Expand Up @@ -111,7 +110,7 @@ def is_closed(self) -> bool | None:
@property
def should_poll(self) -> bool:
"""Whether this entity should be polled or uses subscriptions"""
return True # TODO: Add config option for subscriptions over polling
return False # TODO: Add config option for subscriptions over polling

@property
def supported_features(self) -> CoverEntityFeature:
Expand Down Expand Up @@ -168,8 +167,4 @@ def update(self) -> None:
self._openclose = self._stick.duofern_parser.modules['by_code'][self._duofernId]['moving']
except KeyError:
self._state = None
if self._stick.updating_interval is not None and \
datetime.datetime.now() - self._last_update_time > datetime.timedelta(minutes=self._stick.updating_interval):
self._stick.command(self._duofernId, 'getStatus')
self._last_update_time = datetime.datetime.now() + datetime.timedelta(seconds=randint(0, 60))
_LOGGER.info(f"{self._duofernId} state is now {self._state}")
2 changes: 1 addition & 1 deletion custom_components/duofern/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"config_flow": true,
"issue_tracker": "https://github.com/gluap/pyduofern-hacs/issues" ,
"codeowners": ["@gluap"],
"requirements": ["pyduofern==0.36.1"],
"requirements": ["pyduofern==0.36.2"],
"version": "0.5.16"
}
7 changes: 4 additions & 3 deletions custom_components/duofern/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ set_update_interval:
fields:
period_minutes:
description: approximate forced refresh interval in minutes - defaults to 5, 0 means never
example: 5
example: 5.0
required: true
selector:
number:
min: 0
max: 60
min: 0.0
max: 10.0
step: 0.1

0 comments on commit a27915f

Please sign in to comment.