__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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[str, int],
  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, settings=userdata)
  36. _CurtainMotor.mqtt_subscribe(mqtt_client=mqtt_client, settings=userdata)
  37. def _run(
  38. *,
  39. mqtt_host: str,
  40. mqtt_port: int,
  41. mqtt_disable_tls: bool,
  42. mqtt_username: typing.Optional[str],
  43. mqtt_password: typing.Optional[str],
  44. mqtt_topic_prefix: str,
  45. retry_count: int,
  46. device_passwords: typing.Dict[str, str],
  47. fetch_device_info: bool,
  48. ) -> None:
  49. # https://pypi.org/project/paho-mqtt/
  50. mqtt_client = paho.mqtt.client.Client(
  51. userdata=_MQTTCallbackUserdata(
  52. retry_count=retry_count,
  53. device_passwords=device_passwords,
  54. fetch_device_info=fetch_device_info,
  55. mqtt_topic_prefix=mqtt_topic_prefix,
  56. )
  57. )
  58. mqtt_client.on_connect = _mqtt_on_connect
  59. _LOGGER.info(
  60. "connecting to MQTT broker %s:%d (TLS %s)",
  61. mqtt_host,
  62. mqtt_port,
  63. "disabled" if mqtt_disable_tls else "enabled",
  64. )
  65. if not mqtt_disable_tls:
  66. mqtt_client.tls_set(ca_certs=None) # enable tls trusting default system certs
  67. if mqtt_username:
  68. mqtt_client.username_pw_set(username=mqtt_username, password=mqtt_password)
  69. elif mqtt_password:
  70. raise ValueError("Missing MQTT username")
  71. mqtt_client.connect(host=mqtt_host, port=mqtt_port)
  72. # https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/client.py#L1740
  73. mqtt_client.loop_forever()