natekspencer / hacs-pura

Home Assistant integration for Pura smart fragrance diffusers
MIT License
18 stars 1 forks source link

Fix start timer service when only one vial installed & add additional check if specific slot is selected #25

Closed RobertD502 closed 11 months ago

RobertD502 commented 11 months ago

At the moment, if no slot is specified, the start_timer service will automatically select slot 2 if a user only has one vial installed (in particular in slot 1). A Pura device that only has a vial in slot 1 will return a bay2 wearingTime of 0. As a result, if the wearingTime for bay1 is greater than 0, slot 2 will be the automatically selected slot instead of slot 1 even though no fragrance vial is installed in bay2.

Example data returned by Pura API when only bay1 slot has a fragrance vial:

'bay2': {'msg': '', 'wearingTime': 0, 'code': '', 'min': 64, 'max': 85, 'vialId': '', 'id': 0, 'activeAt': 0, 'isSmartVial': False}, 'connected': True, 'bay1': {'msg': '', 'wearingTime': 344869, 'code': 'SVA', 'min': 64, 'max': 85, 'vialId': 'e66a6e37-7259-4c7f-94b8-17455703e008', 'id': 1694623148, 'activeAt': 0, 'isSmartVial': False}

In order to get around this, if a user hasn't selected a slot: 1) We first check to see what slots are occupied and create a installed_slots list. 2) If determined that both bay1 and bay2 are occupied, then we compare wearingTime 3) If only bay1 or bay2 has a vial installed, then whichever one has a vial is determined to be the automatically selected slot. 4) In addition, a check is performed, and exception raised in the event that the user is attempting to call the start_timer on the device that has no vials installed.

If a user selects a specific slot: An exception is raised indicating that the selected slot doesn't have a vial installed if the slot is not found in the installed_slots list.

RobertD502 commented 11 months ago

Fixes #22.