luminance icon indicating copy to clipboard operation
luminance copied to clipboard

KeyError when connecting to bridge

Open chris-downs opened this issue 8 years ago • 9 comments

I'm getting the following on startup after I connect to the bridge:

(luminance:5441): Gtk-WARNING **: Theme parsing error: gtk.css:68:35: The style property GtkButton:child-displacement-x is deprecated and shouldn't be used anymore. It will be removed in a future version

(luminance:5441): Gtk-WARNING **: Theme parsing error: gtk.css:69:35: The style property GtkButton:child-displacement-y is deprecated and shouldn't be used anymore. It will be removed in a future version

(luminance:5441): Gtk-WARNING **: Theme parsing error: gtk.css:73:46: The style property GtkScrolledWindow:scrollbars-within-bevel is deprecated and shouldn't be used anymore. It will be removed in a future version
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/phue.py", line 855, in get_light
    return state['state'][parameter]
KeyError: 'hue'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/luminance/application.py", line 133, in _setup_finished
    self._init()
  File "/usr/lib/python3.5/site-packages/luminance/application.py", line 114, in _init
    application=self
  File "/usr/lib/python3.5/site-packages/luminance/views/window.py", line 39, in __init__
    self.lights_page.add(FramedEntityList(self.bridge.get_light_objects('id').values()))
  File "/usr/lib/python3.5/site-packages/luminance/views/entity.py", line 27, in __init__
    self.list.add(ListBoxRow(entity))
  File "/usr/lib/python3.5/site-packages/luminance/views/entity.py", line 62, in __init__
    self.model.hue,
  File "/usr/local/lib/python3.5/dist-packages/phue.py", line 196, in hue
    self._hue = self._get('hue')
  File "/usr/local/lib/python3.5/dist-packages/phue.py", line 104, in _get
    return self.bridge.get_light(self.light_id, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/phue.py", line 859, in get_light
    % (parameter, light_id))
KeyError: 'Not a valid key, parameter hue is not associated with light 8)'

Running on:

Linux rougebox 4.8.0-39-generic #42-Ubuntu SMP Mon Feb 20 11:47:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

chris-downs avatar Feb 25 '17 23:02 chris-downs

It looks like this occurs when a White-only light is encountered, as they don't have a 'hue' parameter.

Here is the state object for the white-only light that it's bombing out on:

{'uniqueid': '00:17:88:01:02:30:1c:2c-0b', 'state': {'alert': 'none', 'on': True, 'reachable': True, 'bri': 254}, 'type': 'Dimmable light', 'modelid': 'LWB006', 'manufacturername': 'Philips', 'swversion': '5.38.2.19136', 'name': 'Bedroom Nightstand'}

It doesn't have a key named "hue"

chris-downs avatar Feb 25 '17 23:02 chris-downs

I was able to work around it by forcing the color chooser to white if it encounters a light that throws a KeyError when the hue or saturation params are missing:

entity.py:59

        if self.model.on:
            try:
                self.color_chooser.set_rgba(
                    hsv_to_gdk_rgb(
                        self.model.hue,
                        self.model.saturation,
                        self.model.brightness
                    )
                )
            except KeyError:
                self.color_chooser.set_rgba(
                    hsv_to_gdk_rgb(
                        0,
                        0,
                        self.model.brightness
                    )
                )

I can submit a PR but it's kind of a hacky approach and you might want to fix it a different way\add support for non-color lights. I'll leave it up to you!

chris-downs avatar Feb 26 '17 00:02 chris-downs

Same problem with Hue White Ambiance bulbs as well.

BHSPitMonkey avatar May 19 '17 09:05 BHSPitMonkey

Also came across this and only have Hue White Ambiance bulbs on my network.

jay-to-the-dee avatar Dec 11 '17 21:12 jay-to-the-dee

Have tried your patch @chris-downs :) it helps and gets the GUI running at least for it bit, but it ends up crashing after a few changes of settings.

jay-to-the-dee avatar Dec 11 '17 21:12 jay-to-the-dee

Minimal workaround for also setup with smart plugs that are supported by hue bridge and Philips' app is:

diff --git a/luminance/views/entity.py b/luminance/views/entity.py
index e65dda9..06956a0 100644
--- a/luminance/views/entity.py
+++ b/luminance/views/entity.py
@@ -57,16 +57,37 @@ class ListBoxRow(Gtk.ListBoxRow):
         self.color_chooser = builder.get_object('color-chooser')
 
         if self.model.on:
-            self.color_chooser.set_rgba(
-                hsv_to_gdk_rgb(
-                    self.model.hue,
-                    self.model.saturation,
-                    self.model.brightness
+            try:
+                self.color_chooser.set_rgba(
+                    hsv_to_gdk_rgb(
+                        self.model.hue,
+                        self.model.saturation,
+                        self.model.brightness
+                    )
                 )
-            )
+            except KeyError:
+                try:
+                    self.color_chooser.set_rgba(
+                        hsv_to_gdk_rgb(
+                            0,
+                            0,
+                            self.model.brightness
+                        )
+                    )
+                except KeyError:
+                    self.color_chooser.set_rgba(
+                        hsv_to_gdk_rgb(
+                            0,
+                            0,
+                            0
+                        )
+                    )
 
         self.brightness_scale = builder.get_object('brightness-scale')
-        self.brightness_scale.set_value(self.model.brightness)
+        try:
+            self.brightness_scale.set_value(self.model.brightness)
+        except KeyError:
+            self.brightness_scale.set_value(0)
 
         self.color_chooser_popover_button = builder.get_object('color-chooser-popover-button')

PanderMusubi avatar Dec 31 '18 16:12 PanderMusubi

Note that phue has a new release, please see https://pypi.org/project/phue/#history

PanderMusubi avatar Jan 28 '19 09:01 PanderMusubi

I think I have color lights (am able to change color from iPhone) and I get this crash as well, so perhaps not specifically related to only non-color ones?

jacobseated avatar Jun 13 '19 19:06 jacobseated

Similar error occurs to me when connecting to the bridge:

` [stefan@stefan-pc luminance]$ luminance /usr/bin/phue.py:694: SyntaxWarning: "is not" with a literal. Did you mean "!="? if ip is not '': Traceback (most recent call last): File "/usr/bin/phue.py", line 843, in get_light return state['state'][parameter] KeyError: 'bri'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/luminance/application.py", line 133, in _setup_finished self._init() File "/usr/lib/python3.8/site-packages/luminance/application.py", line 112, in _init self.window = Window( File "/usr/lib/python3.8/site-packages/luminance/views/window.py", line 39, in init self.lights_page.add(FramedEntityList(self.bridge.get_light_objects('id').values())) File "/usr/lib/python3.8/site-packages/luminance/views/entity.py", line 27, in init self.list.add(ListBoxRow(entity)) File "/usr/lib/python3.8/site-packages/luminance/views/entity.py", line 69, in init self.brightness_scale.set_value(self.model.brightness) File "/usr/bin/phue.py", line 194, in brightness self._brightness = self._get('bri') File "/usr/bin/phue.py", line 117, in _get return self.bridge.get_light(self.light_id, *args, **kwargs) File "/usr/bin/phue.py", line 845, in get_light raise KeyError( KeyError: 'Not a valid key, parameter bri is not associated with light 7)

`

Laess3r avatar Feb 16 '20 10:02 Laess3r