Skip to content

Commit 208ae25

Browse files
l-jludeeus
andauthored
[NEW FEATURE] Add addons to available variables (#15)
* 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]>
1 parent 0f76464 commit 208ae25

File tree

3 files changed

+81
-38
lines changed

3 files changed

+81
-38
lines changed

README.md

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111
[![Discord][discord-shield]][discord]
1212
[![Community Forum][forum-shield]][forum]
1313

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

1617

1718
## Installation
1819

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

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

6663
## Usable variables
6764

68-
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.
65+
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.
6966

7067
Variable | Description
7168
-- | --
7269
`states` | This is the same as with the rest of Home Assistant.
7370
`custom_components` | Gives you a list of information about your custom_integrations
7471
`hacs_components` | Gives you a list of information about HACS installed integrations, plugins, and themes
72+
`addons` | List of installed Home Assistant Add-ons
73+
74+
### custom_components
7575

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

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

84-
The information about integrations tracked with HACS are fetched from the storage hacs files, the folowing keys are available:
85-
86-
- `category`
87-
- `name`
88-
- `documentation`
89-
- `authors`
90-
- `description`
91-
9284
**Example usage of the `custom_components` variable:**
9385

9486
```
@@ -101,6 +93,16 @@ _{{custom_component_descriptions[integration.domain]}}_
10193
{% endfor -%}
10294
```
10395
96+
### hacs_components
97+
98+
The information about integrations tracked with HACS are fetched from the storage hacs files, the folowing keys are available:
99+
100+
- `category`
101+
- `name`
102+
- `documentation`
103+
- `authors`
104+
- `description`
105+
104106
**Example usage of the `hacs_components` variable:**
105107
106108
```
@@ -120,6 +122,30 @@ _{{custom_component_descriptions[integration.domain]}}_
120122
{%- endfor %}
121123
```
122124
125+
### Add-ons
126+
127+
128+
The following keys are available:
129+
130+
- `name`
131+
- `slug`
132+
- `description`
133+
- `state`
134+
- `version`
135+
- `version_latest`
136+
- `update_available`
137+
- `repository`
138+
139+
**Example usage:**
140+
```
141+
## Add-ons
142+
{%- for addon in addons | sort(attribute='name') %}
143+
- {{addon.name}} ({{addon.version}}) - {{addon.description}}
144+
{%- endfor %}
145+
```
146+
147+
## Others
148+
123149
**Example usage for documenting Alexa smart home utterances**
124150
```
125151
{%- set alexa_configuration =
@@ -176,14 +202,6 @@ _"Alexa, turn off the `DEVICE NAME`."_
176202
{%- endfor %}
177203
```
178204
179-
If you only use this integration the output of that will be:
180-
181-
```
182-
### [Generate readme](https://github.com/custom-components/readme)
183-
184-
_Generates this awesome readme file._
185-
```
186-
187205
## Contributions are welcome!
188206
189207
If you want to contribute to this please read the [Contribution guidelines](CONTRIBUTING.md)

custom_components/readme/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import json
1111
import os
1212
from shutil import copyfile
13-
from typing import Any, List
13+
from typing import Any, Dict, List
1414

1515
import homeassistant.helpers.config_validation as cv
1616
import voluptuous as vol
1717
import yaml
1818
from homeassistant import config_entries
19-
from homeassistant.core import HomeAssistant
19+
from homeassistant.core import callback, HomeAssistant
2020
from homeassistant.helpers.template import AllStates
2121
from homeassistant.loader import Integration, IntegrationNotFound, async_get_integration
2222
from homeassistant.setup import async_get_loaded_integrations
@@ -152,11 +152,13 @@ async def service_generate(_call):
152152

153153
custom_components = await get_custom_integrations(hass)
154154
hacs_components = get_hacs_components(hass)
155+
installed_addons = get_ha_installed_addons(hass)
155156

156157
variables = {
157158
"custom_components": custom_components,
158159
"states": AllStates(hass),
159160
"hacs_components": hacs_components,
161+
"addons": installed_addons,
160162
}
161163

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

187189

190+
@callback
191+
def get_ha_installed_addons(hass: HomeAssistant) -> List[Dict[str, Any]]:
192+
if not hass.components.hassio.is_hassio():
193+
return []
194+
supervisor_info = hass.components.hassio.get_supervisor_info()
195+
196+
if supervisor_info:
197+
return supervisor_info.get("addons", [])
198+
return []
199+
200+
188201
def get_repository_name(repository) -> str:
189202
"""Return the name of the repository for use in the frontend."""
190203
name = None

custom_components/readme/default.j2

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{%- set custom_component_descriptions = {"readme": "Generates this awesome readme file."} -%}
21
# Welcome !
32

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

1312

14-
{% if custom_components %}
15-
## The custom_components that I use
16-
{% for integration in custom_components %}
17-
### [{{integration.name}}]({{integration.documentation}})
18-
{% if integration.domain in custom_component_descriptions %}
19-
_{{custom_component_descriptions[integration.domain]}}_
20-
{% endif -%}
21-
{% endfor -%}
22-
{% endif %}
13+
## My installed extensions:
14+
15+
### Add-ons
16+
{%- for addon in addons | sort(attribute='name') %}
17+
- {{addon.name}}
18+
{%- endfor %}
19+
20+
### Custom integrations
21+
{%- for component in hacs_components | selectattr('category', 'equalto', 'integration') | sort(attribute='name') %}
22+
- [{{component.name}}]({{component.documentation}})
23+
{%- endfor %}
24+
25+
### Lovelace plugins
26+
{%- for component in hacs_components | selectattr('category', 'equalto', 'plugin') | sort(attribute='name') %}
27+
- [{{component.name}}]({{component.documentation}})
28+
{%- endfor %}
29+
30+
### Themes
31+
{%- for component in hacs_components | selectattr('category', 'equalto', 'theme') | sort(attribute='name') %}
32+
- [{{component.name}}]({{component.documentation}})
33+
{%- endfor %}
34+
2335

2436
***
2537

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

0 commit comments

Comments
 (0)