__init__.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # switchbot-mqtt - MQTT client controlling SwitchBot button automators,
  2. # compatible with home-assistant.io's MQTT Switch 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 argparse
  19. import logging
  20. import typing
  21. import paho.mqtt.client
  22. _LOGGER = logging.getLogger(__name__)
  23. _MQTT_SET_TOPIC_PATTERN = (
  24. "homeassistant/switch/switchbot/{mac_address}/set" # TODO parametrize
  25. )
  26. def _mqtt_on_connect(
  27. mqtt_client: paho.mqtt.client.Client,
  28. user_data: typing.Any,
  29. flags: typing.Dict,
  30. return_code: int,
  31. ) -> None:
  32. # pylint: disable=unused-argument; callback
  33. # https://github.com/eclipse/paho.mqtt.python/blob/v1.5.0/src/paho/mqtt/client.py#L441
  34. assert return_code == 0, return_code # connection accepted
  35. mqtt_broker_host, mqtt_broker_port = mqtt_client.socket().getpeername()
  36. _LOGGER.debug("connected to MQTT broker %s:%d", mqtt_broker_host, mqtt_broker_port)
  37. # https://www.home-assistant.io/docs/mqtt/discovery/#discovery_prefix
  38. mqtt_client.subscribe(_MQTT_SET_TOPIC_PATTERN.format(mac_address="+"))
  39. def _mqtt_on_message(
  40. mqtt_client: paho.mqtt.client.Client,
  41. user_data: typing.Any,
  42. message: paho.mqtt.client.MQTTMessage,
  43. ) -> None:
  44. # pylint: disable=unused-argument; callback
  45. # https://github.com/eclipse/paho.mqtt.python/blob/v1.5.0/src/paho/mqtt/client.py#L469
  46. _LOGGER.debug("received topic=%s payload=%r", message.topic, message.payload)
  47. if message.retain:
  48. _LOGGER.info("ignoring retained message")
  49. return
  50. print("TODO", message.topic, message.payload)
  51. def _main() -> None:
  52. logging.basicConfig(
  53. level=logging.DEBUG,
  54. format="%(asctime)s:%(levelname)s:%(message)s",
  55. datefmt="%Y-%m-%dT%H:%M:%S%z",
  56. )
  57. argparser = argparse.ArgumentParser(
  58. "MQTT client controlling SwitchBot button automators, "
  59. "compatible with home-assistant.io's MQTT Switch platform"
  60. )
  61. argparser.add_argument("--mqtt-host", type=str, required=True)
  62. argparser.add_argument("--mqtt-port", type=int, default=1883)
  63. args = argparser.parse_args()
  64. # https://pypi.org/project/paho-mqtt/
  65. mqtt_client = paho.mqtt.client.Client()
  66. mqtt_client.on_connect = _mqtt_on_connect
  67. mqtt_client.on_message = _mqtt_on_message
  68. _LOGGER.info(
  69. "connecting to MQTT broker %s:%d", args.mqtt_host, args.mqtt_port,
  70. )
  71. mqtt_client.connect(host=args.mqtt_host, port=args.mqtt_port)
  72. mqtt_client.loop_forever()