Complete overhaul
This commit is contained in:
21
custom_components/fontawesome/.translations/en.json
Normal file
21
custom_components/fontawesome/.translations/en.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"config": {
|
||||
"title": "FontAwesome",
|
||||
"abort": {
|
||||
"single_instance_allowed": "Only a single configuration of FontAwesome is allowed."
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
"title": "Icon sets",
|
||||
"description": "Which icon sets to include",
|
||||
"data": {
|
||||
"regular": "Include Regular icons (far:)",
|
||||
"solid": "Include Solid icons (fas:)",
|
||||
"brands": "Include Brand icons (fab:)"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
46
custom_components/fontawesome/__init__.py
Normal file
46
custom_components/fontawesome/__init__.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import homeassistant.components.frontend
|
||||
from homeassistant.components.frontend import _frontend_root
|
||||
|
||||
from .custom_component_server import setup_view
|
||||
|
||||
DOMAIN = "fontawesome"
|
||||
|
||||
DATA_EXTRA_MODULE_URL = 'frontend_extra_module_url'
|
||||
ICONS_URL = '/'+DOMAIN+'/data/'
|
||||
ICON_FILES = {
|
||||
'regular': 'far.js',
|
||||
'solid': 'fas.js',
|
||||
'brands': 'fab.js',
|
||||
}
|
||||
|
||||
async def async_setup(hass, config):
|
||||
setup_view(hass, DOMAIN)
|
||||
conf = config.get(DOMAIN)
|
||||
if not conf:
|
||||
return True
|
||||
register_modules(hass, conf)
|
||||
return True
|
||||
|
||||
async def async_setup_entry(hass, config_entry):
|
||||
config_entry.add_update_listener(_update_listener)
|
||||
register_modules(hass, config_entry.options)
|
||||
return True
|
||||
|
||||
async def async_remove_entry(hass, config_entry):
|
||||
register_modules(hass, [])
|
||||
return True
|
||||
|
||||
async def _update_listener(hass, config_entry):
|
||||
register_modules(hass, config_entry.options)
|
||||
return True
|
||||
|
||||
|
||||
def register_modules(hass, modules):
|
||||
if DATA_EXTRA_MODULE_URL not in hass.data:
|
||||
hass.data[DATA_EXTRA_MODULE_URL] = set()
|
||||
url_set = hass.data[DATA_EXTRA_MODULE_URL]
|
||||
|
||||
for k,v in ICON_FILES.items():
|
||||
url_set.discard(ICONS_URL+v)
|
||||
if k in modules and modules[k] != False:
|
||||
url_set.add(ICONS_URL+v)
|
||||
47
custom_components/fontawesome/config_flow.py
Normal file
47
custom_components/fontawesome/config_flow.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import logging
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.core import callback
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@config_entries.HANDLERS.register("fontawesome")
|
||||
class FontawesomeConfigFlow(config_entries.ConfigFlow):
|
||||
async def async_step_user(self, user_input=None):
|
||||
if self._async_current_entries():
|
||||
return self.async_abort(reason="single_instance_allowed")
|
||||
return self.async_create_entry(title="", data={})
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(config_entry):
|
||||
return FontawesomeEditFlow(config_entry)
|
||||
|
||||
class FontawesomeEditFlow(config_entries.OptionsFlow):
|
||||
def __init__(self, config_entry):
|
||||
self.config_entry = config_entry
|
||||
|
||||
async def async_step_init(self, user_input=None):
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(title="", data=user_input)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Optional(
|
||||
"regular",
|
||||
default=self.config_entry.options.get("regular", False),
|
||||
): bool,
|
||||
vol.Optional(
|
||||
"solid",
|
||||
default=self.config_entry.options.get("solid", False),
|
||||
): bool,
|
||||
vol.Optional(
|
||||
"brands",
|
||||
default=self.config_entry.options.get("brands", False),
|
||||
): bool,
|
||||
}
|
||||
)
|
||||
)
|
||||
29
custom_components/fontawesome/custom_component_server.py
Normal file
29
custom_components/fontawesome/custom_component_server.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from aiohttp import web
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
import os.path
|
||||
|
||||
def setup_view(hass, name):
|
||||
hass.http.register_view(CustomComponentServer(hass, name))
|
||||
|
||||
class CustomComponentServer(HomeAssistantView):
|
||||
|
||||
requires_auth = False
|
||||
|
||||
def __init__(self, hass, domain):
|
||||
self.name = domain+"_server"
|
||||
self.url = '/'+domain+'/{filename:.*}'
|
||||
self.config_dir = hass.config.path()
|
||||
self.domain = domain
|
||||
|
||||
async def get(self, request, filename):
|
||||
path = os.path.join(self.config_dir, 'custom_components', self.domain, filename)
|
||||
filecontent = ""
|
||||
|
||||
try:
|
||||
with open(path, mode="r", encoding="utf-8", errors="ignore") as localfile:
|
||||
filecontent = localfile.read()
|
||||
localfile.close()
|
||||
except Exception as exception:
|
||||
return web.Response(status=404)
|
||||
|
||||
return web.Response(body=filecontent, content_type="text/javascript", charset="utf-8")
|
||||
1
custom_components/fontawesome/data/fab.js
Normal file
1
custom_components/fontawesome/data/fab.js
Normal file
File diff suppressed because one or more lines are too long
1
custom_components/fontawesome/data/far.js
Normal file
1
custom_components/fontawesome/data/far.js
Normal file
File diff suppressed because one or more lines are too long
1
custom_components/fontawesome/data/fas.js
Normal file
1
custom_components/fontawesome/data/fas.js
Normal file
File diff suppressed because one or more lines are too long
9
custom_components/fontawesome/manifest.json
Normal file
9
custom_components/fontawesome/manifest.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"domain": "fontawesome",
|
||||
"name": "Fontawesome icons",
|
||||
"documentation": "",
|
||||
"dependencies": ["frontend"],
|
||||
"codeowners": [],
|
||||
"requirements": [],
|
||||
"config_flow": true
|
||||
}
|
||||
Reference in New Issue
Block a user