jfarmer08 / ha-sengledapi

Home Assistant Integration for Sengled Bulbs. This is a custom component to allow control of Sengled Bulbs in Homeassistant using the unofficial Sengled API. Please note this mimics the Sengled app and therefore Sengled may cut off access at anytime.
Apache License 2.0
106 stars 35 forks source link

Home Assistant OS 2021.3.4 errors on setting up sengledapi #43

Closed HeofHIShirts closed 3 years ago

HeofHIShirts commented 3 years ago

Describe the bug The integration no longer works with Home Assistant OS

To Reproduce Restart Home Assistant from the console controls menu

Expected behavior Integration can control associated Sengled lights through hub

System configuration System: Home Assistant OS HA Version: 2021.3.4 SengledApi Version: v.0.2

configuration.yaml


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

binary_sensor: !include binary_sensor.yaml
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
intent: 
intent_script: !include intent_scripts.yaml
rest_command: !include rest_command.yaml
sensor: !include sensors.yaml
timer: !include timers.yaml
media_player: 
  platform: mpd
  host: 192.168.XXX.XXX
  name: Local Media Player

# Smart Lights Control
sengledapi:
  username: user@example.com
  password: VERY-SECRET-PASSWORD
  country: us
  wifi: true

home-assistant.log

Traceback (most recent call last):
  File "/config/custom_components/sengledapi/sengledapi/sengledapi.py", line 253, in async_do_request
    return await Request(url, payload).async_get_response(jsessionId)
  File "/config/custom_components/sengledapi/sengledapi/devices/request.py", line 57, in async_get_response
    data = await resp.json()
  File "/usr/local/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 1097, in json
    raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://life2.cloud.sengled.com/life2/server/getServerInfo.json')

Also, this same error {'Attempt to decode JSON with unexpected mimetype: text/html'') has happened at https://ucenter.cloud.sengled.com/user/app/customer/v2/AuthenCross.json in a failure. Here's the fuller logs from this new and different failure:

2021-03-22 08:31:34 WARNING (MainThread) [homeassistant.loader] No 'version' key in the manifest file for custom integration 'sengledapi'. This will not be allowed in a future version of Home Assistant. Please report this to the maintainer of 'sengledapi'
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi.sengledapi.devices.bulbs.bulb] SengledApi: Initializing Bulbs
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi.sengledapi.devices.request] SengledApi: Initializing Request
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi]
-------------------------------------------------------------------
Sengled Bulb Home Assistant Integration Created from Config
Version: v0.2
This is a custom integration
If you have any issues with this you need to open an issue here:
https://github.com/jfarmer08/ha-sengledapi
-------------------------------------------------------------------
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi] Creating new SengledApi component
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi.sengledapi.sengledapi] Sengled Api initializing.
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi.sengledapi.sengledapi] Sengled Api initializing async.
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi.sengledapi.sengledapi] Sengledapi: Login
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi.sengledapi.sengledapi] SengledApi: Login Request.
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi.sengledapi.devices.request] SengledApi: Sengled Request initializing.
2021-03-22 08:31:53 INFO (MainThread) [custom_components.sengledapi.sengledapi.devices.request] SengledApi: Get Login Response async.
2021-03-22 08:31:55 INFO (MainThread) [custom_components.sengledapi.sengledapi.devices.request] SengledApi: Sengled Request initializing.
2021-03-22 08:31:55 INFO (MainThread) [custom_components.sengledapi.sengledapi.devices.request] SengledApi: Get Login Reponse.
2021-03-22 08:31:55 WARNING (MainThread) [homeassistant.util.async_] Detected I/O inside the event loop. This is causing stability issues. Please report issue to the custom component author for sengledapi doing I/O at custom_components/sengledapi/sengledapi/devices/request.py, line 64: r = requests.post(self._url, headers=self._header, data=self._payload)
2021-03-22 08:31:55 ERROR (MainThread) [homeassistant.setup] Error during setup of component sengledapi
Traceback (most recent call last):
File "/config/custom_components/sengledapi/sengledapi/sengledapi.py", line 261, in async_do_login_request
return await Request(url, payload).async_get_login_response()
File "/config/custom_components/sengledapi/sengledapi/devices/request.py", line 76, in async_get_login_response
data = await resp.json()
File "/usr/local/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 1097, in json
raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html', url=URL('https://ucenter.cloud.sengled.com/user/app/customer/v2/AuthenCross.json')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/setup.py", line 213, in _async_setup_component
result = await task
File "/config/custom_components/sengledapi/__init__.py", line 54, in async_setup
await sengledapi_account.async_init()
File "/config/custom_components/sengledapi/sengledapi/sengledapi.py", line 53, in async_init
self._access_token = await self.async_login(
File "/config/custom_components/sengledapi/sengledapi/sengledapi.py", line 78, in async_login
data = await self.async_do_login_request(url, payload)
File "/config/custom_components/sengledapi/sengledapi/sengledapi.py", line 263, in async_do_login_request
return Request(url, payload).get_login_response()
File "/config/custom_components/sengledapi/sengledapi/devices/request.py", line 65, in get_login_response
data = r.json()
File "/usr/local/lib/python3.8/site-packages/requests/models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/local/lib/python3.8/site-packages/simplejson/__init__.py", line 525, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/local/lib/python3.8/site-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

It looks like occasionally the integration is being sent HTML instead of JSON, perhaps, and that's causing an issue?

Thank you for maintaining the integration. It's very useful.

HeofHIShirts commented 3 years ago

Adding information about this bug, newly discovered: it only seems to happen when there is an element that comes before the sengledapi block in configuration.yaml that is formatted as a block, rather than as an include. If that element comes after the sengledapi block, the integration loads without issue or throwing an error. I don't know why this is the case, but so long as the sengledapi block is first, it's happy.

jfarmer08 commented 3 years ago

I dont think i understand. What do you mean by "so long as the sengledapi block is first?"

HeofHIShirts commented 3 years ago

in configuration.yaml, if

sengledapi:
  username: user@example.com
  password: VERY-SECRET-PASSWORD
  country: us
  wifi: true

is the first text after all the standalone integrations and those with !includes after them (so, from default:config to timer: include !timers.yaml in this file), then the integration throws no errors and works as expected, regardless of what comes below the sengledapi block. But if, as in the configuration above, there's any element with indents, such as:

media_player: 
  platform: mpd
  host: 192.168.XXX.XXX
  name: Local Media Player

above the sengledapi code, then the integration fails to load and throws out errors that it's getting 'text/html' when it is expecting JSON.

Does that make things any clearer?

HeofHIShirts commented 3 years ago

ETA: Or...not. I just tried to reproduce the error with a known good code with indent before the sengledapi code, and rather than throwing errors, Home Assistant smiled and booted all the integrations without any issues. It was probably a configuration error that was causing the issues, then, rather than the component itself.

Never mind, sorry about that.

Home Assistant does, however, ask me to let you know that

No 'version' key in the manifest file for custom integration 'sengledapi'. This will not be allowed in a future version of Home Assistant. Please report this to the maintainer of 'sengledapi'

So, in the next update, whenever it may be, we'll need a version key in the manifest.