Skip to content

Commit 62d0bd8

Browse files
Merge pull request #77 from PortableProgrammer/dev
Regressions and bug fixes
2 parents f110205 + 767ee94 commit 62d0bd8

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

status-light/sources/collaboration/slack.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _parse_custom_status(self, client: WebClient, default: enum.Status = enum.S
8888
return enum.Status.UNKNOWN
8989

9090
# Join the emoji and text with a space
91-
custom_status = (user_info['profile']['status_emoji'] + ' '
91+
custom_status: str = (user_info['profile']['status_emoji'] + ' '
9292
+ user_info['profile']['status_text']).casefold()
9393

9494
# For each of the Slack custom statuses, check them in reverse precedence order
@@ -98,17 +98,14 @@ def _parse_custom_status(self, client: WebClient, default: enum.Status = enum.S
9898

9999
if self.custom_available_status and \
100100
custom_status.startswith(tuple(self.custom_available_status)):
101-
102101
return_value = self.custom_available_status_map
103102

104103
if self.custom_scheduled_status and \
105104
custom_status.startswith(tuple(self.custom_scheduled_status)):
106-
107105
return_value = self.custom_scheduled_status_map
108106

109107
if self.custom_busy_status and \
110108
custom_status.startswith(tuple(self.custom_busy_status)):
111-
112109
return_value = self.custom_busy_status_map
113110

114111
# Check for Huddle and Call

status-light/status-light.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ def receive_signal(signal_number, frame):
143143
# light.getCurrentStatus()
144144

145145
outside_hours = False
146+
# Init to True so we don't panic the first time
147+
lastTransitionResult = True
146148
while shouldContinue:
147149
try:
148150
# Decide if we need to poll at this time
@@ -219,21 +221,39 @@ def receive_signal(signal_number, frame):
219221
googleStatus.name.lower())
220222
currentStatus = googleStatus
221223

224+
statusChanged = False
222225
if lastStatus != currentStatus:
223226
lastStatus = currentStatus
227+
statusChanged = True
224228

229+
if statusChanged:
225230
print(datetime.now().strftime('[%Y-%m-%d %H:%M:%S]'),
226231
'Found new status:', currentStatus.name.lower(), flush=True)
232+
233+
if not lastTransitionResult:
234+
print(datetime.now().strftime('[%Y-%m-%d %H:%M:%S]'),
235+
'Last attempt to set status failed. Retrying.', flush=True)
236+
237+
# If status changed this loop
238+
# 40: or the last transition failed,
239+
if statusChanged or not lastTransitionResult:
227240
# 74: Log enums as names, not values
228241
logger.info('Transitioning to %s', currentStatus.name.lower())
229-
light.transition_status(currentStatus, localEnv)
242+
lastTransitionResult = light.transition_status(currentStatus, localEnv)
230243

231244
else:
232-
if outside_hours:
233-
logger.debug('Already transitioned to off, doing nothing')
234-
else:
245+
if not outside_hours:
246+
print(datetime.now().strftime('[%Y-%m-%d %H:%M:%S]'),
247+
'Outside of active hours, transitioning to off', flush=True)
235248
logger.info('Outside of active hours, transitioning to off')
236-
light.turn_off()
249+
lastTransitionResult = light.turn_off()
250+
251+
# 40: If the last transition failed, try again
252+
if not lastTransitionResult:
253+
print(datetime.now().strftime('[%Y-%m-%d %H:%M:%S]'),
254+
'Last attempt to turn off the light failed. Retrying.', flush=True)
255+
lastTransitionResult = light.turn_off()
256+
237257
outside_hours = True
238258

239259
# Sleep for a few seconds

status-light/targets/tuya.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _set_status(self, dps: dict, retry: int = 5):
6666
logger.warning('Exception sending to Tuya device: %s', ex)
6767
count = count + 1
6868
time.sleep(1)
69-
69+
7070
# Still some strangeness; reusing the built-in connection in the "tuyaface" key
7171
# causes a broken pipe error.
7272
# Remove the "tuyaface" key to force a new connection
@@ -76,11 +76,12 @@ def _set_status(self, dps: dict, retry: int = 5):
7676
def get_status(self):
7777
return tuyaface.status(self.device)
7878

79-
def transition_status(self, status: enum.Status, environment: env.Environment):
79+
def transition_status(self, status: enum.Status, environment: env.Environment) -> bool:
8080
# 43: Coalesce the statuses and only execute setState once.
8181
# This will still allow a single status to be in more than one list,
8282
# but will not cause the light to rapidly switch between states.
8383
color = None
84+
return_value = False
8485

8586
if status in environment.available_status:
8687
color = environment.available_color
@@ -92,14 +93,16 @@ def transition_status(self, status: enum.Status, environment: env.Environment):
9293
color = environment.busy_color
9394

9495
if color is not None:
95-
self.set_status('colour', color, environment.tuya_brightness)
96+
return_value = self.set_status('colour', color, environment.tuya_brightness)
9697
# OffStatus has the lowest priority, so only check it if none of the others are valid
9798
elif status in environment.off_status:
98-
self.turn_off()
99+
return_value = self.turn_off()
99100
# In the case that we made it here without a valid state,
100101
# just turn the light off and warn about it
101102
# 74: Log enums as names, not values
102103
else:
103104
logger.warning('Called with an invalid status: %s',
104105
status.name.lower())
105-
self.turn_off()
106+
return_value = self.turn_off()
107+
108+
return return_value

status-light/utility/util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,24 @@ def parse_enum_list(value_string: str, value_enum: EnumType, description: str, d
130130
# 66 - Support Slack custom statuses
131131

132132

133-
def parse_str_array(value_string: str, default: list[str], delimiter: str = ',', casefold: bool = False) -> list[str]:
133+
def parse_str_array(value_string: str | list[str], default: list[str], delimiter: str = ',', casefold: bool = False) -> list[str]:
134134
"""Given a string containing an array of strings, attempts to parse the string into an array.
135135
Pass casefold=True to build an array ready for case-insensitive comparison."""
136136
temp_value = default
137137
if value_string in [None, '']:
138-
return temp_value
138+
value_string = temp_value
139139

140140
try:
141141
# Ensure that we return a true list, since the incoming string
142142
# might have a single element only.
143-
temp_value = []
144143
if not isinstance(value_string, list):
144+
temp_value = []
145145
for value in value_string.split(delimiter):
146146
if casefold:
147147
value = value.casefold()
148148
temp_value.append(value)
149149
else:
150+
temp_value = []
150151
if casefold:
151152
for value in value_string:
152153
temp_value.append(value.casefold())

0 commit comments

Comments
 (0)