Skip to content

Commit ccb3a68

Browse files
authored
HACS Data Refactor (#8)
1 parent b2f8069 commit ccb3a68

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

custom_components/readme/__init__.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ async def service_generate(call):
167167
custom_components = get_custom_components(hass)
168168
hacs_components = get_hacs_components(hass)
169169
variables = {
170-
"custom_components": custom_components,
171-
"states": AllStates(hass),
172-
"hacs_components": hacs_components
170+
"custom_components": custom_components,
171+
"states": AllStates(hass),
172+
"hacs_components": hacs_components,
173173
}
174174

175175
with open(f"{base}/templates/README.j2", "r") as readme:
@@ -185,50 +185,46 @@ async def service_generate(call):
185185

186186
hass.services.async_register(DOMAIN, "generate", service_generate)
187187

188+
188189
def get_hacs_components(hass):
189-
base = hass.config.path()
190-
keys = []
191-
hacs_components = []
190+
try:
191+
from custom_components.hacs.share import get_hacs
192+
except ImportError:
193+
return []
192194

193-
for file in os.listdir(f"{base}/.storage/hacs"):
194-
if file.endswith(".hacs"):
195-
keys.append(os.path.splitext(file)[0])
195+
hacs = get_hacs()
196+
repositories = hacs.repositories
197+
installed = []
196198

197-
if os.path.exists(f"{base}/.storage/hacs.repositories"):
198-
with open(f"{base}/.storage/hacs.repositories", "r") as repositories:
199-
content = repositories.read()
200-
content = json.loads(content)
201-
202-
for key in keys:
203-
repository = content["data"][key]
199+
for repo in repositories:
200+
if repo.data.installed:
201+
repo_json = repo.data.to_json()
204202

205-
hacs_components.append(
206-
{
207-
"category": repository["category"],
208-
"name": get_repository_name(repository),
209-
"description": repository["description"],
210-
"authors": repository["authors"],
211-
"documentation": "https://github.com/"+repository["full_name"]
212-
}
213-
)
203+
# Add additional properites to json
204+
repo_json["name"] = get_repository_name(repo)
205+
repo_json["documentation"] = "https://github.com/" + repo.data.full_name
206+
207+
installed.append(repo_json)
208+
209+
return installed
214210

215-
return hacs_components
216211

217212
def get_repository_name(repository) -> str:
218213
"""Return the name of the repository for use in the frontend."""
219214
name = None
220-
if repository["repository_manifest"]:
221-
name = repository["repository_manifest"]["name"]
215+
216+
if repository.repository_manifest.name:
217+
name = repository.repository_manifest.name
222218
else:
223-
name = repository["full_name"].split("/")[-1]
219+
name = repository.data.full_name.split("/")[-1]
220+
221+
name = name.replace("-", " ").replace("_", " ").strip()
224222

225223
if name.isupper():
226224
return name
227225

228-
return (name.replace("-", " ")
229-
.replace("_", " ")
230-
.replace("homeassistant", "")
231-
.title().strip())
226+
return name.title()
227+
232228

233229
def get_custom_components(hass):
234230
"""Return a list with custom_component info."""

0 commit comments

Comments
 (0)