# -*- coding: utf-8 -*-
from setuptools import setup

packages = \
['openhasp_config_manager',
 'openhasp_config_manager.model',
 'openhasp_config_manager.processing',
 'openhasp_config_manager.processing.jsonl',
 'openhasp_config_manager.ui',
 'openhasp_config_manager.validation']

package_data = \
{'': ['*']}

install_requires = \
['Jinja2', 'click', 'paho-mqtt', 'py-range-parse', 'pyyaml', 'requests']

entry_points = \
{'console_scripts': ['openhasp-config-manager = '
                     'openhasp_config_manager.cli:cli']}

setup_kwargs = {
    'name': 'openhasp-config-manager',
    'version': '0.1.0',
    'description': 'A tool to manage all of your openHASP device configs in a centralized place.',
    'long_description': '# openhasp-config-manager\n\nA tool to manage all of your [openHASP](https://github.com/HASwitchPlate/openHASP) device configs in a centralized\nplace.\n\n# Features\n\n* [x] manage as many devices as you like\n* [x] share configuration files between devices\n* [x] jsonl preprocessing, which allows for\n    * [x] `//` comments within jsonl files\n    * [x] line breaks wherever you like\n    * [x] jinja2 templating within object values\n    * [x] local and globally scoped variables\n* [x] output validation for\n    * [x] jsonl object\n    * [x] cmd files\n* [x] one click configuration upload to the device\n    * [x] automatic diffing to only update changed configuration files\n    * [x] git-style diff output for changed lines\n* [x] execute commands directly from the CLI (still needs a connection to the MQTT broker)\n\n# Disclaimer\n\n**TL;DR: This project is still experimental.**\n\nI do use openhasp-config-manager exclusively to configure all of my openHASP devices. I am in the\nprocess of adding tests to everything to make it more reliable and have also added lots of features along the way.\nHowever, there are definitely still a couple of things that do not yet work as intended. Error logs\nmight need some love to be able to figure out what you did wrong. If you like the\nproject, feel free to open an issue or PR to help me out.\n\n# How to use\n\n## Installation\n\nSince openhasp-config-manager needs some dependencies (see [here](/pyproject.toml)) it is\nrecommended to install it inside a virtualenv:\n\n```bash\nmkdir -p ~/venvs/openhasp-config-manager\npython3 -m venv ~/venvs/openhasp-config-manager\nsource ~/venvs/openhasp-config-manager/bin/activate\npip3 install openhasp-config-manager\nopenhasp-config-manager -h\n```\n\n## Configuration\n\nopenhasp-config-manager is first and foremost a configuration\nmanagement system. Simply follow the basic folder structure and\nconfig deployment will become trivial. **Please read all of this,\nas it is very important to understand the basic structure on\nwhich everything relies.**\n\n### Folder Structure\n\nThe following folders should reside inside a single parent\nfolder, f.ex. named `openhasp-configs`. This folder can be\nlocated anywhere you like, but must be accessible to\nopenhasp-config-manager when executing.\n\n* `common`: The `common` subdirectory can be used for files\n  that should be included on _all_ device. This folder is optional.\n* `devices`: The `devices` folder is required. It must contain one\n  subfolder for each openHASP device you want to configure using\n  openhasp-config-maager. It is recommended to name subfolders according\n  to the physical devices associated with them.\n    * `touch_down_1` (example device folder)\n        * A device folder contains `*.jsonl`, `*.cmd` and other files which should\n          only be uploaded to that particular device.\n        * You can create arbitrary nested folder structures for organizing the files.\n          There is a limit to the file name length though,\n          see [FAQ](#output-file-name-length-must-not-exceed-30-characters)\n        * You must provide a `config.json` file, see [config.json](#config.json)\n          for more info.\n\nA more advanced configuration layout could look something like this:\n\n```text\nopenhasp-configs\n├── common\n│\xa0\xa0 ├── content\n│\xa0\xa0 │\xa0\xa0 └── card.jsonl\n│\xa0\xa0 ├── dialog\n│\xa0\xa0 │\xa0\xa0 ├── connected.jsonl\n│\xa0\xa0 │\xa0\xa0 └── offline.jsonl\n│\xa0\xa0 ├── navigation_bar.jsonl\n│\xa0\xa0 └── page_header.jsonl\n└── devices\n    └── touch_down_1\n        ├── 0_home\n        │\xa0\xa0 ├── 0_header.jsonl\n        │\xa0\xa0 ├── 1_content.jsonl\n        │\xa0\xa0 └── page.cmd\n        ├── 5_about\n        │\xa0\xa0 ├── 0_header.jsonl\n        │\xa0\xa0 ├── 1_content.jsonl\n        │\xa0\xa0 └── page.cmd\n        ├── boot.cmd\n        ├── config.json\n        ├── offline.cmd\n        └── online.cmd\n```\n\n### config.json\n\nopenhasp-config-manager makes use of the `config.json` on your plate. It can extract information\nfrom it to detect things like screen orientation, and also allows you to deploy changes within the\n`config.json` file. Since [the official API does not support\nuploading the full file](https://github.com/HASwitchPlate/openHASP/issues/363), only settings\nwhich can also be set through the web ui on the plate itself are currently supported.\n\nTo retrieve the initial version of the `config.json` file you can use the\nbuilt-in file browser integrated into the webserver of your openHASP plate, see\n[official docs](https://www.openhasp.com/latest/faq/?h=web#is-there-a-file-browser-built-in).\n\nThe official `config.json` file doesn\'t provide enough info for openhasp-config-manager\nto enable all of its features though. To fix that simply add a section to the\nfile after downloading it:\n\n```json\n{\n  "openhasp_config_manager": {\n    "device": {\n      "ip": "192.168.5.134",\n      "screen": {\n        "width": 320,\n        "height": 480\n      }\n    }\n  },\n  "wifi": {\n    "ssid": "Turris IoT",\n    ...\n  }\n```\n\n### Config File Preprocessing\n\nopenhasp-config-manager runs all configuration files through various preprocessors, which allow us to use\nfeatures the original file formats do not support by themselves, like f.ex. templating.\n\n#### Templating\n\nYou can use Jinja2 templates inside all jsonl object values. To access the value of another object in a\ntemplate, you can use the `pXbY` syntax established by openHASP, where `X` is the `page` of an object and\n`Y` is its `id`. openhasp-config-manager even tries to resolve templates that lead to other templates.\nBe careful not to create loops in this way though.\n\nYou can use the full functionality of Jinja2 like f.ex. math operations, function calls or type conversions.\n\n```yaml\n{\n  "page": 1,\n  "id": 1,\n  "x": 0,\n  "y": 0,\n  ...\n}\n\n  {\n    "page": 1,\n    "id": 2,\n    "x": "{{ p1b1.x }}",\n    "y": "{{ p1b1.y + 10 }}",\n    ...\n  }\n```\n\n#### Variables\n\nBesides accessing other objects, you can also define custom variables yourself, which can then\nbe referenced inside of templates. Variables are defined using `*.yaml` files. If you\ndecided to use a subfolder structure to organize your configuration files you can use these folders\nto also set the scope of variables. More specific variable definitions (longer path) will override\nless specific ones.\n\n##### Global\n\nGlobal variables can be specified by creating `*.yaml` files inside the root config folder (f.ex. `openhasp-configs`).\n\nExample:\n\n`openhasp-configs/global.vars.yaml`\n\n```yaml\nabout:\n  page_title: "About"\n```\n\nTo access this variable, use a Jinja2 template:\n\n`openhasp-configs/common/about_page.jsonl`\n\n```json lines\n{\n  "page": 9,\n  "id": 1,\n  ...\n  "title": "{{ about.page_title }}",\n  ...\n}\n```\n\n##### Device specific\n\nDevice specific variables can be specified by creating `*.yaml` files inside any of the sub-folders\nof a `device` folder.\n\n> **Note**\n>\n> Device specific variables will override global variables, given the same name.\n\nExample:\n\n`openhasp-configs/device/my_device/device.vars.yaml`\n\n```yaml\npage_title: "My Device"\n```\n\n`openhasp-configs/device/my_device/some_folder/some_page.jsonl`\n\n```json lines\n{\n  "page": 1,\n  "id": 1,\n  ...\n  "title": "{{ page_title }}",\n  ...\n}\n```\n\n`openhasp-configs/device/my_device/some_other_folder/some_page.jsonl`\n\n```json lines\n{\n  "page": 2,\n  "id": 1,\n  ...\n  "title": "{{ page_title }}",\n  ...\n}\n```\n\n## Deployment\n\nTo deploy your configurations to the already connected openHASP devices, simply use the\ncommand line tool `openhasp-config-manager`:\n\n```shell\n> openhasp-config-manager                                                         0 (0.604s) < 02:11:38\nUsage: openhasp-config-manager [OPTIONS] COMMAND [ARGS]...\n\nOptions:\n  --version   Show the version and exit.\n  -h, --help  Show this message and exit.\n\nCommands:\n  cmd       Sends a command request to a device.\n  deploy    Combines the generation and upload of a configuration.\n  generate  Generates the output files for all devices in the given...\n  upload    Uploads the previously generated configuration to their...\n```\n\n## Run commands\n\nWhile openhasp-config-manager is first and foremost a config management system,\nit also allows you to run commands on a device by issuing MQTT messages without the need to install a separate\nMQTT client first. Note that the MQTT _server_ still needs to be running and also has to\nbe reachable from your local machine for this to work.\n\nFor a list of possible commands to send to a device, take a look at the official\ndocumentation: https://openhasp.haswitchplate.com/latest/commands/\n\n```shell\n> openhasp-config-manager cmd -c ./openhasp-configs -d plate35 -C backlight -p "{\\"state\\":\\"on\\",\\"brightness\\":128}"\n```\n\n# FAQ\n\n## How do I see device logs?\n\nWith the device connected via USB cable, open a terminal and run:\n\n```shell\nbash -c "screen -q -L -Logfile device.log /dev/ttyUSB0 115200 &> /dev/null; tail -F device.log; killall screen"\n```\n\n## Output file name length must not exceed 30 characters\n\nIf you want to organize your files (both common and device-specific ones) you can\nsimply create subfolders to achieve your desired structure. However, due to a technical\nlimitation openHASP does not support subfolder on the actual device. To overcome\nthis limitation openhasp-config-manager will automatically generate a file name for\nfiles in subfolders before uploading them to the device. `.json` or `.cmd` files within subfolders\nwill be renamed by concatenating their full subpath using an underscore (`_`) as a separator. So f.ex.\nthe file in the following structure:\n\n```text\nopenhasp-configs\n└── devices\n    └── touch_down_1\n        └── 0_home\n            └── 0_header.jsonl\n```\n\nwould be uploaded to the `touch_down_1` device with the name `0_home_0_header.jsonl`.\n\n# Contributing\n\nGitHub is for social coding: if you want to write code, I encourage contributions\nthrough pull requests from forks of this repository. Create GitHub tickets for\nbugs and new features and comment on the ones that you are interested in.\n\n# License\n\n```text\nopenhasp-config-manager is free software: you can redistribute it and/or modify\nit under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program.  If not, see <https://www.gnu.org/licenses/>.\n```\n',
    'author': 'Markus Ressel',
    'author_email': 'mail@markusressel.de',
    'maintainer': 'None',
    'maintainer_email': 'None',
    'url': 'https://github.com/markusressel/openhasp-config-manager',
    'packages': packages,
    'package_data': package_data,
    'install_requires': install_requires,
    'entry_points': entry_points,
    'python_requires': '>=3.10,<4.0',
}


setup(**setup_kwargs)
