Skip to content

Commit

Permalink
catch more specific error, when no switchbot id exists
Browse files Browse the repository at this point in the history
  • Loading branch information
mqcmd196 committed Jan 17, 2022
1 parent 8b26767 commit adf6a2a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions switchbot_ros/scripts/switchbot_ros_server.py
Expand Up @@ -8,6 +8,7 @@
from switchbot_ros.msg import SwitchBotCommandFeedback
from switchbot_ros.msg import SwitchBotCommandResult
from switchbot_ros.switchbot import SwitchBotAPIClient
from switchbot_ros.switchbot import DeviceError, SwitchBotAPIError


class SwitchBotAction:
Expand Down Expand Up @@ -40,7 +41,7 @@ def __init__(self):
execute_cb=self.execute_cb, auto_start=False)
self._as.start()


def execute_cb(self, goal):
feedback = SwitchBotCommandFeedback()
result = SwitchBotCommandResult()
Expand All @@ -62,7 +63,7 @@ def execute_cb(self, goal):
command_type=command_type,
device_name=goal.device_name
))
except Exception as e:
except (DeviceError, SwitchBotAPIError, KeyError) as e:
rospy.logerr(str(e))
feedback.status = str(e)
success = False
Expand Down
15 changes: 12 additions & 3 deletions switchbot_ros/src/switchbot_ros/switchbot.py
Expand Up @@ -111,7 +111,10 @@ def device_status(self, device_id=None, device_name=None):
if device_id:
pass
elif device_name:
device_id = self.device_name_id[device_name]
try:
device_id = self.device_name_id[device_name]
except KeyError as e:
raise KeyError("Device name:{} is not registered at switchbot server. Please check the setting.".format(device_name))
else:
raise ValueError("Please set device_id or device_name.")

Expand All @@ -130,7 +133,10 @@ def control_device(self, command, parameter='default', command_type='command', d
if device_id:
pass
elif device_name:
device_id = self.device_name_id[device_name]
try:
device_id = self.device_name_id[device_name]
except KeyError as e:
raise KeyError("Device name:{} is not registered at switchbot server. Please check the setting.".format(device_name))
else:
raise ValueError("Please set device_id or device_name.")

Expand All @@ -144,7 +150,10 @@ def execute_scene(self, scene_id=None, scene_name=None):
if scene_id:
pass
elif scene_name:
scene_id = self.scene_name_id[scene_name]
try:
scene_id = self.scene_name_id[scene_name]
except KeyError as e:
raise KeyError("Scene name:{} is not registered at switchbot server. Please check the setting.".format(device_name))
else:
raise ValueError("Please set scene_id or scene_name.")

Expand Down

0 comments on commit adf6a2a

Please sign in to comment.