jdeath / Hubspace-Homeassistant

Hubspace Integration for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error Updating Hubspace Light

ahahamyan opened this issue · comments

Hi, recently added 4 Hubspace model: 11PR38120RGBWH1 lights to HA. Running 2023.9.1 and receiving the following error in logs. The devices were automatically discovered fine, but I also noticed that integration reports that their state is "on" while the devices are actually offline. It should be reporting the state correctly, but does not appear so, besides the following error in the logs.

Thanks,

This error originated from a custom integration.

Logger: homeassistant.helpers.entity
Source: custom_components/hubspace/hubspace.py:428
Integration: hubspace (documentation, issues)
First occurred: 12:46:49 PM (1 occurrences)
Last logged: 12:46:49 PM

Update for light.outdoor2 fails
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/requests/models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/simplejson/init.py", line 514, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/simplejson/decoder.py", line 386, in decode
obj, end = self.raw_decode(s)
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/simplejson/decoder.py", line 416, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 683, in async_update_ha_state
await self.async_device_update()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 938, in async_device_update
await hass.async_add_executor_job(self.update)
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/hubspace/light.py", line 595, in update
self._debugInfo = self._hs.getDebugInfo(self._childId)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/hubspace/hubspace.py", line 428, in getDebugInfo
_LOGGER.debug(json.dumps(r.json(), indent=4, sort_keys=True))
^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/requests/models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

commented

That is weird. It does take a while to update the state, but surprised it says on when really unavailable. Could you run the TestHubspace.py script and post the output? Looks like it could use a check to see if the data is valid. I can take a look. Perhaps turn off debugging by making debug: false in the configuration and see if errors still occur.

The lights are offline now, but HA reports that state is on, see below. Also ran TestHubspace.py - the results are in the text file. Also, I turned the debug off in configuration file and the error log entry with stacktrace went away, but still the wrong state is reported as before.

Thanks for looking into this.

image
TestHubspace2.txt
IMG_8041

commented

No one ever asked for that before because usually they are quite stable. I think I figured out in the API where it says if the device is available. The API will return the last known state if that field is not explicitly checked. You can try downloading the latest hubspace.py in the repo. I added a check for the available flag. Not sure if it will work or if it will break something. Let me know.

Thanks. I re-downloaded the integration and restarted HA (I checked to make sure timestamps were updated on the downloaded code in HA to make sure the updates took), but unfortunately the state of the offline lights still reports "on" as before. The 4 lights I have are used for outdoor/highlighting the home, so they are offline (powered off) other than sunset to sunrise. I have automation via HA for the outlets the lights are plugged in to that turns the outlets on at sunset and the HubSpace based lights establish connection then and light up. That is why they are offline during day.

commented

Did you manually download the hubspace.py from the repo and replace it? Simply redownloading with HACS or downloading the latest release zip file will not work.

I updated the code manually as you mentioned (for both hubspace.py and light.py since they were both newer), placed them into the /root/config/custom_components/hubspace and restarted HA. But it made no difference. There is nothing in the logs. I see the scan interval by default is 1 minute, so should be updating I would think.

I believe I found what is causing the state not to be set correctly. I ran Testhubspace.py twice when the devices were off and on. When I compared the two outputs, I noticed that on the device objects, power attribute is always set to "on" whether the device is offline or not. But the "available" attribute is set to "true" when the device is online and "false" when the device is offline. I believe if you just check the "available" object when you set the state, it should work fine. I confirmed that the available attribute is updated on the Hubspace cloud after devices disconnect/turned off, similarly as it does on their mobile app - it takes about a couple of minutes at most.

I know you had mentioned about the available attribute previously as well.

I then turned off the lights using the app (devices still connected/online) and ran TestHubspace.py to see what was different, and I noticed that the power attribute was set to off. It appears the power attribute indicates if the light itself is on or not (or last known position when last online), whereas the "available" attribute is for indicating whether the device is connected to the cloud/online or not.

So, the state of the light would be "on" if it is connected and turned on. Its state would be off if it is disconnected or turned off while connected. Of course, through HA, if the device is offline (available set to false) then operations on the device should be disallowed.

I am attaching all outputs from Testhubspace.py - when lights are on/connected and when lights are off/disconnected and when the lights are connected but turned off.

Thanks

TestHubspace_on.txt

TestHubspace_off.txt

TestHubspace_on_lights_off.txt

{
"functionClass": "available",
"lastUpdateTime": 1694828042545,
"value": false
},

{
"functionClass": "available",
"lastUpdateTime": 1694814805751,
"value": true
},

commented

Yes, that is exactly what the latest update to hubspace.py did. However, I think there is a difference in json between False and "false" which I have not figured out yet. I will try to take one my devices down. When I detect that, I set the state to None. But not sure if that makes it show as unavailable, or just causes an error.

In the meantime, take a look at:
Look at:

if key == "functionClass" and val == "available" and lis.get("value") == "false":
and
if key == "functionClass" and val == "available" and lis.get("value") == "false":

See if changing to False (capitol F and no quotes) instead of "false" works....

I added some debug statements into the getState function in hubspace.py, and it does not seem to iterate all through the values returned when it tries to get power state, it just goes through the first one (brightness) and never to the other ones.

r = requests.get(auth_url, data=auth_data, headers=auth_header)
r.close()
for lis in r.json().get("values"):
for key, val in lis.items():
if key == "functionClass" and val == desiredStateName:
state = lis.get("value")
if key == "functionClass" and val == "available" and not lis.get("value"):
return None
#return None

    # print(desiredStateName + ": " + state)
    return state

"values": [
{
"functionClass": "brightness",
"lastUpdateTime": 1694811922524,
"value": 100
},
{
"functionClass": "color-sequence",
"functionInstance": "custom",
"lastUpdateTime": 1694819227156,
"value": "rainbow"
},
{
"functionClass": "restore-values",
"lastUpdateTime": 1694819147499,
"value": "partial-restore"
},
{
"functionClass": "power",
"lastUpdateTime": 1694832018585,
"value": "off"
},
{
"functionClass": "color-temperature",
"lastUpdateTime": 1694812603264,
"value": 3000
},
{
"functionClass": "color-mode",
"lastUpdateTime": 1694813568852,
"value": "white"
},
{
"functionClass": "speed",
"functionInstance": "color-sequence",
"lastUpdateTime": 1694817368624,
"value": 0
},
{
"functionClass": "color-sequence",
"functionInstance": "preset",
"lastUpdateTime": 1694816519536,
"value": "custom"
},
{
"functionClass": "color-rgb",
"lastUpdateTime": 1694812322005,
"value": {
"color-rgb": {
"b": 0,
"g": 0,
"r": 255
}
}
},
{
"functionClass": "wifi-ssid",
"lastUpdateTime": 1694828281554,
"value": "SSID0"
},
{
"functionClass": "wifi-rssi",
"lastUpdateTime": 1694828573668,
"value": -56
},
{
"functionClass": "wifi-steady-state",
"lastUpdateTime": 1694804169514,
"value": "connected"
},
{
"functionClass": "wifi-setup-state",
"lastUpdateTime": 1694804941018,
"value": "connected"
},
{
"functionClass": "wifi-mac-address",
"lastUpdateTime": 0,
"value": "eef35f7da255"
},
{
"functionClass": "geo-coordinates",
"functionInstance": "system-device-location",
"lastUpdateTime": 1694828887393
},
{
"functionClass": "scheduler-flags",
"lastUpdateTime": 1694824437520,
"value": 0
},
{
"functionClass": "available",
"lastUpdateTime": 1694809263630,
"value": true
},
{
"functionClass": "visible",
"lastUpdateTime": 1694809263630,
"value": true
},
{
"functionClass": "direct",
"lastUpdateTime": 1694809263630,
"value": true
},
{
"functionClass": "ble-mac-address",
"lastUpdateTime": 0,
"value": " 41e41187119"
}
]
},

commented

Ok. I got it (I think). Manually download light.py and hubspace.py . The light should show as unknown if the state is unavailable. It took changes in a few places. Let me know.

Why don't you just turn off the lights with an automation, versus hard power offs?

Thanks. it is working much better. But I did get the following error in the logs. When I updated the code and restarted HA when lights were offline, it left them in off state which was correct. Then when I turned the lights on/connected and detected them. When I turned the light off while connected, it also detected. When I turned the light back on while connected, it also detected. However, when i finally turned off/disconnected the lights, it did not detect, it left them in the "on" state. So, I think it is very close.

Since they are outdoor lights that are on from sunset to sunrise, there is no need to have the lights be powered and connected to the Internet all day long. Also, prior to getting the Smart Lights, I had the dumb-non-smart lights that would just come on via outlet powered on, so I followed the same approach with these.

Thanks again for your help.

This error originated from a custom integration.

Logger: homeassistant.helpers.entity
Source: custom_components/hubspace/hubspace.py:580
Integration: hubspace (documentation, issues)
First occurred: 4:55:37 PM (24 occurrences)
Last logged: 5:07:38 PM

Update for light.outdoor4 fails
Update for light.outdoor1 fails
Update for light.outdoor2 fails
Update for light.outdoor3 fails
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 683, in async_update_ha_state
await self.async_device_update()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 938, in async_device_update
await hass.async_add_executor_job(self.update)
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/hubspace/light.py", line 613, in update
self._rgbColor = self._hs.getRGB(self._childId)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/hubspace/hubspace.py", line 580, in getRGB
r = int(state.get("color-rgb").get("r"))
^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get'

commented

Remember, it will take time to update to return the correct state. But I think it should get it eventually. There could be time before the API reports it is unavailable. Looks like I need more error checking now that the state returns none. It will report none as soon as the API says it is.

Try latest hubspace.py . We will just have to squash these bugs as they show up. I will not do a release until you have a few days of it working ok.

Thanks much. Updated with the latest Hubspace.py and things are working very well. When the devices are not connected the light state is "unknown" versus on/off when connected. Switching from white light to color between HA and Hubspace App seems to work well with things getting updated well on HA side and Hubspace side correctly. The only thing that seemed to not work is to use HA integration to transition back from color to white light setting - but when I did same through the hubspace app, HA was able to detect correct mode at the next polling interval. So, far that is the only anomaly I noticed, besides the error log entry below.

When the devices are offline the graphic looks like below which is nice - the lightning bolt with the strikethrough is good to indicate it is offline/unknown, the lightning bolt to the right (without strikethrough) really does nothing as there is nothing the integration do to turn on/connect the light to cloud. Its fine, but just an observation. Truly, when the device state is unknown, there cannot be any operations carried out on the device.

image

instead of the usual slider. I love the fact that if color mode is turned on, the state color is the color it is set to, otherwise it is yellow if selected light mode is white light.

I did see the following error in the log

This error originated from a custom integration.

Logger: homeassistant.helpers.entity
Source: custom_components/hubspace/hubspace.py:369
Integration: hubspace (documentation, issues)
First occurred: 3:32:52 PM (1 occurrences)
Last logged: 3:32:52 PM

Update for light.outdoor4 fails
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/requests/models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/simplejson/init.py", line 514, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/simplejson/decoder.py", line 386, in decode
obj, end = self.raw_decode(s)
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/simplejson/decoder.py", line 416, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 683, in async_update_ha_state
await self.async_device_update()
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 938, in async_device_update
await hass.async_add_executor_job(self.update)
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/hubspace/light.py", line 613, in update
self._rgbColor = self._hs.getRGB(self._childId)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/hubspace/hubspace.py", line 579, in getRGB
state = self.getState(child, "color-rgb")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/hubspace/hubspace.py", line 369, in getState
for lis in r.json().get("values"):
^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/requests/models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Hello, I noticed the following messages in the logs today, happening at every update interval and not updating the state of the lights in HA.

Logger: homeassistant.components.light
Source: helpers/entity_platform.py:857
Integration: Light (documentation, issues)
First occurred: 8:07:34 PM (1 occurrences)
Last logged: 8:07:34 PM

Updating hubspace light took longer than the scheduled update interval 0:01:00

This morning I noticed the same issue still persists. Did something change on the Hubspace API?

Thanks

Logger: homeassistant.components.light
Source: helpers/entity_platform.py:857
Integration: Light (documentation, issues)
First occurred: October 24, 2023 at 8:07:34 PM (741 occurrences)
Last logged: 8:27:41 AM

Updating hubspace light took longer than the scheduled update interval 0:01:00

UPDATE - After a restart due to HA OS update and HA Supervisor update - it seems to be working ok for now.

Closing this issue due to inactivity. If the issue persists, please re-open the issue