Skip to content

Commit

Permalink
[NEW FEATURE] Add addons to available variables (#15)
Browse files Browse the repository at this point in the history
* Remove dependency to get_hacs() method, which was removed in hacs 0.19.0

* Set hacs version in hacs.json and fix CR suggestions

* Introduce new variable addons with the list of installed HA addons

* Update the default template and README

* Apply suggestions from code review

Co-authored-by: Joakim Sørensen <[email protected]>

Co-authored-by: Joakim Sørensen <[email protected]>
  • Loading branch information
l-j and ludeeus authored May 28, 2022
1 parent 0f76464 commit 208ae25
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 38 deletions.
68 changes: 43 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
[![Discord][discord-shield]][discord]
[![Community Forum][forum-shield]][forum]

_Use Jinja and data from Home Assistant to generate your README.md file_
_Use Jinja and data from Home Assistant to generate your README.md file
with the list of all your installed add-ons and custom components_


## Installation

1. Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`).
2. If you do not have a `custom_components` directory (folder) there, you need to create it.
3. In the `custom_components` directory (folder) create a new folder called `readme`.
4. Download _all_ the files from the `custom_components/readme/` directory (folder) in this repository.
5. Place the files you downloaded in the new directory (folder) you created.
6. Restart Home Assistant
7. Choose:
1. Download it with HACS
2. Restart Home Assistant
3. Choose:
- Add `readme:` to your HA configuration.
- In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Generate readme"

Expand Down Expand Up @@ -65,13 +62,16 @@ When you are happy with how the template look, run the service `readme.generate`

## Usable variables

In addition to all [Jijna magic you can do](https://jinja.palletsprojects.com/en/2.10.x/templates/), there is also some additional variables you can use in the templates.
In addition to all [Jinja magic you can do](https://jinja.palletsprojects.com/en/2.10.x/templates/), there is also some additional variables you can use in the templates.

Variable | Description
-- | --
`states` | This is the same as with the rest of Home Assistant.
`custom_components` | Gives you a list of information about your custom_integrations
`hacs_components` | Gives you a list of information about HACS installed integrations, plugins, and themes
`addons` | List of installed Home Assistant Add-ons

### custom_components

The information about custom integrations are fetched from the integrations manifest.json file, the folowing keys are available:

Expand All @@ -81,14 +81,6 @@ The information about custom integrations are fetched from the integrations mani
- `codeowners`
- `version`

The information about integrations tracked with HACS are fetched from the storage hacs files, the folowing keys are available:

- `category`
- `name`
- `documentation`
- `authors`
- `description`

**Example usage of the `custom_components` variable:**

```
Expand All @@ -101,6 +93,16 @@ _{{custom_component_descriptions[integration.domain]}}_
{% endfor -%}
```
### hacs_components
The information about integrations tracked with HACS are fetched from the storage hacs files, the folowing keys are available:
- `category`
- `name`
- `documentation`
- `authors`
- `description`
**Example usage of the `hacs_components` variable:**
```
Expand All @@ -120,6 +122,30 @@ _{{custom_component_descriptions[integration.domain]}}_
{%- endfor %}
```
### Add-ons
The following keys are available:
- `name`
- `slug`
- `description`
- `state`
- `version`
- `version_latest`
- `update_available`
- `repository`
**Example usage:**
```
## Add-ons
{%- for addon in addons | sort(attribute='name') %}
- {{addon.name}} ({{addon.version}}) - {{addon.description}}
{%- endfor %}
```
## Others
**Example usage for documenting Alexa smart home utterances**
```
{%- set alexa_configuration =
Expand Down Expand Up @@ -176,14 +202,6 @@ _"Alexa, turn off the `DEVICE NAME`."_
{%- endfor %}
```
If you only use this integration the output of that will be:
```
### [Generate readme](https://github.com/custom-components/readme)

_Generates this awesome readme file._
```
## Contributions are welcome!
If you want to contribute to this please read the [Contribution guidelines](CONTRIBUTING.md)
Expand Down
17 changes: 15 additions & 2 deletions custom_components/readme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import json
import os
from shutil import copyfile
from typing import Any, List
from typing import Any, Dict, List

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
import yaml
from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.template import AllStates
from homeassistant.loader import Integration, IntegrationNotFound, async_get_integration
from homeassistant.setup import async_get_loaded_integrations
Expand Down Expand Up @@ -152,11 +152,13 @@ async def service_generate(_call):

custom_components = await get_custom_integrations(hass)
hacs_components = get_hacs_components(hass)
installed_addons = get_ha_installed_addons(hass)

variables = {
"custom_components": custom_components,
"states": AllStates(hass),
"hacs_components": hacs_components,
"addons": installed_addons,
}

content = await read_file(hass, "templates/README.j2")
Expand Down Expand Up @@ -185,6 +187,17 @@ def get_hacs_components(hass: HomeAssistant):
]


@callback
def get_ha_installed_addons(hass: HomeAssistant) -> List[Dict[str, Any]]:
if not hass.components.hassio.is_hassio():
return []
supervisor_info = hass.components.hassio.get_supervisor_info()

if supervisor_info:
return supervisor_info.get("addons", [])
return []


def get_repository_name(repository) -> str:
"""Return the name of the repository for use in the frontend."""
name = None
Expand Down
34 changes: 23 additions & 11 deletions custom_components/readme/default.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{%- set custom_component_descriptions = {"readme": "Generates this awesome readme file."} -%}
# Welcome !

This is my Home Assistant installation.
Expand All @@ -11,16 +10,29 @@ Number of entities | {{states | count}}
Number of sensors | {{states.sensor | count}}


{% if custom_components %}
## The custom_components that I use
{% for integration in custom_components %}
### [{{integration.name}}]({{integration.documentation}})
{% if integration.domain in custom_component_descriptions %}
_{{custom_component_descriptions[integration.domain]}}_
{% endif -%}
{% endfor -%}
{% endif %}
## My installed extensions:

### Add-ons
{%- for addon in addons | sort(attribute='name') %}
- {{addon.name}}
{%- endfor %}

### Custom integrations
{%- for component in hacs_components | selectattr('category', 'equalto', 'integration') | sort(attribute='name') %}
- [{{component.name}}]({{component.documentation}})
{%- endfor %}

### Lovelace plugins
{%- for component in hacs_components | selectattr('category', 'equalto', 'plugin') | sort(attribute='name') %}
- [{{component.name}}]({{component.documentation}})
{%- endfor %}

### Themes
{%- for component in hacs_components | selectattr('category', 'equalto', 'theme') | sort(attribute='name') %}
- [{{component.name}}]({{component.documentation}})
{%- endfor %}


***

Generated by the [custom readme integration](https://github.com/custom-components/readme)
Generated by the [custom readme integration](https://github.com/custom-components/readme)

0 comments on commit 208ae25

Please sign in to comment.