Please implement Exceptions for better Error handling
Hello,
it would be nice to have some exceptions to be thrown:
- Discovery failed while using
Bulb()
so throwBulbConnectionException
- Socket that don't respond should throw
BulbTimeoutException
- LAN control is not enabled so throw
BulbLanControlException
Hacky solution that includes bulb.turn_on()
to test for enabled lan control:
def bulb_factory(ip):
bulb = None
# handle import
try:
from yeelight import Bulb, BulbException
except ImportError:
exit(wording.get('package_no').format('YEELIGHT') + wording.get('exclamation_mark'))
# create instance
try:
bulb = Bulb(ip)
except BulbException:
exit(wording.get('connection_no').format('XIAOMI YEELIGHT') + wording.get('exclamation_mark'))
try:
bulb.turn_on()
except BulbException:
exit(wording.get('enable_feature').format('LAN CONTROL', 'XIAOMI YEELIGHT') + wording.get('exclamation_mark'))
return bulb
Wanted solution that throws BulbConnectionException
, BulbTimeoutException
and BulbLanControlException
:
def bulb_factory(ip):
bulb = None
# handle import
try:
from yeelight import Bulb, BulbConnectionException, BulbTimeoutException, BulbLanControlException
except ImportError:
exit(wording.get('package_no').format('YEELIGHT') + wording.get('exclamation_mark'))
# create instance
try:
bulb = Bulb(ip)
except (BulbConnectionException, BulbTimeoutException):
exit(wording.get('connection_no').format('XIAOMI YEELIGHT') + wording.get('exclamation_mark'))
except BulbLanControlException:
exit(wording.get('enable_feature').format('LAN CONTROL', 'XIAOMI YEELIGHT') + wording.get('exclamation_mark'))
return bulb
return bulb
Edited by Henry Ruhs