Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion plugwise_usb/nodes/helpers/pulses.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,18 +905,22 @@ def _last_known_duration(self) -> timedelta:
"""Duration for last known logs."""
if self._logs is None:
raise EnergyError("Unable to return last known duration without any logs")

if len(self._logs) < 2:
return timedelta(hours=1)

address, slot = self._last_log_reference()
if address is None or slot is None:
raise EnergyError("Unable to return last known duration without any logs")

last_known_timestamp = self._logs[address][slot].timestamp
address, slot = calc_log_address(address, slot, -1)
while (
self._log_exists(address, slot)
or self._logs[address][slot].timestamp == last_known_timestamp
and self._logs[address][slot].timestamp == last_known_timestamp
):
address, slot = calc_log_address(address, slot, -1)

return self._logs[address][slot].timestamp - last_known_timestamp

def _missing_addresses_before(
Expand Down
58 changes: 28 additions & 30 deletions plugwise_usb/nodes/sed.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,10 @@ async def set_awake_duration(self, seconds: int) -> bool:
raise ValueError(
f"Invalid awake duration ({seconds}). It must be between 1 and 255 seconds."
)

if self._battery_config.awake_duration == seconds:
self._new_battery_config = replace(
self._new_battery_config, awake_duration=seconds
)
return False
return False

self._new_battery_config = replace(
self._new_battery_config, awake_duration=seconds
)
Expand All @@ -251,6 +250,7 @@ async def set_awake_duration(self, seconds: int) -> bool:
"set_awake_duration | Device %s | config scheduled",
self.name,
)

return True

@raise_not_loaded
Expand All @@ -266,11 +266,10 @@ async def set_clock_interval(self, minutes: int) -> bool:
raise ValueError(
f"Invalid clock interval ({minutes}). It must be between 1 and 65535 minutes."
)

if self.battery_config.clock_interval == minutes:
self._new_battery_config = replace(
self._new_battery_config, clock_interval=minutes
)
return False

self._new_battery_config = replace(
self._new_battery_config, clock_interval=minutes
)
Expand All @@ -281,6 +280,7 @@ async def set_clock_interval(self, minutes: int) -> bool:
"set_clock_interval | Device %s | config scheduled",
self.name,
)

return True

@raise_not_loaded
Expand All @@ -293,10 +293,8 @@ async def set_clock_sync(self, sync: bool) -> bool:
sync,
)
if self._battery_config.clock_sync == sync:
self._new_battery_config = replace(
self._new_battery_config, clock_sync=sync
)
return False

self._new_battery_config = replace(self._new_battery_config, clock_sync=sync)
if not self._sed_config_task_scheduled:
await self.schedule_task_when_awake(self._configure_sed_task())
Expand All @@ -305,6 +303,7 @@ async def set_clock_sync(self, sync: bool) -> bool:
"set_clock_sync | Device %s | config scheduled",
self.name,
)

return True

@raise_not_loaded
Expand All @@ -320,11 +319,10 @@ async def set_maintenance_interval(self, minutes: int) -> bool:
raise ValueError(
f"Invalid maintenance interval ({minutes}). It must be between 1 and 1440 minutes."
)

if self.battery_config.maintenance_interval == minutes:
self._new_battery_config = replace(
self._new_battery_config, maintenance_interval=minutes
)
return False

self._new_battery_config = replace(
self._new_battery_config, maintenance_interval=minutes
)
Expand All @@ -335,6 +333,7 @@ async def set_maintenance_interval(self, minutes: int) -> bool:
"set_maintenance_interval | Device %s | config scheduled",
self.name,
)

return True

@raise_not_loaded
Expand All @@ -353,11 +352,10 @@ async def set_sleep_duration(self, minutes: int) -> bool:
raise ValueError(
f"Invalid sleep duration ({minutes}). It must be between 1 and 65535 minutes."
)

if self._battery_config.sleep_duration == minutes:
self._new_battery_config = replace(
self._new_battery_config, sleep_duration=minutes
)
return False

self._new_battery_config = replace(
self._new_battery_config, sleep_duration=minutes
)
Expand All @@ -368,6 +366,7 @@ async def set_sleep_duration(self, minutes: int) -> bool:
"set_sleep_duration | Device %s | config scheduled",
self.name,
)

return True

# endregion
Expand Down Expand Up @@ -639,25 +638,24 @@ async def _send_tasks(self) -> None:
"""Send all tasks in queue."""
if len(self._send_task_queue) == 0:
return
await self._send_task_lock.acquire()
task_result = await gather(*self._send_task_queue)
if not all(task_result):
_LOGGER.warning(
"Executed %s tasks (result=%s) for %s",
len(self._send_task_queue),
task_result,
self.name,
)
self._send_task_queue = []
self._send_task_lock.release()

async with self._send_task_lock:
task_result = await gather(*self._send_task_queue)
if not all(task_result):
_LOGGER.warning(
"Executed %s tasks (result=%s) for %s",
len(self._send_task_queue),
task_result,
self.name,
)
self._send_task_queue = []

async def schedule_task_when_awake(
self, task_fn: Coroutine[Any, Any, bool]
) -> None:
"""Add task to queue to be executed when node is awake."""
await self._send_task_lock.acquire()
self._send_task_queue.append(task_fn)
self._send_task_lock.release()
async with self._send_task_lock:
self._send_task_queue.append(task_fn)

async def sed_configure( # pylint: disable=too-many-arguments
self,
Expand Down
2 changes: 0 additions & 2 deletions plugwise_usb/nodes/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def __init__(
super().__init__(mac, address, controller, loaded_callback)
self._switch_subscription: Callable[[], None] | None = None
self._switch_state: bool | None = None
self._switch: bool = False

async def load(self) -> bool:
"""Load and activate Switch node features."""
Expand Down Expand Up @@ -124,7 +123,6 @@ async def _switch_state_update(

self._set_cache(CACHE_SWITCH_TIMESTAMP, timestamp)
if state_update:
self._switch = switch_state
await gather(
*[
self.publish_feature_update_to_subscribers(
Expand Down
Loading