Skip to content

Commit

Permalink
Adds theme support
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Jun 21, 2019
1 parent 7fdab92 commit 33c004c
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 7 deletions.
3 changes: 3 additions & 0 deletions custom_components/hacs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
vol.Required("token"): cv.string,
vol.Optional("appdaemon", default=False): cv.boolean,
vol.Optional("python_script", default=False): cv.boolean,
vol.Optional("theme", default=False): cv.boolean,
}
)
},
Expand All @@ -75,6 +76,8 @@ async def async_setup(hass, config): # pylint: disable=unused-argument
ELEMENT_TYPES.append("appdaemon")
if config[DOMAIN]["python_script"]:
ELEMENT_TYPES.append("python_script")
if config[DOMAIN]["theme"]:
ELEMENT_TYPES.append("theme")

# Print DEV warning
if VERSION == "DEV":
Expand Down
1 change: 1 addition & 0 deletions custom_components/hacs/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
from .hacsrepositoryintegration import HacsRepositoryIntegration
from .hacsrepositorybaseplugin import HacsRepositoryPlugin
from .hacsrepositorypythonscript import HacsRepositoryPythonScripts
from .hacsrepositorytheme import HacsRepositoryThemes
from .hacsviewbase import HacsViewBase
1 change: 1 addition & 0 deletions custom_components/hacs/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@
"CyrisXD/love-lock-card",
],
"python_script": [],
"theme": [],
}
19 changes: 13 additions & 6 deletions custom_components/hacs/hacsrepositorybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def custom(self):
return False
elif self.repository_name in DEFAULT_REPOSITORIES["python_script"]:
return False
elif self.repository_name in DEFAULT_REPOSITORIES["theme"]:
return False
return True

@property
Expand All @@ -95,6 +97,9 @@ def local_path(self):
elif self.repository_type == "python_script":
local_path = "{}/python_scripts".format(self.config_dir)

elif self.repository_type == "theme":
local_path = "{}/themes".format(self.config_dir)

return local_path

@property
Expand Down Expand Up @@ -182,10 +187,10 @@ async def download_repository_directory_content(
"""Download the content of a directory."""
try:
# Get content
if (
self.content_path == "release"
or self.repository_type == "python_script"
):
if self.content_path == "release" or self.repository_type in [
"python_script",
"theme",
]:
contents = self.content_objects
else:
contents = await self.repository.get_contents(
Expand Down Expand Up @@ -225,7 +230,7 @@ async def download_repository_directory_content(
"{}/".format(self.content_path), ""
)

if self.repository_type == "python_script":
if self.repository_type in ["python_script", "theme"]:
local_directory = self.local_path
else:
local_directory = "{}/{}".format(self.local_path, _content_path)
Expand Down Expand Up @@ -334,13 +339,15 @@ async def remove_local_directory(self):
try:
if self.repository_type == "python_script":
local_path = "{}/{}.py".format(self.local_path, self.name)
elif self.repository_type == "theme":
local_path = "{}/{}.yaml".format(self.local_path, self.name)
else:
local_path = self.local_path

if os.path.exists(local_path):
_LOGGER.debug("(%s) - Removing %s", self.repository_name, local_path)

if self.repository_type == "python_script":
if self.repository_type in ["python_script", "theme"]:
os.remove(local_path)
else:
shutil.rmtree(local_path)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hacs/hacsrepositorypythonscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HacsRepositoryPythonScripts(HacsRepositoryBase):
"""

def __init__(self, repository_name: str, repositoryobject=None):
"""Initialize a HacsRepositoryAppDaemon object."""
"""Initialize a HacsRepositoryPythonScripts object."""
super().__init__()
self.repository = repositoryobject
self.repository_name = repository_name
Expand Down
53 changes: 53 additions & 0 deletions custom_components/hacs/hacsrepositorytheme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Blueprint for HacsRepositoryThemes."""
# pylint: disable=too-many-instance-attributes,invalid-name,broad-except
import logging

from .blueprints import HacsRepositoryBase
from .exceptions import HacsRequirement

_LOGGER = logging.getLogger("custom_components.hacs.repository")


class HacsRepositoryThemes(HacsRepositoryBase):
"""
Set up a HacsRepositoryThemes object.
repository_name(str): The full name of a repository
(example: awesome-dev/awesome-repo)
"""

def __init__(self, repository_name: str, repositoryobject=None):
"""Initialize a HacsRepositoryThemes object."""
super().__init__()
self.repository = repositoryobject
self.repository_name = repository_name
self.repository_type = "theme"
self.manifest_content = None

async def update(self):
"""Run update tasks."""
if await self.common_update():
return
await self.set_repository_content()

async def set_repository_content(self):
"""Set repository content attributes."""
contentfiles = []

if self.content_path is None:
self.content_objects = await self.repository.get_contents(
"themes", self.ref
)

self.content_path = self.content_objects[0].path

self.name = self.content_objects[0].name.replace(".yaml", "")

if not isinstance(self.content_objects, list):
raise HacsRequirement("Repository structure does not meet the requirements")

for filename in self.content_objects:
contentfiles.append(filename.name)

if contentfiles:
self.content_files = contentfiles
1 change: 1 addition & 0 deletions docs/developer/python_script.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ python_scripts/awesome.py
info.md
README.md
```

#### Not OK example:

```text
Expand Down
40 changes: 40 additions & 0 deletions docs/developer/theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Theme developers

A template to use as a reference is [theme-hacs](https://github.com/ludeeus/theme-hacs)

This is for the [`frontend` integration in Home Assistant](https://www.home-assistant.io/components/frontend/)

## Requirements

For a theme repository to be valid these are the requirements:

### Repository structure

- The theme configuration file are located here `ROOT_OF_THE_REPO/themes/SCRIPT_NAME.yaml`
- There is only one theme configuration file (one directory under `ROOT_OF_THE_REPO/themes/`) per repository (if you have more, only the first one will be managed.)

#### OK example:

```text
themes/awesome.yaml
info.md
README.md
```

#### Not OK example:

```text
awesome.py
info.md
README.md
```

### GitHub releases (optional)

#### If there are releases

When installing/upgrading it will scan the content in the latest release.

#### If there are no releases

It will scan files in the branch marked as default.
13 changes: 13 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ The first file under the `python_scripts` directory._

The files are downloaded to `<config_dir>/python_scripts/*`

### Files downloaded for `theme`

The first file under the `themes` directory._

The files are downloaded to `<config_dir>/themes/*`

For this to work you need to include the themes directory like this:

```yaml
frontend:
themes: !include_dir_named themes
```
***
## How does it work: Upgrade
Expand Down
1 change: 1 addition & 0 deletions docs/installation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ key | optional | default | description
`token` | False | | A Github Personal Access Token
`appdaemon` | True | `False` | Enable tracking of AppDaemon apps.
`python_script` | True | `False` | Enable tracking of python scripts.
`theme` | True | `False` | Enable tracking of themes.

***

Expand Down
1 change: 1 addition & 0 deletions docs/usage/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Type | Description
`Integration` | Integrations for Home Assistant (custom_components)
`Plugin` | Pugins for Lovelace (cards, mods, elements, rows and so on.)
`Python_Script` | Python scripts for the [`python_script` integration](https://www.home-assistant.io/components/python_script/)
`Theme` | Themes for the [`frontend` integration](https://www.home-assistant.io/components/frontend/)

After adding a repository the repository will be scanned, if it can be tracked the element will show up under "STORE", and you will be redirected to that element.

Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ nav:
- AppDaemon developer: developer/appdaemon.md
- Integration developer: developer/integration.md
- Plugin developer: developer/plugin.md
- python_script developer: developer/python_script.md
- Theme developer: developer/theme.md
- Include default repositories: developer/include_default_repositories.md
- Issues: https://github.com/custom-components/hacs/issues

0 comments on commit 33c004c

Please sign in to comment.