__init__.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 socket
  20. import typing
  21. import paho.mqtt.client
  22. from switchbot_mqtt._actors import _ButtonAutomator, _CurtainMotor
  23. from switchbot_mqtt._actors.base import _MQTTCallbackUserdata
  24. _LOGGER = logging.getLogger(__name__)
  25. def _mqtt_on_connect(
  26. mqtt_client: paho.mqtt.client.Client,
  27. userdata: _MQTTCallbackUserdata,
  28. flags: typing.Dict[str, int],
  29. return_code: int,
  30. ) -> None:
  31. # pylint: disable=unused-argument; callback
  32. # https://github.com/eclipse/paho.mqtt.python/blob/v1.5.0/src/paho/mqtt/client.py#L441
  33. assert return_code == 0, return_code # connection accepted
  34. mqtt_broker_host, mqtt_broker_port, *_ = mqtt_client.socket().getpeername()
  35. # https://www.rfc-editor.org/rfc/rfc5952#section-6
  36. _LOGGER.debug(
  37. "connected to MQTT broker %s:%d",
  38. f"[{mqtt_broker_host}]"
  39. if mqtt_client.socket().family == socket.AF_INET6
  40. else mqtt_broker_host,
  41. mqtt_broker_port,
  42. )
  43. _ButtonAutomator.mqtt_subscribe(mqtt_client=mqtt_client, settings=userdata)
  44. _CurtainMotor.mqtt_subscribe(mqtt_client=mqtt_client, settings=userdata)
  45. def _run(
  46. *,
  47. mqtt_host: str,
  48. mqtt_port: int,
  49. mqtt_disable_tls: bool,
  50. mqtt_username: typing.Optional[str],
  51. mqtt_password: typing.Optional[str],
  52. mqtt_topic_prefix: str,
  53. retry_count: int,
  54. device_passwords: typing.Dict[str, str],
  55. fetch_device_info: bool,
  56. ) -> None:
  57. # https://pypi.org/project/paho-mqtt/
  58. mqtt_client = paho.mqtt.client.Client(
  59. userdata=_MQTTCallbackUserdata(
  60. retry_count=retry_count,
  61. device_passwords=device_passwords,
  62. fetch_device_info=fetch_device_info,
  63. mqtt_topic_prefix=mqtt_topic_prefix,
  64. )
  65. )
  66. mqtt_client.on_connect = _mqtt_on_connect
  67. _LOGGER.info(
  68. "connecting to MQTT broker %s:%d (TLS %s)",
  69. mqtt_host,
  70. mqtt_port,
  71. "disabled" if mqtt_disable_tls else "enabled",
  72. )
  73. if not mqtt_disable_tls:
  74. mqtt_client.tls_set(ca_certs=None) # enable tls trusting default system certs
  75. if mqtt_username:
  76. mqtt_client.username_pw_set(username=mqtt_username, password=mqtt_password)
  77. elif mqtt_password:
  78. raise ValueError("Missing MQTT username")
  79. mqtt_client.connect(host=mqtt_host, port=mqtt_port)
  80. # https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/client.py#L1740
  81. mqtt_client.loop_forever()