Skip to content

Commit a27915f

Browse files
committed
move away from per-device polling, make the status update frequency adjustable down to sub-minute level, experimentally disable polling mode for duofern devices (only updating via callbacks).
1 parent 505fd3e commit a27915f

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

custom_components/duofern/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
118118

119119
stick = DuofernStickThreaded(serial_port=serial_port, system_code=code, config_file_json=configfile,
120120
ephemeral=False)
121+
stick.updating_interval = 5*60
121122

122123
_registerServices(hass, stick, configEntries[0])
123124
_registerUpdateHassFromStickCallback(hass, stick)
@@ -149,7 +150,7 @@ def _registerUpdateHassFromStickCallback(hass: HomeAssistant, stick: DuofernStic
149150
def update_callback(id: str | None, key: Any, value: Any) -> None:
150151
if id is not None:
151152
try:
152-
_LOGGER.info(f"Updatecallback for {id}")
153+
_LOGGER.debug(f"Updatecallback for {id}: {key}: {value}")
153154
device = hass.data[DOMAIN]['devices'][id] # Get device by id
154155
if device.enabled:
155156
try:
@@ -196,7 +197,8 @@ def get_device_id(hass_entity_id):
196197
hass_device_id = call.data.get('device_id', None)
197198
if get_all:
198199
_LOGGER.info("Asking all devices for update")
199-
device_ids = hass.data[DOMAIN]['stick'].duofern_parser.modules['by_code'].keys()
200+
hass.data[DOMAIN]['stick'].status_request()
201+
return
200202
else:
201203
_LOGGER.info("Asking specific devices for update")
202204
device_ids = [get_device_id(i) for i in hass_device_id]
@@ -224,13 +226,13 @@ def get_device_id(hass_entity_id):
224226
def set_update_interval(call: ServiceCall) -> None:
225227
try:
226228
period_minutes = call.data.get('period_minutes', None)
227-
if period_minutes == int(0):
229+
if period_minutes == 0:
228230
period_minutes = None
229231
_LOGGER.warning("set period_minutes to 0 - no updates will be triggered automatically")
230232
except Exception:
231233
_LOGGER.warning("something went wrong while reading period from parameters")
232234
return
233-
getDuofernStick(hass).updating_interval = period_minutes
235+
getDuofernStick(hass).updating_interval = period_minutes*60 if period_minutes is not None else None
234236

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

244246
UPDATE_INTERVAL_SCHEMA = vol.Schema({
245-
vol.Optional('period_minutes', default=5): cv.positive_int,
247+
vol.Optional('period_minutes', default=5): cv.positive_float,
246248
})
247249

248250
hass.services.register(DOMAIN, 'start_pairing', start_pairing, PAIRING_SCHEMA)

custom_components/duofern/cover.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def __init__(self, duofernId: str, name: str, stick: DuofernStickThreaded):
7575
self._stick = stick
7676
self._openclose: Literal["up", "down", "stop"] = 'stop'
7777
self._last_update_time = datetime.datetime.now()
78-
self._stick.updating_interval = 5
7978

8079
@property
8180
def name(self) -> str:
@@ -111,7 +110,7 @@ def is_closed(self) -> bool | None:
111110
@property
112111
def should_poll(self) -> bool:
113112
"""Whether this entity should be polled or uses subscriptions"""
114-
return True # TODO: Add config option for subscriptions over polling
113+
return False # TODO: Add config option for subscriptions over polling
115114

116115
@property
117116
def supported_features(self) -> CoverEntityFeature:
@@ -168,8 +167,4 @@ def update(self) -> None:
168167
self._openclose = self._stick.duofern_parser.modules['by_code'][self._duofernId]['moving']
169168
except KeyError:
170169
self._state = None
171-
if self._stick.updating_interval is not None and \
172-
datetime.datetime.now() - self._last_update_time > datetime.timedelta(minutes=self._stick.updating_interval):
173-
self._stick.command(self._duofernId, 'getStatus')
174-
self._last_update_time = datetime.datetime.now() + datetime.timedelta(seconds=randint(0, 60))
175170
_LOGGER.info(f"{self._duofernId} state is now {self._state}")

custom_components/duofern/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"config_flow": true,
77
"issue_tracker": "https://github.com/gluap/pyduofern-hacs/issues" ,
88
"codeowners": ["@gluap"],
9-
"requirements": ["pyduofern==0.36.1"],
9+
"requirements": ["pyduofern==0.36.2"],
1010
"version": "0.5.16"
1111
}

custom_components/duofern/services.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ set_update_interval:
3939
fields:
4040
period_minutes:
4141
description: approximate forced refresh interval in minutes - defaults to 5, 0 means never
42-
example: 5
42+
example: 5.0
4343
required: true
4444
selector:
4545
number:
46-
min: 0
47-
max: 60
46+
min: 0.0
47+
max: 10.0
48+
step: 0.1

0 commit comments

Comments
 (0)