cagnulein / switchbot_press

This is a simple project that manage the Switchbot ( https://amzn.to/3dnliBD ) that has only the "press" ability in Home Assistant.
GNU Lesser General Public License v3.0
14 stars 5 forks source link

HA v2022.2.0 causes integration to fail to load #8

Closed smithbill17 closed 2 years ago

smithbill17 commented 2 years ago

After upgrading Home Assistant to v2022.2.0, this integration is failing to load. Can you please fix this issue as soon as you can?

This error originated from a custom integration.

Logger: homeassistant.config
Source: custom_components/switchbot_press/switch.py:9
Integration: switchbot_press
First occurred: 12:28:20 (1 occurrences)
Last logged: 12:28:20

Platform error: switch
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config.py", line 877, in async_process_component_config
    platform = p_integration.get_platform(domain)
  File "/usr/src/homeassistant/homeassistant/loader.py", line 566, in get_platform
    cache[full_name] = self._import_platform(platform_name)
  File "/usr/src/homeassistant/homeassistant/loader.py", line 583, in _import_platform
    return importlib.import_module(f"{self.pkg_path}.{platform_name}")
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/config/custom_components/switchbot_press/switch.py", line 9, in <module>
    from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
ImportError: cannot import name 'SwitchDevice' from 'homeassistant.components.switch' (/usr/src/homeassistant/homeassistant/components/switch/__init__.py)
cagnulein commented 2 years ago

Hi @smithbill17 thanks for reporting this. I don't have the time right now, but I will be happy to merge a PR if you can do it

smithbill17 commented 2 years ago

Sorry, I don't know how to develop Python stuff - I'm an end-user, so wouldn't know where to begin.

smithbill17 commented 2 years ago

Also, the failed integration prevents Home Assistant from being restarted as it generates the below error everytime you try to restart Home Assistant:

Failed to restart Home AssistantCore
The system cannot restart because the configuration is not valid: Platform error switch.switchbot_press - cannot import name 'SwitchDevice' from 'homeassistant.components.switch' (/usr/src/homeassistant/homeassistant/components/switch/__init__.py)
cagnulein commented 2 years ago

i didn't try but pull this commit https://github.com/cagnulein/switchbot_press/commit/b936c9fc982ac011b38b35493b50d14b917fb1c5 and let me know if it's better or not. Thanks

smithbill17 commented 2 years ago

Sorry - I don't really know how you "pull a commit" but looking at the change made to the manifest.json file, the only change is to bump the version number to 0.8.1, so I went to my existing manifest.json file in the switchbot_press custom component & edited the version number & saved it. But it's made no difference, I still get the below error when trying to restart Home Assistant:

Platform error switch.switchbot_press - cannot import name 'SwitchDevice' from 'homeassistant.components.switch' (/usr/src/homeassistant/homeassistant/components/switch/__init__.py)

cagnulein commented 2 years ago

@smithbill17 no i did another change before that. please download the new zip file https://github.com/cagnulein/switchbot_press/archive/refs/heads/master.zip

smithbill17 commented 2 years ago

Okay, downloaded the zip, extracted the 3 files and copied them up to switchbot_press in my custom components, restarted Home Assistant and it seems to be working again - brilliant! Thanks for the quick fix! What exactly was the issue with the new HA v2022.2.0 ?

cagnulein commented 2 years ago

@smithbill17 great! they changed the way how call a switch device.

smithbill17 commented 2 years ago

I may have spoken too soon. I tried controlling my SwitchBot a 2nd time from within HomeAssistant (as normal) and it generated this error:

[2897047336] [Errno 32] Broken pipe

Traceback (most recent call last):

File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 93, in _sendcommand

send_success = self._writekey(command)

File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 77, in _writekey

hand = hand_service.getCharacteristics(HANDLE)[0]

File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 144, in getCharacteristics

self.chars = [] if self.hndEnd <= self.hndStart else self.peripheral.getCharacteristics(self.hndStart, self.hndEnd)

File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 508, in getCharacteristics

rsp = self._getResp('find')

File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 407, in _getResp

resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)

File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 361, in _waitResp

self._stopHelper()

File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 293, in _stopHelper

self._helper.stdin.flush()

BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 190, in handle_call_service

await hass.services.async_call(

File "/usr/src/homeassistant/homeassistant/core.py", line 1630, in async_call

task.result()

File "/usr/src/homeassistant/homeassistant/core.py", line 1667, in _execute_service

await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(

File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 204, in handle_service

await self.hass.helpers.service.entity_service_call(

File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 668, in entity_service_call

future.result() # pop exception if have

File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 921, in async_request_call

await coro

File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 705, in _handle_entity_call

await result

File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 975, in async_turn_on

await self.hass.async_add_executor_job(ft.partial(self.turn_on, **kwargs))

File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run

result = self.fn(*self.args, **self.kwargs)

File "/config/custom_components/switchbot_press/switch.py", line 61, in turn_on

if self._device.press():

File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 117, in press

return self._sendcommand(PRESS_KEY, self._retry_count)

File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 97, in _sendcommand

self._disconnect()

File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 58, in _disconnect

self._device.disconnect()

File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 453, in disconnect

self._writeCmd("disc\n")

File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 305, in _writeCmd

self._helper.stdin.flush()

BrokenPipeError: [Errno 32] Broken pipe
cagnulein commented 2 years ago

@smithbill17 this seems an error with your bluetooth dongle, restart it and retry.

smithbill17 commented 2 years ago

I'm just using the inbuilt Bluetooth on RPi4b, so I just restarted the host & tried again & this time didn't get the error.

Only issue appearing in the log is the below warning. Thanks for your quick work!

Logger: homeassistant.helpers.entity Source: helpers/entity.py:574 First occurred: 13:37:01 (1 occurrences) Last logged: 13:37:01

Entity switch.switchbot_press (<class 'custom_components.switchbot_press.switch.SwitchBot_press'>) implements device_state_attributes. Please report it to the custom component author.