Skip to content

Commit

Permalink
Add support for icon picker
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Nov 3, 2021
1 parent c3e47d2 commit 55dc083
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
33 changes: 33 additions & 0 deletions custom_components/fontawesome/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import logging

from homeassistant.components.frontend import add_extra_js_url
from homeassistant.components.http.view import HomeAssistantView

import json
from os import walk, path

LOGGER = logging.getLogger(__name__)

Expand All @@ -10,11 +14,28 @@
LOADER_URL = f'/{DOMAIN}/main.js'
LOADER_PATH = f'custom_components/{DOMAIN}/main.js'
ICONS_URL = f'/{DOMAIN}/icons'
ICONLIST_URL = f'/{DOMAIN}/list'
ICONS_PATH = f'custom_components/{DOMAIN}/data'
CUSTOM_ICONS_URL = f'/{DOMAIN}/icons/pro'
CUSTOM_ICONS_PATH = 'custom_icons/'


class ListingView(HomeAssistantView):

requires_auth = False

def __init__(self, url, iconpath):
self.url = url
self.iconpath = iconpath
self.name = "Icon Listing"

async def get(self, request):
icons = []
for (dirpath, dirnames, filenames) in walk(self.iconpath):
icons.extend([{"name": path.join(dirpath[len(self.iconpath):], fn[:-4])} for fn in filenames if fn.endswith(".svg")])
return json.dumps(icons)


async def async_setup(hass, config):
hass.http.register_static_path(
LOADER_URL,
Expand All @@ -29,11 +50,23 @@ async def async_setup(hass, config):
hass.config.path(ICONS_PATH + "/" + iset),
True
)
hass.http.register_view(
ListingView(
ICONLIST_URL + "/" + iset,
hass.config.path(ICONS_PATH + "/" + iset)
)
)
hass.http.register_static_path(
CUSTOM_ICONS_URL,
hass.config.path(CUSTOM_ICONS_PATH),
True
)
hass.http.register_view(
ListingView(
ICONLIST_URL + "/pro",
hass.config.path(CUSTOM_ICONS_PATH)
)
)

return True

Expand Down
2 changes: 1 addition & 1 deletion custom_components/fontawesome/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions custom_components/fontawesome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"domain": "fontawesome",
"name": "Fontawesome icons",
"documentation": "https://github.com/thomasloven/hass-fontawesome",
"dependencies": ["frontend"],
"dependencies": ["frontend", "http"],
"codeowners": [],
"requirements": [],
"config_flow": true,
"version": "2.1.2",
"version": "2.1.3",
"iot_class": "local_polling"
}
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "fontawesome",
"render_readme": true,
"homeassistant": "2021.5.0b0"
"homeassistant": "2021.11.0b0"
}
32 changes: 27 additions & 5 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,38 @@ const getIcon = (iconSet, iconName) => {
});
};

window.getIcon = getIcon;
const getIconList = async (iconSet) => {
const data = await fetch(`/${DOMAIN}/list/${iconSet}`);
const text = await data.text();
return JSON.parse(text);
};

// window.getIconList = getIconList;
// window.getIcon = getIcon;

if (!("customIconsets" in window)) {
window.customIconsets = {};
}
if (!("customIcons" in window)) {
window.customIcons = {};
}

window.customIconsets["fab"] = (iconName) => getIcon("brands", iconName);
window.customIconsets["far"] = (iconName) => getIcon("regular", iconName);
window.customIconsets["fas"] = (iconName) => getIcon("solid", iconName);
window.customIconsets["fapro"] = (iconName) => getIcon("pro", iconName);
window.customIcons["fab"] = {
getIcon: (iconName) => getIcon("brands", iconName),
getIconList: () => getIconList("brands"),
};
window.customIcons["far"] = {
getIcon: (iconName) => getIcon("regular", iconName),
getIconList: () => getIconList("regular"),
};
window.customIcons["fas"] = {
getIcon: (iconName) => getIcon("solid", iconName),
getIconList: () => getIconList("solid"),
};
window.customIcons["fapro"] = {
getIcon: (iconName) => getIcon("pro", iconName),
getIconList: () => getIconList("pro"),
};
window.customIconsets["facustom"] = (iconName) => getIcon("pro", iconName);

// Duotone patches
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fontawesome",
"private": true,
"version": "2.0.0",
"version": "2.1.3",
"description": "",
"scripts": {
"export": "./export.sh",
Expand Down

0 comments on commit 55dc083

Please sign in to comment.