HASwitchPlate / openHASP-custom-component

Home Assistant custom component for openHASP
https://www.openhasp.com
MIT License
51 stars 12 forks source link

'str' object has no attribute 'read' #131

Closed illuzn closed 4 months ago

illuzn commented 4 months ago

Version of the custom_component

0.7.3

Configuration

yaml not relevant.

OpenHASP is configured to send a json file (not jsonl). The json file is correctly formatted and is in HA's allowed external directories.

Describe the bug

The following handler fails to load the json file correctly. https://github.com/HASwitchPlate/openHASP-custom-component/blob/24f071cb510f3da0e5c801ccd52eb5abad9c4d6b/custom_components/openhasp/__init__.py#L656-L667

json.load expects a file object but it appears to be receiving a string instead.

Debug log


2024-07-03 18:51:51.891 ERROR (MainThread) [homeassistant.util.logging] Exception in lwt_message_received when handling msg on 'hasp/hallway_panel/LWT': 'online'
Traceback (most recent call last):
File "/config/custom_components/openhasp/__init__.py", line 479, in lwt_message_received
await self.async_load_page(self._pages_jsonl)
File "/config/custom_components/openhasp/__init__.py", line 658, in async_load_page
json_data = json.load(pages_file)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/json/__init__.py", line 293, in load
return loads(fp.read(),
^^^^^^^
AttributeError: 'str' object has no attribute 'read'
Woutch commented 4 months ago

I have the same problem, rollback to 0.7.2 worked for now.

fvanroie commented 4 months ago

dgomes I believe it should be either

     json_data = json.loads(pages_file) 

or

     json_data = json.load(path) 
dgomes commented 4 months ago

Yes that's the solution, but need to validate

illuzn commented 4 months ago

I'm absolutely stumped... I upgraded to HA 2024.7.2 (from 2024.6.x - I forget which I was running when I made this issue) and I just plugged my device in for other reasons and it just worked.

No idea what is going on.

Edit: Ignore my last, I had manually changed the code on line 658 to:

                json_data = json.loads(pages_file)

and I'd forgotten I'd done that in the meantime. Whoops I'll send through a PR with this fix.