diff --git a/custom_components/watchman/__init__.py b/custom_components/watchman/__init__.py index f18a6a6..6ad9b13 100644 --- a/custom_components/watchman/__init__.py +++ b/custom_components/watchman/__init__.py @@ -95,15 +95,18 @@ def notification(title, message, n_id="watchman"): notification_id=n_id, ) - def report_to_file(hass, config, path): - """ "save report to a file""" + def get_report_path(path): # if path not specified, create report in config directory with default filename if not path: path = os.path.join(hass.config.config_dir, DEFAULT_REPORT_FILENAME) folder, _ = os.path.split(path) if not os.path.exists(folder): _LOGGER.error(f"Incorrect `report_path` {path}.") + raise Exception + return path + def report_to_file(hass, config, path): + """ "save report to a file""" delayed_refresh(0) report_chunks = report(hass, config, table_renderer, chunk_size=0) @@ -116,6 +119,7 @@ def report_to_notification(hass, config, service_str, service_data, chunk_size): if not service_str: service_str = config[DOMAIN].get(CONF_SERVICE_NAME, None) service_data = config[DOMAIN].get(CONF_SERVICE_DATA, {}) + if not service_str: notification( "Watchman Error", @@ -144,21 +148,35 @@ def report_to_notification(hass, config, service_str, service_data, chunk_size): ) break + def onboarding(service, path): + service = service or config[DOMAIN].get(CONF_SERVICE_NAME, None) + return not (service or os.path.exists(path)) + def handle_report(call): """Handle the service call""" if call.data.get(CONF_PARSE_CONFIG, False): parse_config() - if call.data.get(CONF_CREATE_FILE, True): - path = config[DOMAIN].get(CONF_REPORT_PATH, None) - report_to_file(hass, config, path) + path = get_report_path(config[DOMAIN].get(CONF_REPORT_PATH, None)) if call.data.get(CONF_SEND_NITIFICATION, True): chunk_size = call.data.get(CONF_CHUNK_SIZE, None) service = call.data.get(CONF_SERVICE_NAME, None) service_data = call.data.get(CONF_SERVICE_DATA, None) - report_to_notification(hass, config, service, service_data, chunk_size) + if onboarding(service, path): + notification( + "🖖 Achievement unlocked: first report!", + f"Your first watchman report was stored in `{path}` \n\n " + "TIP: set `service` parameter in configuration.yaml file to " + "receive report via notification service of choice. \n\n " + "This is one-time message, it will not bother you in the future.", + ) + else: + report_to_notification(hass, config, service, service_data, chunk_size) + + if call.data.get(CONF_CREATE_FILE, True): + report_to_file(hass, config, path) def get_included_folders(config): """gather the list of folders to parse""" diff --git a/custom_components/watchman/manifest.json b/custom_components/watchman/manifest.json index 0c3a5a8..0372cd2 100644 --- a/custom_components/watchman/manifest.json +++ b/custom_components/watchman/manifest.json @@ -4,7 +4,7 @@ "documentation": "https://github.com/dummylabs/thewatchman", "issuetracker": "https://github.com/dummylabs/thewatchman/issues", "iot_class": "local_push", - "version": "0.2.0", + "version": "0.2.5", "requirements": ["prettytable==3.0.0"], "codeowners": [ "@dummylabs"