From 1fc97823e141c4d211ee752e5fc95b5288d9a8c5 Mon Sep 17 00:00:00 2001 From: Carlo Mion Date: Sat, 24 Jun 2023 07:13:14 +0200 Subject: [PATCH] feat(MQTT): enable to set mqtt options in add-on configuration --- hikvision-doorbell/config.yaml | 9 ++++++++- hikvision-doorbell/docs/DEVELOPMENT.md | 16 ++++++---------- hikvision-doorbell/src/config.py | 19 ++++++++++++++++++- hikvision-doorbell/translations/en.yaml | 7 ++++++- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/hikvision-doorbell/config.yaml b/hikvision-doorbell/config.yaml index 72fc7b0..31c8a6e 100644 --- a/hikvision-doorbell/config.yaml +++ b/hikvision-doorbell/config.yaml @@ -22,7 +22,8 @@ options: system: log_level: INFO sdk_log_level: NONE - + mqtt: {} + # Schema for the options above schema: doorbells: @@ -34,6 +35,12 @@ schema: system: log_level: match(^ERROR|WARNING|INFO|DEBUG$) sdk_log_level: match(^NONE|ERROR|INFO|DEBUG$)? + mqtt: + host: "str?" + port: "int?" + ssl: "bool?" + username: "str?" + password: "str?" # To request MQTT configuration using the supervisor API services: diff --git a/hikvision-doorbell/docs/DEVELOPMENT.md b/hikvision-doorbell/docs/DEVELOPMENT.md index 8f35a3d..e032727 100644 --- a/hikvision-doorbell/docs/DEVELOPMENT.md +++ b/hikvision-doorbell/docs/DEVELOPMENT.md @@ -28,18 +28,14 @@ python src/main.py ``` ## VSCode -If using VSCode, there is a run configuration already provided. -First create a `development.env` file with your own values -```bash -cp development.env.example development.env -``` - If using VSCode, there is a run configuration already provided. -First create a `development.env` file with your own values, then run the application using the integrated VSCode debugger. -```bash -cp development.env.example development.env -``` -Run the application using the integrated VSCode runner (under `Run and Debug`). + + First create a `development.env` file with your own values, then run the application using the integrated VSCode debugger. + ```bash + cp development.env.example development.env + ``` +- Run the application using the integrated VSCode runner (under `Run and Debug`). ## Testing the addon locally (VSCode devcontainer) For more information see the official HA [guide](https://developers.home-assistant.io/docs/add-ons/testing). diff --git a/hikvision-doorbell/src/config.py b/hikvision-doorbell/src/config.py index ed67909..a4f52a3 100644 --- a/hikvision-doorbell/src/config.py +++ b/hikvision-doorbell/src/config.py @@ -38,7 +38,7 @@ def mqtt_config_from_supervisor(): if service_response.status_code == 400: # MQTT addon is not configured logger.error("MQTT service not available") - raise RuntimeError("This addon need the mosquitto broker to work correctly. Please see the Documentation tab for details.") + raise RuntimeError("This addon needs the Mosquitto broker to work correctly. Please see the Documentation tab for details.") if service_response.status_code != 200: raise RuntimeError(f"Unexpected response while requesting MQTT service: {service_response.text}") @@ -110,6 +110,23 @@ def from_string_to_enum(cls, v): mqtt: Optional[MQTT] = Field(default_factory=mqtt_config_from_supervisor) system: System + @validator('mqtt', pre=True) + def load_mqtt_config(cls, v): + ''' + Load the MQTT configuration from the user-supplied values, if provided, or fallback to asking the HA supervisor for the integrated MQTT add-on + ''' + + # If the user supplied some configuration values, ues them + if v: + return v + + # Try to load configuration from the HA supervisor, if running as an add-on + logger.debug("Loading MQTT configuration from supervisor") + config = mqtt_config_from_supervisor() + if not config: + raise ValueError("Cannot load MQTT configuration from supervisor") + return config + class Config: env_nested_delimiter = "__" # Name of the environment variable defining the path to the configuration file diff --git a/hikvision-doorbell/translations/en.yaml b/hikvision-doorbell/translations/en.yaml index c1b5b21..7ef2e15 100644 --- a/hikvision-doorbell/translations/en.yaml +++ b/hikvision-doorbell/translations/en.yaml @@ -8,4 +8,9 @@ configuration: system: name: "System options" description: >- - Configure general options for the add-on itself, such as its logs. See the 'Documentation' tab for more details. \ No newline at end of file + Configure general options for the add-on itself, such as its logs. See the 'Documentation' tab for more details. + + mqtt: + name: "MQTT broker" + description: >- + Optional settings to configure the connection to an external broker. By default the add-on auto-discovers the Mosquitto add-on, if available. \ No newline at end of file