studioimaginaire / phue

A Python library for the Philips Hue system
Other
1.53k stars 267 forks source link

Please implement PhueConnectionException and fix PhueRequestTimeout #157

Open henryruhs opened 5 years ago

henryruhs commented 5 years ago

Hello,

passing various invalid strings to the constructor is causing different errors and exceptions:

  1. ip = 1.1.1 ~ OSError: [Errno 101] Network is unreachable
  2. ip = 1.1.1.1 ~ json.decoder.JSONDecodeError: Expecting value
  3. ´ip = invalid` ~ socket.gaierror: [Errno -2] Name or service not known
  4. PhueRequestTimeout does not always catch socket.timeout for some reason
  5. Using an ip that does not exist causes socket.timeout on line 743, in connectand line 652, in request

it would be nice to have the following exceptions to be thrown:

  1. Discovery failed while using Bridge() so throw PhueConnectionException
  2. Socket that don't respond should throw PhueTimeoutException
  3. Keep the PhueRegistrationException

Hacky solution that is currently working properly accept random socket.timeout:

def bridge_factory(ip):
    bridge = None

    # handle import

    try:
        from phue import Bridge, PhueRegistrationException, PhueRequestTimeout
    except ImportError:
        exit(wording.get('package_no').format('PHUE') + wording.get('exclamation_mark'))

    # create instance

    try:
        bridge = Bridge(ip)
    except (PhueRequestTimeout, OSError, ValueError):
        exit(wording.get('connection_no').format('PHILIPS HUE') + wording.get('exclamation_mark'))
    except PhueRegistrationException:
        exit(wording.get('press_button').format('PAIRING', 'PHILIPS HUE BRIDGE') + wording.get('exclamation_mark'))
    return bridge

Wanted solution that throws PhueConnectionException, PhueTimeoutException and PhueRegistrationException:

def bridge_factory(ip):
    bridge = None

    # handle import

    try:
        from phue import Bridge, PhueRegistrationException, PhueTimeoutException
    except ImportError:
        exit(wording.get('package_no').format('PHUE') + wording.get('exclamation_mark'))

    # create instance

    try:
        bridge = Bridge(ip)
    except (PhueConnectionException, PhueTimeoutException):
        exit(wording.get('connection_no').format('PHILIPS HUE') + wording.get('exclamation_mark'))
    except PhueRegistrationException:
        exit(wording.get('press_button').format('PAIRING', 'PHILIPS HUE BRIDGE') + wording.get('exclamation_mark'))
    return bridge