__init__.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # switchbot-mqtt - MQTT client controlling SwitchBot button & curtain automators,
  2. # compatible with home-assistant.io's MQTT Switch & Cover platform
  3. #
  4. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import logging
  19. import typing
  20. import paho.mqtt.client
  21. from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
  22. from switchbot_mqtt._actors._base import _MQTTCallbackUserdata
  23. _LOGGER = logging.getLogger(__name__)
  24. def _mqtt_on_connect(
  25. mqtt_client: paho.mqtt.client.Client,
  26. userdata: _MQTTCallbackUserdata,
  27. flags: typing.Dict,
  28. return_code: int,
  29. ) -> None:
  30. # pylint: disable=unused-argument; callback
  31. # https://github.com/eclipse/paho.mqtt.python/blob/v1.5.0/src/paho/mqtt/client.py#L441
  32. assert return_code == 0, return_code # connection accepted
  33. mqtt_broker_host, mqtt_broker_port = mqtt_client.socket().getpeername()
  34. _LOGGER.debug("connected to MQTT broker %s:%d", mqtt_broker_host, mqtt_broker_port)
  35. _ButtonAutomator.mqtt_subscribe(mqtt_client=mqtt_client)
  36. _CurtainMotor.mqtt_subscribe(mqtt_client=mqtt_client)
  37. def _run(
  38. *,
  39. mqtt_host: str,
  40. mqtt_port: int,
  41. mqtt_username: typing.Optional[str],
  42. mqtt_password: typing.Optional[str],
  43. retry_count: int,
  44. device_passwords: typing.Dict[str, str],
  45. fetch_device_info: bool,
  46. ) -> None:
  47. # https://pypi.org/project/paho-mqtt/
  48. mqtt_client = paho.mqtt.client.Client(
  49. userdata=_MQTTCallbackUserdata(
  50. retry_count=retry_count,
  51. device_passwords=device_passwords,
  52. fetch_device_info=fetch_device_info,
  53. )
  54. )
  55. mqtt_client.on_connect = _mqtt_on_connect
  56. _LOGGER.info("connecting to MQTT broker %s:%d", mqtt_host, mqtt_port)
  57. if mqtt_username:
  58. mqtt_client.username_pw_set(username=mqtt_username, password=mqtt_password)
  59. elif mqtt_password:
  60. raise ValueError("Missing MQTT username")
  61. mqtt_client.connect(host=mqtt_host, port=mqtt_port)
  62. # https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/client.py#L1740
  63. mqtt_client.loop_forever()