diff --git a/.HA_VERSION b/.HA_VERSION new file mode 100644 index 0000000..8c53e4e --- /dev/null +++ b/.HA_VERSION @@ -0,0 +1 @@ +0.107.7 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5c8486c --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/.storage/ +/.vscode/ +/.cloud/ +*.code-workspace +*.js.gz +*.log +*.db diff --git a/configs/auth.yaml b/configs/auth.yaml new file mode 100644 index 0000000..54a490d --- /dev/null +++ b/configs/auth.yaml @@ -0,0 +1,7 @@ +- type: homeassistant +# - type: trusted_networks + # trusted_networks: + # - 10.0.0.3 + # - 127.0.0.1 + # - ::1 + # allow_bypass_login: true diff --git a/configs/automations.yaml b/configs/automations.yaml new file mode 100644 index 0000000..adf9ab4 --- /dev/null +++ b/configs/automations.yaml @@ -0,0 +1,8 @@ +- alias: Update Available Notification + trigger: + platform: state + entity_id: updater.updater + action: + service: notify.martin + data: + message: "Update for Home Assistant is available." diff --git a/configs/customize.yaml b/configs/customize.yaml new file mode 100644 index 0000000..52e266d --- /dev/null +++ b/configs/customize.yaml @@ -0,0 +1,2 @@ +fan.fan_state: + friendly_name: This is a dummy entry to be overwrrten diff --git a/configs/groups.yaml b/configs/groups.yaml new file mode 100644 index 0000000..e69de29 diff --git a/configs/http.yaml b/configs/http.yaml new file mode 100644 index 0000000..2a078a8 --- /dev/null +++ b/configs/http.yaml @@ -0,0 +1,11 @@ +# https://home-assistant.io/components/http/ +# ssl_certificate: !secret http_ssl_certificate +# ssl_key: !secret http_ssl_key +# base_url: !secret http_base_url +# ip_ban_enabled: False +# login_attempts_threshold: 5 +# base_url: https://ha238.kebler.net +#use_x_forwarded_for: true +# You must set the trusted proxy IP address so that Home Assistant will properly accept connections +# Set this to your Caddy machine IP, or localhost if hosted on the same machine. +#trusted_proxies: 127.0.0.1 diff --git a/configs/mqtt.yaml b/configs/mqtt.yaml new file mode 100644 index 0000000..8708d3c --- /dev/null +++ b/configs/mqtt.yaml @@ -0,0 +1,18 @@ +# https://www.home-assistant.io/components/mqtt/ + +# these four are now set in the mqtt integrations ui +# there is no way to edit these. You must remove and add the mqtt integration +#broker: 10.0.0.1 +#port: 1883 +#username: !secret MQTT_username +#password: !secret MQTT_password +birth_message: + topic: "status/ha" + payload: "online" + qos: 1 + retain: true +will_message: + topic: "status/ha" + payload: "offline" + qos: 1 + retain: true diff --git a/configs/panel.yaml b/configs/panel.yaml new file mode 100644 index 0000000..a132fd9 --- /dev/null +++ b/configs/panel.yaml @@ -0,0 +1,4 @@ +nodered: + title: Node-RED + icon: mdi:nodejs + url: https://ha.kebler.net/hassio/ingress/a0d7b954_nodered diff --git a/configs/scripts.yaml b/configs/scripts.yaml new file mode 100644 index 0000000..e69de29 diff --git a/configs/secrets.yaml b/configs/secrets.yaml new file mode 100644 index 0000000..d0e011b --- /dev/null +++ b/configs/secrets.yaml @@ -0,0 +1,4 @@ + +# Use this file to store secrets like usernames and passwords. +# Learn more at https://home-assistant.io/docs/configuration/secrets/ +some_password: welcome diff --git a/configuration.yaml b/configuration.yaml new file mode 100644 index 0000000..07825cd --- /dev/null +++ b/configuration.yaml @@ -0,0 +1,27 @@ +homeassistant: + # see packages/aa_system.yaml for basic settings + customize: !include configs/customize.yaml + auth_providers: !include configs/auth.yaml + packages: !include_dir_named packages + # discovery: +default_config: +# CONIG FILES +# group: !include configs/groups.yaml +automation: !include configs/automations.yaml +# script: !include configs/scripts.yaml +# http: !include configs/http.yaml +# mqtt: !include configs/mqtt.yaml +# panel_iframe: !include configs/panel.yaml +lovelace: + mode: yaml +# needed for any time of day related triggers +sensor: + - platform: time_date + display_options: + - 'time' + - 'date' + - 'date_time' + - 'date_time_iso' + - 'time_date' + - 'time_utc' + - 'beat' diff --git a/custom_components/hacs/.translations/en.json b/custom_components/hacs/.translations/en.json new file mode 100644 index 0000000..1e55666 --- /dev/null +++ b/custom_components/hacs/.translations/en.json @@ -0,0 +1,39 @@ +{ + "config": { + "abort": { + "single_instance_allowed": "Only a single configuration of HACS is allowed." + }, + "error": { + "auth": "Personal Access Token is not correct." + }, + "step": { + "user": { + "data": { + "token": "GitHub Personal Access Token", + "appdaemon": "Enable AppDaemon apps discovery & tracking", + "sidepanel_icon": "Side panel icon", + "sidepanel_title": "Side panel title" + }, + "description": "If you need help with the configuration have a look here: https:\/\/hacs.xyz\/docs\/configuration\/start", + "title": "HACS (Home Assistant Community Store)" + } + }, + "title": "HACS (Home Assistant Community Store)" + }, + "options": { + "step": { + "user": { + "data": { + "not_in_use": "Not in use with YAML", + "country": "Filter with country code.", + "experimental": "Enable experimental features", + "release_limit": "Number of releases to show.", + "debug": "Enable debug.", + "appdaemon": "Enable AppDaemon apps discovery & tracking", + "sidepanel_icon": "Side panel icon", + "sidepanel_title": "Side panel title" + } + } + } + } +} \ No newline at end of file diff --git a/custom_components/hacs/__init__.py b/custom_components/hacs/__init__.py new file mode 100644 index 0000000..c9cd91d --- /dev/null +++ b/custom_components/hacs/__init__.py @@ -0,0 +1,221 @@ +""" +Custom element manager for community created elements. + +For more details about this integration, please refer to the documentation at +https://hacs.xyz/ +""" + +import voluptuous as vol +from aiogithubapi import AIOGitHub +from homeassistant import config_entries +from homeassistant.const import EVENT_HOMEASSISTANT_START +from homeassistant.const import __version__ as HAVERSION +from homeassistant.components.lovelace import system_health_info +from homeassistant.exceptions import ConfigEntryNotReady, ServiceNotFound +from homeassistant.helpers.aiohttp_client import async_create_clientsession +from homeassistant.helpers.event import async_call_later + +from custom_components.hacs.configuration_schema import ( + hacs_base_config_schema, + hacs_config_option_schema, +) +from custom_components.hacs.const import DOMAIN, ELEMENT_TYPES, STARTUP, VERSION +from custom_components.hacs.constrains import check_constans, check_requirements +from custom_components.hacs.hacsbase.configuration import Configuration +from custom_components.hacs.hacsbase.data import HacsData +from custom_components.hacs.setup import ( + add_sensor, + load_hacs_repository, + setup_frontend, +) + +from custom_components.hacs.globals import get_hacs +from custom_components.hacs.helpers.network import internet_connectivity_check + +SCHEMA = hacs_base_config_schema() +SCHEMA[vol.Optional("options")] = hacs_config_option_schema() +CONFIG_SCHEMA = vol.Schema({DOMAIN: SCHEMA}, extra=vol.ALLOW_EXTRA) + + +async def async_setup(hass, config): + """Set up this integration using yaml.""" + hacs = get_hacs() + if DOMAIN not in config: + return True + hass.data[DOMAIN] = config + hacs.hass = hass + hacs.session = async_create_clientsession(hass) + hacs.configuration = Configuration.from_dict( + config[DOMAIN], config[DOMAIN].get("options") + ) + hacs.configuration.config = config + hacs.configuration.config_type = "yaml" + await startup_wrapper_for_yaml() + hass.async_create_task( + hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={} + ) + ) + return True + + +async def async_setup_entry(hass, config_entry): + """Set up this integration using UI.""" + hacs = get_hacs() + conf = hass.data.get(DOMAIN) + if config_entry.source == config_entries.SOURCE_IMPORT: + if conf is None: + hass.async_create_task( + hass.config_entries.async_remove(config_entry.entry_id) + ) + return False + hacs.hass = hass + hacs.session = async_create_clientsession(hass) + hacs.configuration = Configuration.from_dict( + config_entry.data, config_entry.options + ) + hacs.configuration.config_type = "flow" + hacs.configuration.config_entry = config_entry + config_entry.add_update_listener(reload_hacs) + startup_result = await hacs_startup() + if not startup_result: + hacs.system.disabled = True + raise ConfigEntryNotReady + hacs.system.disabled = False + return startup_result + + +async def startup_wrapper_for_yaml(): + """Startup wrapper for yaml config.""" + hacs = get_hacs() + startup_result = await hacs_startup() + if not startup_result: + hacs.system.disabled = True + hacs.hass.components.frontend.async_remove_panel( + hacs.configuration.sidepanel_title.lower() + .replace(" ", "_") + .replace("-", "_") + ) + hacs.logger.info("Could not setup HACS, trying again in 15 min") + async_call_later(hacs.hass, 900, startup_wrapper_for_yaml()) + return + hacs.system.disabled = False + + +async def hacs_startup(): + """HACS startup tasks.""" + hacs = get_hacs() + if not check_requirements(): + return False + if hacs.configuration.debug: + try: + await hacs.hass.services.async_call( + "logger", "set_level", {"hacs": "debug"} + ) + except ServiceNotFound: + hacs.logger.error( + "Could not set logging level to debug, logger is not enabled" + ) + + lovelace_info = await system_health_info(hacs.hass) + hacs.logger.debug(f"Configuration type: {hacs.configuration.config_type}") + hacs.version = VERSION + hacs.logger.info(STARTUP) + hacs.system.config_path = hacs.hass.config.path() + hacs.system.ha_version = HAVERSION + + hacs.system.lovelace_mode = lovelace_info.get("mode", "yaml") + hacs.system.disabled = False + hacs.github = AIOGitHub( + hacs.configuration.token, async_create_clientsession(hacs.hass) + ) + hacs.data = HacsData() + + # Check HACS Constrains + if not await hacs.hass.async_add_executor_job(check_constans): + if hacs.configuration.config_type == "flow": + if hacs.configuration.config_entry is not None: + await async_remove_entry(hacs.hass, hacs.configuration.config_entry) + return False + + # Set up frontend + await setup_frontend() + + if not await hacs.hass.async_add_executor_job(internet_connectivity_check): + hacs.logger.critical("No network connectivity") + return False + + # Load HACS + if not await load_hacs_repository(): + if hacs.configuration.config_type == "flow": + if hacs.configuration.config_entry is not None: + await async_remove_entry(hacs.hass, hacs.configuration.config_entry) + return False + + # Restore from storefiles + if not await hacs.data.restore(): + hacs_repo = hacs.get_by_name("hacs/integration") + hacs_repo.pending_restart = True + if hacs.configuration.config_type == "flow": + if hacs.configuration.config_entry is not None: + await async_remove_entry(hacs.hass, hacs.configuration.config_entry) + return False + + # Add aditional categories + hacs.common.categories = ELEMENT_TYPES + if hacs.configuration.appdaemon: + hacs.common.categories.append("appdaemon") + if hacs.configuration.python_script: + hacs.configuration.python_script = False + if hacs.configuration.config_type == "yaml": + hacs.logger.warning( + "Configuration option 'python_script' is deprecated and you should remove it from your configuration, HACS will know if you use 'python_script' in your Home Assistant configuration, this option will be removed in a future release." + ) + if hacs.configuration.theme: + hacs.configuration.theme = False + if hacs.configuration.config_type == "yaml": + hacs.logger.warning( + "Configuration option 'theme' is deprecated and you should remove it from your configuration, HACS will know if you use 'theme' in your Home Assistant configuration, this option will be removed in a future release." + ) + + # Setup startup tasks + if hacs.configuration.config_type == "yaml": + hacs.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, hacs.startup_tasks()) + else: + async_call_later(hacs.hass, 5, hacs.startup_tasks()) + + # Show the configuration + hacs.configuration.print() + + # Set up sensor + await hacs.hass.async_add_executor_job(add_sensor) + + # Mischief managed! + return True + + +async def async_remove_entry(hass, config_entry): + """Handle removal of an entry.""" + hacs = get_hacs() + hacs.logger.info("Disabling HACS") + hacs.logger.info("Removing recuring tasks") + for task in hacs.recuring_tasks: + task() + hacs.logger.info("Removing sensor") + try: + await hass.config_entries.async_forward_entry_unload(config_entry, "sensor") + except ValueError: + pass + hacs.logger.info("Removing sidepanel") + try: + hass.components.frontend.async_remove_panel("hacs") + except AttributeError: + pass + hacs.system.disabled = True + hacs.logger.info("HACS is now disabled") + + +async def reload_hacs(hass, config_entry): + """Reload HACS.""" + await async_remove_entry(hass, config_entry) + await async_setup_entry(hass, config_entry) diff --git a/custom_components/hacs/config_flow.py b/custom_components/hacs/config_flow.py new file mode 100644 index 0000000..2650782 --- /dev/null +++ b/custom_components/hacs/config_flow.py @@ -0,0 +1,102 @@ +"""Adds config flow for HACS.""" +# pylint: disable=dangerous-default-value +import logging +import voluptuous as vol +from aiogithubapi import AIOGitHubException, AIOGitHubAuthentication +from homeassistant import config_entries +from homeassistant.core import callback +from homeassistant.helpers import aiohttp_client + +from .const import DOMAIN +from .configuration_schema import hacs_base_config_schema, hacs_config_option_schema + +from custom_components.hacs.globals import get_hacs +from custom_components.hacs.helpers.information import get_repository + +_LOGGER = logging.getLogger(__name__) + + +class HacsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): + """Config flow for HACS.""" + + VERSION = 1 + CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL + + def __init__(self): + """Initialize.""" + self._errors = {} + + async def async_step_user(self, user_input={}): + """Handle a flow initialized by the user.""" + self._errors = {} + if self._async_current_entries(): + return self.async_abort(reason="single_instance_allowed") + if self.hass.data.get(DOMAIN): + return self.async_abort(reason="single_instance_allowed") + + if user_input is not None: + if await self._test_token(user_input["token"]): + return self.async_create_entry(title="", data=user_input) + + self._errors["base"] = "auth" + return await self._show_config_form(user_input) + + return await self._show_config_form(user_input) + + async def _show_config_form(self, user_input): + """Show the configuration form to edit location data.""" + return self.async_show_form( + step_id="user", + data_schema=vol.Schema(hacs_base_config_schema(user_input)), + errors=self._errors, + ) + + @staticmethod + @callback + def async_get_options_flow(config_entry): + return HacsOptionsFlowHandler(config_entry) + + async def async_step_import(self, user_input): + """Import a config entry. + Special type of import, we're not actually going to store any data. + Instead, we're going to rely on the values that are in config file. + """ + if self._async_current_entries(): + return self.async_abort(reason="single_instance_allowed") + + return self.async_create_entry(title="configuration.yaml", data={}) + + async def _test_token(self, token): + """Return true if token is valid.""" + try: + session = aiohttp_client.async_get_clientsession(self.hass) + await get_repository(session, token, "hacs/org") + return True + except (AIOGitHubException, AIOGitHubAuthentication) as exception: + _LOGGER.error(exception) + return False + + +class HacsOptionsFlowHandler(config_entries.OptionsFlow): + """HACS config flow options handler.""" + + def __init__(self, config_entry): + """Initialize HACS options flow.""" + self.config_entry = config_entry + + async def async_step_init(self, user_input=None): + """Manage the options.""" + return await self.async_step_user() + + async def async_step_user(self, user_input=None): + """Handle a flow initialized by the user.""" + hacs = get_hacs() + if user_input is not None: + return self.async_create_entry(title="", data=user_input) + + if hacs.configuration.config_type == "yaml": + schema = {vol.Optional("not_in_use", default=""): str} + else: + schema = hacs_config_option_schema(self.config_entry.options) + + return self.async_show_form(step_id="user", data_schema=vol.Schema(schema)) diff --git a/custom_components/hacs/configuration_schema.py b/custom_components/hacs/configuration_schema.py new file mode 100644 index 0000000..6b97709 --- /dev/null +++ b/custom_components/hacs/configuration_schema.py @@ -0,0 +1,62 @@ +"""HACS Configuration Schemas.""" +# pylint: disable=dangerous-default-value +import voluptuous as vol +from .const import LOCALE + +# Configuration: +TOKEN = "token" +SIDEPANEL_TITLE = "sidepanel_title" +SIDEPANEL_ICON = "sidepanel_icon" +APPDAEMON = "appdaemon" +PYTHON_SCRIPT = "python_script" +THEME = "theme" + +# Options: +COUNTRY = "country" +DEBUG = "debug" +RELEASE_LIMIT = "release_limit" +EXPERIMENTAL = "experimental" + + +def hacs_base_config_schema(config: dict = {}) -> dict: + """Return a shcema configuration dict for HACS.""" + if not config: + config = { + TOKEN: "xxxxxxxxxxxxxxxxxxxxxxxxxxx", + SIDEPANEL_ICON: "mdi:alpha-c-box", + SIDEPANEL_TITLE: "HACS", + APPDAEMON: False, + PYTHON_SCRIPT: False, + THEME: False, + } + return { + vol.Required(TOKEN, default=config.get(TOKEN)): str, + vol.Optional(SIDEPANEL_TITLE, default=config.get(SIDEPANEL_TITLE)): str, + vol.Optional(SIDEPANEL_ICON, default=config.get(SIDEPANEL_ICON)): str, + vol.Optional(APPDAEMON, default=config.get(APPDAEMON)): bool, + vol.Optional(PYTHON_SCRIPT, default=config.get(PYTHON_SCRIPT)): bool, + vol.Optional(THEME, default=config.get(THEME)): bool, + } + + +def hacs_config_option_schema(options: dict = {}) -> dict: + """Return a shcema for HACS configuration options.""" + if not options: + options = {COUNTRY: "ALL", DEBUG: False, RELEASE_LIMIT: 5, EXPERIMENTAL: False} + return { + vol.Optional(COUNTRY, default=options.get(COUNTRY)): vol.In(LOCALE), + vol.Optional(RELEASE_LIMIT, default=options.get(RELEASE_LIMIT)): int, + vol.Optional(EXPERIMENTAL, default=options.get(EXPERIMENTAL)): bool, + vol.Optional(DEBUG, default=options.get(DEBUG)): bool, + } + + +def hacs_config_combined() -> dict: + """Combine the configuration options.""" + base = hacs_base_config_schema() + options = hacs_config_option_schema() + + for option in options: + base[option] = options[option] + + return base diff --git a/custom_components/hacs/const.py b/custom_components/hacs/const.py new file mode 100644 index 0000000..b91f130 --- /dev/null +++ b/custom_components/hacs/const.py @@ -0,0 +1,293 @@ +"""Constants for HACS""" +NAME_LONG = "HACS (Home Assistant Community Store)" +NAME_SHORT = "HACS" +VERSION = "master" +DOMAIN = "hacs" +PROJECT_URL = "https://github.com/hacs/integration/" +CUSTOM_UPDATER_LOCATIONS = [ + "{}/custom_components/custom_updater.py", + "{}/custom_components/custom_updater/__init__.py", +] + +ISSUE_URL = f"{PROJECT_URL}issues" +DOMAIN_DATA = f"{NAME_SHORT.lower()}_data" + +ELEMENT_TYPES = ["integration", "plugin"] + +IFRAME = { + "title": "Community", + "icon": "mdi:alpha-c-box", + "url": "/community_overview", + "path": "community", + "require_admin": True, +} + + +# Messages +CUSTOM_UPDATER_WARNING = """ +This cannot be used with custom_updater. +To use this you need to remove custom_updater form {} +""" + +STARTUP = f""" +------------------------------------------------------------------- +HACS (Home Assistant Community Store) + +Version: {VERSION} +This is a custom integration +If you have any issues with this you need to open an issue here: +https://github.com/hacs/integration/issues +------------------------------------------------------------------- +""" + +LOCALE = [ + "ALL", + "AF", + "AL", + "DZ", + "AS", + "AD", + "AO", + "AI", + "AQ", + "AG", + "AR", + "AM", + "AW", + "AU", + "AT", + "AZ", + "BS", + "BH", + "BD", + "BB", + "BY", + "BE", + "BZ", + "BJ", + "BM", + "BT", + "BO", + "BQ", + "BA", + "BW", + "BV", + "BR", + "IO", + "BN", + "BG", + "BF", + "BI", + "KH", + "CM", + "CA", + "CV", + "KY", + "CF", + "TD", + "CL", + "CN", + "CX", + "CC", + "CO", + "KM", + "CG", + "CD", + "CK", + "CR", + "HR", + "CU", + "CW", + "CY", + "CZ", + "CI", + "DK", + "DJ", + "DM", + "DO", + "EC", + "EG", + "SV", + "GQ", + "ER", + "EE", + "ET", + "FK", + "FO", + "FJ", + "FI", + "FR", + "GF", + "PF", + "TF", + "GA", + "GM", + "GE", + "DE", + "GH", + "GI", + "GR", + "GL", + "GD", + "GP", + "GU", + "GT", + "GG", + "GN", + "GW", + "GY", + "HT", + "HM", + "VA", + "HN", + "HK", + "HU", + "IS", + "IN", + "ID", + "IR", + "IQ", + "IE", + "IM", + "IL", + "IT", + "JM", + "JP", + "JE", + "JO", + "KZ", + "KE", + "KI", + "KP", + "KR", + "KW", + "KG", + "LA", + "LV", + "LB", + "LS", + "LR", + "LY", + "LI", + "LT", + "LU", + "MO", + "MK", + "MG", + "MW", + "MY", + "MV", + "ML", + "MT", + "MH", + "MQ", + "MR", + "MU", + "YT", + "MX", + "FM", + "MD", + "MC", + "MN", + "ME", + "MS", + "MA", + "MZ", + "MM", + "NA", + "NR", + "NP", + "NL", + "NC", + "NZ", + "NI", + "NE", + "NG", + "NU", + "NF", + "MP", + "NO", + "OM", + "PK", + "PW", + "PS", + "PA", + "PG", + "PY", + "PE", + "PH", + "PN", + "PL", + "PT", + "PR", + "QA", + "RO", + "RU", + "RW", + "RE", + "BL", + "SH", + "KN", + "LC", + "MF", + "PM", + "VC", + "WS", + "SM", + "ST", + "SA", + "SN", + "RS", + "SC", + "SL", + "SG", + "SX", + "SK", + "SI", + "SB", + "SO", + "ZA", + "GS", + "SS", + "ES", + "LK", + "SD", + "SR", + "SJ", + "SZ", + "SE", + "CH", + "SY", + "TW", + "TJ", + "TZ", + "TH", + "TL", + "TG", + "TK", + "TO", + "TT", + "TN", + "TR", + "TM", + "TC", + "TV", + "UG", + "UA", + "AE", + "GB", + "US", + "UM", + "UY", + "UZ", + "VU", + "VE", + "VN", + "VG", + "VI", + "WF", + "EH", + "YE", + "ZM", + "ZW", +] diff --git a/custom_components/hacs/constrains.py b/custom_components/hacs/constrains.py new file mode 100644 index 0000000..269cba8 --- /dev/null +++ b/custom_components/hacs/constrains.py @@ -0,0 +1,99 @@ +"""HACS Startup constrains.""" +# pylint: disable=bad-continuation +import os + +from .const import CUSTOM_UPDATER_LOCATIONS, CUSTOM_UPDATER_WARNING +from .helpers.misc import version_left_higher_then_right + +from custom_components.hacs.globals import get_hacs + +MINIMUM_HA_VERSION = "0.98.0" + + +def check_constans(): + """Check HACS constrains.""" + if not constrain_translations(): + return False + if not constrain_custom_updater(): + return False + if not constrain_version(): + return False + return True + + +def constrain_custom_updater(): + """Check if custom_updater exist.""" + hacs = get_hacs() + for location in CUSTOM_UPDATER_LOCATIONS: + if os.path.exists(location.format(hacs.system.config_path)): + msg = CUSTOM_UPDATER_WARNING.format( + location.format(hacs.system.config_path) + ) + hacs.logger.critical(msg) + return False + return True + + +def constrain_version(): + """Check if the version is valid.""" + hacs = get_hacs() + if not version_left_higher_then_right(hacs.system.ha_version, MINIMUM_HA_VERSION): + hacs.logger.critical( + f"You need HA version {MINIMUM_HA_VERSION} or newer to use this integration." + ) + return False + return True + + +def constrain_translations(): + """Check if traslations exist.""" + hacs = get_hacs() + if not os.path.exists( + f"{hacs.system.config_path}/custom_components/hacs/.translations" + ): + hacs.logger.critical("You are missing the translations directory.") + return False + return True + + +def check_requirements(): + """Check the requirements""" + missing = [] + try: + from aiogithubapi import AIOGitHubException # pylint: disable=unused-import + except ImportError: + missing.append("aiogithubapi") + + try: + from hacs_frontend import locate_gz # pylint: disable=unused-import + except ImportError: + missing.append("hacs_frontend") + + try: + import semantic_version # pylint: disable=unused-import + except ImportError: + missing.append("semantic_version") + + try: + from integrationhelper import Logger # pylint: disable=unused-import + except ImportError: + missing.append("integrationhelper") + + try: + import backoff # pylint: disable=unused-import + except ImportError: + missing.append("backoff") + + try: + import aiofiles # pylint: disable=unused-import + except ImportError: + missing.append("aiofiles") + + if missing: + hacs = get_hacs() + for requirement in missing: + hacs.logger.critical( + f"Required python requirement '{requirement}' is not installed" + ) + return False + return True diff --git a/custom_components/hacs/globals.py b/custom_components/hacs/globals.py new file mode 100644 index 0000000..91d8314 --- /dev/null +++ b/custom_components/hacs/globals.py @@ -0,0 +1,29 @@ +# pylint: disable=invalid-name, missing-docstring +hacs = [] +removed_repositories = [] + + +def get_hacs(): + if not hacs: + from custom_components.hacs.hacsbase import Hacs + + hacs.append(Hacs()) + + return hacs[0] + + +def is_removed(repository): + return repository in [x.repository for x in removed_repositories] + + +def get_removed(repository): + if not is_removed(repository): + from custom_components.hacs.repositories.removed import RemovedRepository + + removed_repo = RemovedRepository() + removed_repo.repository = repository + removed_repositories.append(removed_repo) + filter_repos = [ + x for x in removed_repositories if x.repository.lower() == repository.lower() + ] + return filter_repos[0] diff --git a/custom_components/hacs/hacsbase/__init__.py b/custom_components/hacs/hacsbase/__init__.py new file mode 100644 index 0000000..8795661 --- /dev/null +++ b/custom_components/hacs/hacsbase/__init__.py @@ -0,0 +1,355 @@ +"""Initialize the HACS base.""" +# pylint: disable=unused-argument, bad-continuation +import json +import uuid +from datetime import timedelta + +from homeassistant.helpers.event import async_call_later, async_track_time_interval + +from aiogithubapi import AIOGitHubException, AIOGitHubRatelimit +from integrationhelper import Logger + +from custom_components.hacs.hacsbase.task_factory import HacsTaskFactory +from custom_components.hacs.hacsbase.exceptions import HacsException + +from custom_components.hacs.const import ELEMENT_TYPES +from custom_components.hacs.setup import setup_extra_stores +from custom_components.hacs.store import async_load_from_store, async_save_to_store +from custom_components.hacs.helpers.get_defaults import ( + get_default_repos_lists, + get_default_repos_orgs, +) + +from custom_components.hacs.helpers.register_repository import register_repository +from custom_components.hacs.globals import removed_repositories, get_removed, is_removed +from custom_components.hacs.repositories.removed import RemovedRepository + + +class HacsStatus: + """HacsStatus.""" + + startup = True + new = False + background_task = False + reloading_data = False + upgrading_all = False + + +class HacsFrontend: + """HacsFrontend.""" + + version_running = None + version_available = None + update_pending = False + + +class HacsCommon: + """Common for HACS.""" + + categories = [] + default = [] + installed = [] + skip = [] + + +class System: + """System info.""" + + status = HacsStatus() + config_path = None + ha_version = None + disabled = False + lovelace_mode = "storage" + + +class Developer: + """Developer settings/tools.""" + + template_id = "Repository ID" + template_content = "" + template_raw = "" + + @property + def devcontainer(self): + """Is it a devcontainer?""" + import os + + if "DEVCONTAINER" in os.environ: + return True + return False + + +class Hacs: + """The base class of HACS, nested thoughout the project.""" + + token = f"{str(uuid.uuid4())}-{str(uuid.uuid4())}" + hacsweb = f"/hacsweb/{token}" + hacsapi = f"/hacsapi/{token}" + repositories = [] + frontend = HacsFrontend() + repo = None + data_repo = None + developer = Developer() + data = None + configuration = None + logger = Logger("hacs") + github = None + hass = None + version = None + session = None + factory = HacsTaskFactory() + system = System() + recuring_tasks = [] + common = HacsCommon() + + @staticmethod + def init(hass, github_token): + """Return a initialized HACS object.""" + return Hacs() + + def get_by_id(self, repository_id): + """Get repository by ID.""" + try: + for repository in self.repositories: + if repository.information.uid == repository_id: + return repository + except Exception: # pylint: disable=broad-except + pass + return None + + def get_by_name(self, repository_full_name): + """Get repository by full_name.""" + try: + for repository in self.repositories: + if repository.data.full_name.lower() == repository_full_name.lower(): + return repository + except Exception: # pylint: disable=broad-except + pass + return None + + def is_known(self, repository_full_name): + """Return a bool if the repository is known.""" + return repository_full_name.lower() in [ + x.data.full_name.lower() for x in self.repositories + ] + + @property + def sorted_by_name(self): + """Return a sorted(by name) list of repository objects.""" + return sorted(self.repositories, key=lambda x: x.display_name) + + @property + def sorted_by_repository_name(self): + """Return a sorted(by repository_name) list of repository objects.""" + return sorted(self.repositories, key=lambda x: x.data.full_name) + + async def register_repository(self, full_name, category, check=True): + """Register a repository.""" + await register_repository(full_name, category, check=True) + + async def startup_tasks(self): + """Tasks tha are started after startup.""" + self.system.status.background_task = True + await self.hass.async_add_executor_job(setup_extra_stores) + self.hass.bus.async_fire("hacs/status", {}) + self.logger.debug(self.github.ratelimits.remaining) + self.logger.debug(self.github.ratelimits.reset_utc) + + await self.handle_critical_repositories_startup() + await self.handle_critical_repositories() + await self.load_known_repositories() + await self.clear_out_removed_repositories() + + self.recuring_tasks.append( + async_track_time_interval( + self.hass, self.recuring_tasks_installed, timedelta(minutes=30) + ) + ) + self.recuring_tasks.append( + async_track_time_interval( + self.hass, self.recuring_tasks_all, timedelta(minutes=800) + ) + ) + + self.hass.bus.async_fire("hacs/reload", {"force": True}) + await self.recuring_tasks_installed() + + self.system.status.startup = False + self.system.status.new = False + self.system.status.background_task = False + self.hass.bus.async_fire("hacs/status", {}) + await self.data.async_write() + + async def handle_critical_repositories_startup(self): + """Handled critical repositories during startup.""" + alert = False + critical = await async_load_from_store(self.hass, "critical") + if not critical: + return + for repo in critical: + if not repo["acknowledged"]: + alert = True + if alert: + self.logger.critical("URGENT!: Check the HACS panel!") + self.hass.components.persistent_notification.create( + title="URGENT!", message="**Check the HACS panel!**" + ) + + async def handle_critical_repositories(self): + """Handled critical repositories during runtime.""" + # Get critical repositories + instored = [] + critical = [] + was_installed = False + + try: + critical = await self.data_repo.get_contents("critical") + critical = json.loads(critical.content) + except AIOGitHubException: + pass + + if not critical: + self.logger.debug("No critical repositories") + return + + stored_critical = await async_load_from_store(self.hass, "critical") + + for stored in stored_critical or []: + instored.append(stored["repository"]) + + stored_critical = [] + + for repository in critical: + removed_repo = get_removed(repository["repository"]) + removed_repo.removal_type = "critical" + repo = self.get_by_name(repository["repository"]) + + stored = { + "repository": repository["repository"], + "reason": repository["reason"], + "link": repository["link"], + "acknowledged": True, + } + if repository["repository"] not in instored: + if repo is not None and repo.installed: + self.logger.critical( + f"Removing repository {repository['repository']}, it is marked as critical" + ) + was_installed = True + stored["acknowledged"] = False + # Uninstall from HACS + repo.remove() + await repo.uninstall() + stored_critical.append(stored) + removed_repo.update_data(stored) + + # Save to FS + await async_save_to_store(self.hass, "critical", stored_critical) + + # Resart HASS + if was_installed: + self.logger.critical("Resarting Home Assistant") + self.hass.async_create_task(self.hass.async_stop(100)) + + async def recuring_tasks_installed(self, notarealarg=None): + """Recuring tasks for installed repositories.""" + self.logger.debug( + "Starting recuring background task for installed repositories" + ) + self.system.status.background_task = True + self.hass.bus.async_fire("hacs/status", {}) + self.logger.debug(self.github.ratelimits.remaining) + self.logger.debug(self.github.ratelimits.reset_utc) + for repository in self.repositories: + if ( + repository.status.installed + and repository.data.category in self.common.categories + ): + self.factory.tasks.append(self.factory.safe_update(repository)) + + await self.factory.execute() + await self.handle_critical_repositories() + self.system.status.background_task = False + self.hass.bus.async_fire("hacs/status", {}) + await self.data.async_write() + self.logger.debug("Recuring background task for installed repositories done") + + async def recuring_tasks_all(self, notarealarg=None): + """Recuring tasks for all repositories.""" + self.logger.debug("Starting recuring background task for all repositories") + await self.hass.async_add_executor_job(setup_extra_stores) + self.system.status.background_task = True + self.hass.bus.async_fire("hacs/status", {}) + self.logger.debug(self.github.ratelimits.remaining) + self.logger.debug(self.github.ratelimits.reset_utc) + for repository in self.repositories: + if repository.data.category in self.common.categories: + self.factory.tasks.append(self.factory.safe_common_update(repository)) + + await self.factory.execute() + await self.load_known_repositories() + await self.clear_out_removed_repositories() + self.system.status.background_task = False + await self.data.async_write() + self.hass.bus.async_fire("hacs/status", {}) + self.hass.bus.async_fire("hacs/repository", {"action": "reload"}) + self.logger.debug("Recuring background task for all repositories done") + + async def clear_out_removed_repositories(self): + """Clear out blaclisted repositories.""" + need_to_save = False + for removed in removed_repositories: + if self.is_known(removed.repository): + repository = self.get_by_name(removed.repository) + if repository.status.installed and removed.removal_type != "critical": + self.logger.warning( + f"You have {repository.data.full_name} installed with HACS " + + f"this repository has been removed, please consider removing it. " + + f"Removal reason ({removed.removal_type})" + ) + else: + need_to_save = True + repository.remove() + + if need_to_save: + await self.data.async_write() + + async def get_repositories(self): + """Return a list of repositories.""" + repositories = {} + for category in self.common.categories: + repositories[category] = await get_default_repos_lists( + self.session, self.configuration.token, category + ) + org = await get_default_repos_orgs(self.github, category) + for repo in org: + repositories[category].append(repo) + + for category in repositories: + for repo in repositories[category]: + if repo not in self.common.default: + self.common.default.append(repo) + return repositories + + async def load_known_repositories(self): + """Load known repositories.""" + self.logger.info("Loading known repositories") + repositories = await self.get_repositories() + + for item in await get_default_repos_lists( + self.session, self.configuration.token, "removed" + ): + removed = get_removed(item["repository"]) + removed.reason = item.get("reason") + removed.link = item.get("link") + removed.removal_type = item.get("removal_type") + + for category in repositories: + for repo in repositories[category]: + if is_removed(repo): + continue + if self.is_known(repo): + continue + self.factory.tasks.append(self.factory.safe_register(repo, category)) + await self.factory.execute() + self.logger.info("Loading known repositories finished") diff --git a/custom_components/hacs/hacsbase/backup.py b/custom_components/hacs/hacsbase/backup.py new file mode 100644 index 0000000..2702dcc --- /dev/null +++ b/custom_components/hacs/hacsbase/backup.py @@ -0,0 +1,72 @@ +"""Backup.""" +import os +import shutil +import tempfile +from time import sleep + +from integrationhelper import Logger + +BACKUP_PATH = tempfile.gettempdir() + "/hacs_backup/" + + +class Backup: + """Backup.""" + + def __init__(self, local_path, backup_path=BACKUP_PATH): + """initialize.""" + self.logger = Logger("hacs.backup") + self.local_path = local_path + self.backup_path = backup_path + self.backup_path_full = f"{self.backup_path}{self.local_path.split('/')[-1]}" + + def create(self): + """Create a backup in /tmp""" + if not os.path.exists(self.local_path): + return + if os.path.exists(self.backup_path): + shutil.rmtree(self.backup_path) + while os.path.exists(self.backup_path): + sleep(0.1) + os.makedirs(self.backup_path, exist_ok=True) + + try: + if os.path.isfile(self.local_path): + shutil.copyfile(self.local_path, self.backup_path_full) + os.remove(self.local_path) + else: + shutil.copytree(self.local_path, self.backup_path_full) + shutil.rmtree(self.local_path) + while os.path.exists(self.local_path): + sleep(0.1) + self.logger.debug( + f"Backup for {self.local_path}, created in {self.backup_path_full}" + ) + except Exception: # pylint: disable=broad-except + pass + + def restore(self): + """Restore from backup.""" + if not os.path.exists(self.backup_path_full): + return + + if os.path.isfile(self.backup_path_full): + if os.path.exists(self.local_path): + os.remove(self.local_path) + shutil.copyfile(self.backup_path_full, self.local_path) + else: + if os.path.exists(self.local_path): + shutil.rmtree(self.local_path) + while os.path.exists(self.local_path): + sleep(0.1) + shutil.copytree(self.backup_path_full, self.local_path) + self.logger.debug( + f"Restored {self.local_path}, from backup {self.backup_path_full}" + ) + + def cleanup(self): + """Cleanup backup files.""" + if os.path.exists(self.backup_path): + shutil.rmtree(self.backup_path) + while os.path.exists(self.backup_path): + sleep(0.1) + self.logger.debug(f"Backup dir {self.backup_path} cleared") diff --git a/custom_components/hacs/hacsbase/configuration.py b/custom_components/hacs/hacsbase/configuration.py new file mode 100644 index 0000000..2e86984 --- /dev/null +++ b/custom_components/hacs/hacsbase/configuration.py @@ -0,0 +1,71 @@ +"""HACS Configuration.""" +import attr +from integrationhelper import Logger +from custom_components.hacs.hacsbase.exceptions import HacsException + + +@attr.s(auto_attribs=True) +class Configuration: + """Configuration class.""" + + # Main configuration: + appdaemon_path: str = "appdaemon/apps/" + appdaemon: bool = False + config: dict = {} + config_entry: dict = {} + config_type: str = None + debug: bool = False + dev: bool = False + frontend_mode: str = "Grid" + frontend_compact: bool = False + options: dict = {} + onboarding_done: bool = False + plugin_path: str = "www/community/" + python_script_path: str = "python_scripts/" + python_script: bool = False + sidepanel_icon: str = "mdi:alpha-c-box" + sidepanel_title: str = "Community" + theme_path: str = "themes/" + theme: bool = False + token: str = None + + # Config options: + country: str = "ALL" + experimental: bool = False + release_limit: int = 5 + + def to_json(self): + """Return a dict representation of the configuration.""" + return self.__dict__ + + def print(self): + """Print the current configuration to the log.""" + logger = Logger("hacs.configuration") + config = self.to_json() + for key in config: + if key in ["config", "config_entry", "options", "token"]: + continue + logger.debug(f"{key}: {config[key]}") + + @staticmethod + def from_dict(configuration: dict, options: dict): + """Set attributes from dicts.""" + if isinstance(options, bool) or isinstance(configuration.get("options"), bool): + raise HacsException("Configuration is not valid.") + + if options is None: + options = {} + + if not configuration: + raise HacsException("Configuration is not valid.") + + config = Configuration() + + config.config = configuration + config.options = options + + for conf_type in [configuration, options]: + for key in conf_type: + setattr(config, key, conf_type[key]) + + return config diff --git a/custom_components/hacs/hacsbase/const.py b/custom_components/hacs/hacsbase/const.py new file mode 100644 index 0000000..1e72154 --- /dev/null +++ b/custom_components/hacs/hacsbase/const.py @@ -0,0 +1,10 @@ +"""Constants for HACS""" +# pylint: disable=unused-import +STORAGE_VERSION = "6" +STORENAME = "hacs" + +# Messages +NOT_SUPPORTED_HA_VERSION = "You have version '{}' of Home Assistant, but version '{}' of '{}' require version '{}' of Home Assistant, install and upgrades are disabled for this integration untill you upgrade Home Assistant." + + +NO_ELEMENTS = "No elements to show, open the store to install some awesome stuff." diff --git a/custom_components/hacs/hacsbase/data.py b/custom_components/hacs/hacsbase/data.py new file mode 100644 index 0000000..1464849 --- /dev/null +++ b/custom_components/hacs/hacsbase/data.py @@ -0,0 +1,152 @@ +"""Data handler for HACS.""" +from integrationhelper import Logger +from ..const import VERSION +from ..repositories.repository import HacsRepository +from ..repositories.manifest import HacsManifest +from ..store import async_save_to_store, async_load_from_store + +from custom_components.hacs.globals import get_hacs, removed_repositories, get_removed +from custom_components.hacs.helpers.register_repository import register_repository + + +class HacsData: + """HacsData class.""" + + def __init__(self): + """Initialize.""" + self.logger = Logger("hacs.data") + self.hacs = get_hacs() + + async def async_write(self): + """Write content to the store files.""" + if self.hacs.system.status.background_task or self.hacs.system.disabled: + return + + self.logger.debug("Saving data") + + # Hacs + await async_save_to_store( + self.hacs.hass, + "hacs", + { + "view": self.hacs.configuration.frontend_mode, + "compact": self.hacs.configuration.frontend_compact, + "onboarding_done": self.hacs.configuration.onboarding_done, + }, + ) + + await async_save_to_store( + self.hacs.hass, "removed", [x.__dict__ for x in removed_repositories] + ) + + # Repositories + content = {} + for repository in self.hacs.repositories: + if repository.repository_manifest is not None: + repository_manifest = repository.repository_manifest.manifest + else: + repository_manifest = None + content[repository.information.uid] = { + "authors": repository.data.authors, + "category": repository.data.category, + "description": repository.data.description, + "downloads": repository.releases.downloads, + "full_name": repository.data.full_name, + "first_install": repository.status.first_install, + "hide": repository.status.hide, + "installed_commit": repository.versions.installed_commit, + "installed": repository.status.installed, + "last_commit": repository.versions.available_commit, + "last_release_tag": repository.versions.available, + "last_updated": repository.information.last_updated, + "name": repository.data.name, + "new": repository.status.new, + "repository_manifest": repository_manifest, + "selected_tag": repository.status.selected_tag, + "show_beta": repository.status.show_beta, + "stars": repository.data.stargazers_count, + "topics": repository.data.topics, + "version_installed": repository.versions.installed, + } + + await async_save_to_store(self.hacs.hass, "repositories", content) + self.hacs.hass.bus.async_fire("hacs/repository", {}) + self.hacs.hass.bus.fire("hacs/config", {}) + + async def restore(self): + """Restore saved data.""" + hacs = await async_load_from_store(self.hacs.hass, "hacs") + repositories = await async_load_from_store(self.hacs.hass, "repositories") + removed = await async_load_from_store(self.hacs.hass, "removed") + try: + if not hacs and not repositories: + # Assume new install + self.hacs.system.status.new = True + return True + self.logger.info("Restore started") + + # Hacs + self.hacs.configuration.frontend_mode = hacs.get("view", "Grid") + self.hacs.configuration.frontend_compact = hacs.get("compact", False) + self.hacs.configuration.onboarding_done = hacs.get("onboarding_done", False) + + for entry in removed: + removed_repo = get_removed(entry["repository"]) + removed_repo.update_data(entry) + + # Repositories + for entry in repositories: + repo = repositories[entry] + if not self.hacs.is_known(repo["full_name"]): + await register_repository( + repo["full_name"], repo["category"], False + ) + repository = self.hacs.get_by_name(repo["full_name"]) + if repository is None: + self.logger.error(f"Did not find {repo['full_name']}") + continue + + # Restore repository attributes + repository.information.uid = entry + await self.hacs.hass.async_add_executor_job( + restore_repository_data, repository, repo + ) + + self.logger.info("Restore done") + except Exception as exception: # pylint: disable=broad-except + self.logger.critical(f"[{exception}] Restore Failed!") + return False + return True + + +def restore_repository_data( + repository: type(HacsRepository), repository_data: dict +) -> None: + """Restore Repository Data""" + repository.data.authors = repository_data.get("authors", []) + repository.data.description = repository_data.get("description") + repository.releases.last_release_object_downloads = repository_data.get("downloads") + repository.information.last_updated = repository_data.get("last_updated") + repository.data.topics = repository_data.get("topics", []) + repository.data.stargazers_count = repository_data.get("stars", 0) + repository.releases.last_release = repository_data.get("last_release_tag") + repository.status.hide = repository_data.get("hide", False) + repository.status.installed = repository_data.get("installed", False) + repository.status.new = repository_data.get("new", True) + repository.status.selected_tag = repository_data.get("selected_tag") + repository.status.show_beta = repository_data.get("show_beta", False) + repository.versions.available = repository_data.get("last_release_tag") + repository.versions.available_commit = repository_data.get("last_commit") + repository.versions.installed = repository_data.get("version_installed") + repository.versions.installed_commit = repository_data.get("installed_commit") + + repository.repository_manifest = HacsManifest.from_dict( + repository_data.get("repository_manifest", {}) + ) + + if repository.status.installed: + repository.status.first_install = False + + if repository_data["full_name"] == "hacs/integration": + repository.versions.installed = VERSION + repository.status.installed = True diff --git a/custom_components/hacs/hacsbase/exceptions.py b/custom_components/hacs/hacsbase/exceptions.py new file mode 100644 index 0000000..62e5389 --- /dev/null +++ b/custom_components/hacs/hacsbase/exceptions.py @@ -0,0 +1,9 @@ +"""Custom Exceptions.""" + + +class HacsException(Exception): + """Super basic.""" + + +class HacsExpectedException(HacsException): + """For stuff that are expected.""" diff --git a/custom_components/hacs/hacsbase/task_factory.py b/custom_components/hacs/hacsbase/task_factory.py new file mode 100644 index 0000000..8b32aa9 --- /dev/null +++ b/custom_components/hacs/hacsbase/task_factory.py @@ -0,0 +1,75 @@ +# pylint: disable=missing-docstring,invalid-name +import logging +import time +from datetime import timedelta +import asyncio +from aiogithubapi import AIOGitHubException + +from custom_components.hacs.hacsbase.exceptions import HacsException +from custom_components.hacs.helpers.register_repository import register_repository + + +max_concurrent_tasks = asyncio.Semaphore(15) +sleeper = 5 + +logger = logging.getLogger("hacs.factory") + + +class HacsTaskFactory: + def __init__(self): + self.tasks = [] + self.running = False + + async def execute(self): + if not self.tasks: + logger.debug("No tasks to execute") + return + if self.running: + logger.debug("Allready executing tasks") + return + try: + self.running = True + logger.info("Processing %s tasks", len(self.tasks)) + start = time.time() + await asyncio.gather(*self.tasks) + logger.info( + "Task processing of %s tasks completed in %s seconds", + len(self.tasks), + timedelta(seconds=round(time.time() - start)).seconds, + ) + self.tasks = [] + self.running = False + except RuntimeError: + logger.warning("RuntimeError, Clearing current tasks") + self.tasks = [] + self.running = False + + async def safe_common_update(self, repository): + async with max_concurrent_tasks: + try: + await repository.common_update() + except (AIOGitHubException, HacsException) as exception: + logger.error("%s - %s", repository.data.full_name, exception) + + # Due to GitHub ratelimits we need to sleep a bit + await asyncio.sleep(sleeper) + + async def safe_update(self, repository): + async with max_concurrent_tasks: + try: + await repository.update_repository() + except (AIOGitHubException, HacsException) as exception: + logger.error("%s - %s", repository.data.full_name, exception) + + # Due to GitHub ratelimits we need to sleep a bit + await asyncio.sleep(sleeper) + + async def safe_register(self, repo, category): + async with max_concurrent_tasks: + try: + await register_repository(repo, category) + except (AIOGitHubException, HacsException) as exception: + logger.error("%s - %s", repo, exception) + + # Due to GitHub ratelimits we need to sleep a bit + await asyncio.sleep(sleeper) diff --git a/custom_components/hacs/handler/__init__.py b/custom_components/hacs/handler/__init__.py new file mode 100644 index 0000000..ab064c7 --- /dev/null +++ b/custom_components/hacs/handler/__init__.py @@ -0,0 +1 @@ +"""Initialize handlers.""" diff --git a/custom_components/hacs/handler/download.py b/custom_components/hacs/handler/download.py new file mode 100644 index 0000000..489c99d --- /dev/null +++ b/custom_components/hacs/handler/download.py @@ -0,0 +1,90 @@ +"""Download.""" +import os +import gzip +import shutil + +import aiofiles +import async_timeout +from integrationhelper import Logger +import backoff +from ..hacsbase.exceptions import HacsException + +from custom_components.hacs.globals import get_hacs + + +@backoff.on_exception(backoff.expo, Exception, max_tries=5) +async def async_download_file(url): + """ + Download files, and return the content. + """ + hacs = get_hacs() + logger = Logger("hacs.download.downloader") + if url is None: + return + + # There is a bug somewhere... TODO: Find that bug.... + if "tags/" in url: + url = url.replace("tags/", "") + + logger.debug(f"Downloading {url}") + + result = None + + with async_timeout.timeout(60, loop=hacs.hass.loop): + request = await hacs.session.get(url) + + # Make sure that we got a valid result + if request.status == 200: + result = await request.read() + else: + raise HacsException( + "Got status code {} when trying to download {}".format( + request.status, url + ) + ) + + return result + + +async def async_save_file(location, content): + """Save files.""" + logger = Logger("hacs.download.save") + logger.debug(f"Saving {location}") + mode = "w" + encoding = "utf-8" + errors = "ignore" + + if not isinstance(content, str): + mode = "wb" + encoding = None + errors = None + + try: + async with aiofiles.open( + location, mode=mode, encoding=encoding, errors=errors + ) as outfile: + await outfile.write(content) + outfile.close() + + # Create gz for .js files + if os.path.isfile(location): + if location.endswith(".js") or location.endswith(".css"): + with open(location, "rb") as f_in: + with gzip.open(location + ".gz", "wb") as f_out: + shutil.copyfileobj(f_in, f_out) + + # Remove with 2.0 + if "themes" in location and location.endswith(".yaml"): + filename = location.split("/")[-1] + base = location.split("/themes/")[0] + combined = f"{base}/themes/{filename}" + if os.path.exists(combined): + logger.info(f"Removing old theme file {combined}") + os.remove(combined) + + except Exception as error: # pylint: disable=broad-except + msg = "Could not write data to {} - {}".format(location, error) + logger.error(msg) + return False + + return os.path.exists(location) diff --git a/custom_components/hacs/handler/template.py b/custom_components/hacs/handler/template.py new file mode 100644 index 0000000..ca77e7a --- /dev/null +++ b/custom_components/hacs/handler/template.py @@ -0,0 +1,29 @@ +"""Custom template support.""" +# pylint: disable=broad-except +from jinja2 import Template +from integrationhelper import Logger + + +def render_template(content, context): + """Render templates in content.""" + # Fix None issues + if context.releases.last_release_object is not None: + prerelease = context.releases.last_release_object.prerelease + else: + prerelease = False + + # Render the template + try: + render = Template(content) + render = render.render( + installed=context.status.installed, + pending_update=context.pending_upgrade, + prerelease=prerelease, + selected_tag=context.status.selected_tag, + version_available=context.releases.last_release, + version_installed=context.display_installed_version, + ) + return render + except Exception as exception: + Logger("hacs.template").debug(exception) + return content diff --git a/custom_components/hacs/helpers/download.py b/custom_components/hacs/helpers/download.py new file mode 100644 index 0000000..63d59c1 --- /dev/null +++ b/custom_components/hacs/helpers/download.py @@ -0,0 +1,180 @@ +"""Helpers to download repository content.""" +import pathlib +import tempfile +import zipfile +from custom_components.hacs.hacsbase.exceptions import HacsException +from custom_components.hacs.handler.download import async_download_file, async_save_file +from custom_components.hacs.helpers.filters import filter_content_return_one_of_type + + +class FileInformation: + def __init__(self, url, path, name): + self.download_url = url + self.path = path + self.name = name + + +def should_try_releases(repository): + """Return a boolean indicating whether to download releases or not.""" + if repository.data.zip_release: + if repository.data.filename.endswith(".zip"): + if repository.ref != repository.data.default_branch: + return True + if repository.ref == repository.data.default_branch: + return False + if repository.data.category not in ["plugin", "theme"]: + return False + if not repository.releases.releases: + return False + return True + + +def gather_files_to_download(repository): + """Return a list of file objects to be downloaded.""" + files = [] + tree = repository.tree + ref = f"{repository.ref}".replace("tags/", "") + releaseobjects = repository.releases.objects + category = repository.data.category + remotelocation = repository.content.path.remote + + if should_try_releases(repository): + for release in releaseobjects or []: + if ref == release.tag_name: + for asset in release.assets or []: + files.append(asset) + if files: + return files + + if repository.content.single: + for treefile in tree: + if treefile.filename == repository.data.file_name: + files.append( + FileInformation( + treefile.download_url, treefile.full_path, treefile.filename + ) + ) + return files + + if category == "plugin": + for treefile in tree: + if treefile.path in ["", "dist"]: + if remotelocation == "dist" and not treefile.filename.startswith( + "dist" + ): + continue + if not remotelocation: + if not treefile.filename.endswith(".js"): + continue + if treefile.path != "": + continue + if not treefile.is_directory: + files.append( + FileInformation( + treefile.download_url, treefile.full_path, treefile.filename + ) + ) + if files: + return files + + if repository.data.content_in_root: + if not repository.data.filename: + if category == "theme": + tree = filter_content_return_one_of_type( + repository.tree, "", "yaml", "full_path" + ) + + for path in tree: + if path.is_directory: + continue + if path.full_path.startswith(repository.content.path.remote): + files.append( + FileInformation(path.download_url, path.full_path, path.filename) + ) + return files + + +async def download_zip(repository, validate): + """Download ZIP archive from repository release.""" + contents = [] + try: + for release in repository.releases.objects: + repository.logger.info( + f"ref: {repository.ref} --- tag: {release.tag_name}" + ) + if release.tag_name == repository.ref.split("/")[1]: + contents = release.assets + + if not contents: + return validate + + for content in contents: + filecontent = await async_download_file(content.download_url) + + if filecontent is None: + validate.errors.append(f"[{content.name}] was not downloaded.") + continue + + result = await async_save_file( + f"{tempfile.gettempdir()}/{repository.data.filename}", filecontent + ) + with zipfile.ZipFile( + f"{tempfile.gettempdir()}/{repository.data.filename}", "r" + ) as zip_file: + zip_file.extractall(repository.content.path.local) + + if result: + repository.logger.info(f"download of {content.name} complete") + continue + validate.errors.append(f"[{content.name}] was not downloaded.") + except Exception as exception: # pylint: disable=broad-except + validate.errors.append(f"Download was not complete [{exception}]") + + return validate + + +async def download_content(repository): + """Download the content of a directory.""" + contents = gather_files_to_download(repository) + if not contents: + raise HacsException("No content to download") + + for content in contents: + if repository.data.content_in_root and repository.data.filename is not None: + if content.name != repository.data.filename: + continue + repository.logger.debug(f"Downloading {content.name}") + + filecontent = await async_download_file(content.download_url) + + if filecontent is None: + repository.validate.errors.append(f"[{content.name}] was not downloaded.") + continue + + # Save the content of the file. + if repository.content.single or content.path is None: + local_directory = repository.content.path.local + + else: + _content_path = content.path + if not repository.data.content_in_root: + _content_path = _content_path.replace( + f"{repository.content.path.remote}", "" + ) + + local_directory = f"{repository.content.path.local}/{_content_path}" + local_directory = local_directory.split("/") + del local_directory[-1] + local_directory = "/".join(local_directory) + + # Check local directory + pathlib.Path(local_directory).mkdir(parents=True, exist_ok=True) + + local_file_path = (f"{local_directory}/{content.name}").replace("//", "/") + + result = await async_save_file(local_file_path, filecontent) + if result: + repository.logger.info(f"download of {content.name} complete") + continue + repository.validate.errors.append(f"[{content.name}] was not downloaded.") + diff --git a/custom_components/hacs/helpers/filters.py b/custom_components/hacs/helpers/filters.py new file mode 100644 index 0000000..4f98491 --- /dev/null +++ b/custom_components/hacs/helpers/filters.py @@ -0,0 +1,55 @@ +"""Filter functions.""" + + +def filter_content_return_one_of_type( + content, namestartswith, filterfiltype, attr="name" +): + """Only match 1 of the filter.""" + contents = [] + filetypefound = False + for filename in content: + if isinstance(filename, str): + if filename.startswith(namestartswith): + if filename.endswith(f".{filterfiltype}"): + if not filetypefound: + contents.append(filename) + filetypefound = True + continue + else: + contents.append(filename) + else: + if getattr(filename, attr).startswith(namestartswith): + if getattr(filename, attr).endswith(f".{filterfiltype}"): + if not filetypefound: + contents.append(filename) + filetypefound = True + continue + else: + contents.append(filename) + return contents + + +def find_first_of_filetype(content, filterfiltype, attr="name"): + """Find the first of the file type.""" + filename = "" + for _filename in content: + if isinstance(_filename, str): + if _filename.endswith(f".{filterfiltype}"): + filename = _filename + break + else: + if getattr(_filename, attr).endswith(f".{filterfiltype}"): + filename = getattr(_filename, attr) + break + return filename + + +def get_first_directory_in_directory(content, dirname): + """Return the first directory in dirname or None.""" + directory = None + for path in content: + if path.full_path.startswith(dirname) and path.full_path != dirname: + if path.is_directory: + directory = path.filename + break + return directory diff --git a/custom_components/hacs/helpers/get_defaults.py b/custom_components/hacs/helpers/get_defaults.py new file mode 100644 index 0000000..744540d --- /dev/null +++ b/custom_components/hacs/helpers/get_defaults.py @@ -0,0 +1,44 @@ +"""Helpers to get default repositories.""" +import json +from aiogithubapi import AIOGitHub, AIOGitHubException +from integrationhelper import Logger +from custom_components.hacs.helpers.information import get_repository + + +async def get_default_repos_orgs(github: type(AIOGitHub), category: str) -> dict: + """Gets default org repositories.""" + repositories = [] + logger = Logger("hacs") + orgs = { + "plugin": "custom-cards", + "integration": "custom-components", + "theme": "home-assistant-community-themes", + } + if category not in orgs: + return repositories + + try: + repos = await github.get_org_repos(orgs[category]) + for repo in repos: + repositories.append(repo.full_name) + + except AIOGitHubException as exception: + logger.error(exception) + + return repositories + + +async def get_default_repos_lists(session, token, default: str) -> dict: + """Gets repositories from default list.""" + repositories = [] + logger = Logger("hacs") + + try: + repo = await get_repository(session, token, "hacs/default") + content = await repo.get_contents(default) + repositories = json.loads(content.content) + + except AIOGitHubException as exception: + logger.error(exception) + + return repositories diff --git a/custom_components/hacs/helpers/information.py b/custom_components/hacs/helpers/information.py new file mode 100644 index 0000000..2107d66 --- /dev/null +++ b/custom_components/hacs/helpers/information.py @@ -0,0 +1,181 @@ +"""Return repository information if any.""" +import json +from aiogithubapi import AIOGitHubException, AIOGitHub +from custom_components.hacs.handler.template import render_template +from custom_components.hacs.hacsbase.exceptions import HacsException + + +def info_file(repository): + """get info filename.""" + if repository.data.render_readme: + for filename in ["readme", "readme.md", "README", "README.md", "README.MD"]: + if filename in repository.treefiles: + return filename + return "" + for filename in ["info", "info.md", "INFO", "INFO.md", "INFO.MD"]: + if filename in repository.treefiles: + return filename + return "" + + +async def get_info_md_content(repository): + """Get the content of info.md""" + filename = info_file(repository) + if not filename: + return "" + try: + info = await repository.repository_object.get_contents(filename, repository.ref) + if info is None: + return "" + info = info.content.replace(" str: + """Return the name of the repository for use in the frontend.""" + + if repository.repository_manifest.name is not None: + return repository.repository_manifest.name + + if repository.data.category == "integration": + if repository.integration_manifest: + if "name" in repository.integration_manifest: + return repository.integration_manifest["name"] + + return ( + repository.data.full_name.split("/")[-1] + .replace("-", " ") + .replace("_", " ") + .title() + ) + + +def version_left_higher_then_right(new: str, old: str) -> bool: + """Return a bool if source is newer than target, will also be true if identical.""" + if not isinstance(new, str) or not isinstance(old, str): + return False + if new == old: + return True + return semantic_version.Version.coerce(new) > semantic_version.Version.coerce(old) diff --git a/custom_components/hacs/helpers/network.py b/custom_components/hacs/helpers/network.py new file mode 100644 index 0000000..f15fc5d --- /dev/null +++ b/custom_components/hacs/helpers/network.py @@ -0,0 +1,20 @@ +"""Verify network.""" +import socket + + +def internet_connectivity_check(): + """Verify network connectivity.""" + hosts = [{"host": "github.com", "port": 443, "connection": False}] + + for host in hosts: + try: + socket.setdefaulttimeout(3) + socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect( + (host["host"], host["port"]) + ) + + host["connection"] = True + except Exception: # pylint: disable=broad-except + host["connection"] = False + + return False not in [x["connection"] for x in hosts] diff --git a/custom_components/hacs/helpers/register_repository.py b/custom_components/hacs/helpers/register_repository.py new file mode 100644 index 0000000..d7c556f --- /dev/null +++ b/custom_components/hacs/helpers/register_repository.py @@ -0,0 +1,49 @@ +"""Register a repository.""" +from aiogithubapi import AIOGitHubException +from custom_components.hacs.globals import get_hacs +from custom_components.hacs.hacsbase.exceptions import ( + HacsException, + HacsExpectedException, +) + + +async def register_repository(full_name, category, check=True): + """Register a repository.""" + hacs = get_hacs() + from custom_components.hacs.repositories import ( + RERPOSITORY_CLASSES, + ) # To hanle import error + + if full_name in hacs.common.skip: + if full_name != "hacs/integration": + raise HacsExpectedException(f"Skipping {full_name}") + + if category not in RERPOSITORY_CLASSES: + raise HacsException(f"{category} is not a valid repository category.") + + repository = RERPOSITORY_CLASSES[category](full_name) + if check: + try: + await repository.registration() + if hacs.system.status.new: + repository.status.new = False + if repository.validate.errors: + hacs.common.skip.append(repository.data.full_name) + if not hacs.system.status.startup: + hacs.logger.error(f"Validation for {full_name} failed.") + return repository.validate.errors + repository.logger.info("Registration complete") + except AIOGitHubException as exception: + hacs.common.skip.append(repository.data.full_name) + raise HacsException(f"Validation for {full_name} failed with {exception}.") + + hacs.hass.bus.async_fire( + "hacs/repository", + { + "id": 1337, + "action": "registration", + "repository": repository.data.full_name, + "repository_id": repository.information.uid, + }, + ) + hacs.repositories.append(repository) diff --git a/custom_components/hacs/helpers/validate_repository.py b/custom_components/hacs/helpers/validate_repository.py new file mode 100644 index 0000000..f20adbf --- /dev/null +++ b/custom_components/hacs/helpers/validate_repository.py @@ -0,0 +1,90 @@ +"""Helper to do common validation for repositories.""" +from aiogithubapi import AIOGitHubException +from custom_components.hacs.globals import get_hacs, is_removed +from custom_components.hacs.hacsbase.exceptions import HacsException +from custom_components.hacs.helpers.install import version_to_install +from custom_components.hacs.helpers.information import ( + get_repository, + get_tree, + get_releases, +) + + +async def common_validate(repository): + """Common validation steps of the repository.""" + repository.validate.errors = [] + + # Make sure the repository exist. + repository.logger.debug("Checking repository.") + await common_update_data(repository) + + # Step 6: Get the content of hacs.json + await repository.get_repository_manifest_content() + + +async def common_update_data(repository): + """Common update data.""" + hacs = get_hacs() + try: + repository_object = await get_repository( + hacs.session, hacs.configuration.token, repository.data.full_name + ) + repository.repository_object = repository_object + repository.data.update_data(repository_object.attributes) + except (AIOGitHubException, HacsException) as exception: + if not hacs.system.status.startup: + repository.logger.error(exception) + repository.validate.errors.append("Repository does not exist.") + raise HacsException(exception) + + # Make sure the repository is not archived. + if repository.data.archived: + repository.validate.errors.append("Repository is archived.") + raise HacsException("Repository is archived.") + + # Make sure the repository is not in the blacklist. + if is_removed(repository.data.full_name): + repository.validate.errors.append("Repository is in the blacklist.") + raise HacsException("Repository is in the blacklist.") + + # Get releases. + try: + releases = await get_releases( + repository.repository_object, + repository.status.show_beta, + hacs.configuration.release_limit, + ) + if releases: + repository.releases.releases = True + repository.releases.objects = releases + repository.releases.published_tags = [ + x.tag_name for x in releases if not x.draft + ] + repository.versions.available = next(iter(releases)).tag_name + for release in releases: + if release.tag_name == repository.ref: + assets = release.assets + if assets: + downloads = next(iter(assets)).attributes.get("download_count") + repository.releases.downloads = downloads + + except (AIOGitHubException, HacsException): + repository.releases.releases = False + + repository.ref = version_to_install(repository) + + repository.logger.debug( + f"Running checks against {repository.ref.replace('tags/', '')}" + ) + + try: + repository.tree = await get_tree(repository.repository_object, repository.ref) + if not repository.tree: + raise HacsException("No files in tree") + repository.treefiles = [] + for treefile in repository.tree: + repository.treefiles.append(treefile.full_path) + except (AIOGitHubException, HacsException) as exception: + if not hacs.system.status.startup: + repository.logger.error(exception) + raise HacsException(exception) diff --git a/custom_components/hacs/http.py b/custom_components/hacs/http.py new file mode 100644 index 0000000..cab3fb1 --- /dev/null +++ b/custom_components/hacs/http.py @@ -0,0 +1,83 @@ +"""HACS http endpoints.""" +import os +from integrationhelper import Logger +from homeassistant.components.http import HomeAssistantView +from aiohttp import web +from hacs_frontend import locate_gz, locate_debug_gz + +from custom_components.hacs.globals import get_hacs + + +class HacsFrontend(HomeAssistantView): + """Base View Class for HACS.""" + + requires_auth = False + name = "hacs_files" + url = r"/hacsfiles/{requested_file:.+}" + + async def get(self, request, requested_file): # pylint: disable=unused-argument + """Handle HACS Web requests.""" + return await get_file_response(requested_file) + + +class HacsPluginViewLegacy(HacsFrontend): + """Alias for legacy, remove with 1.0""" + + name = "community_plugin" + url = r"/community_plugin/{requested_file:.+}" + + async def get(self, request, requested_file): # pylint: disable=unused-argument + """DEPRECATED.""" + hacs = get_hacs() + if hacs.system.ha_version.split(".")[1] >= "107": + logger = Logger("hacs.deprecated") + logger.warning( + "The '/community_plugin/*' is deprecated and will be removed in an upcomming version of HACS, it has been replaced by '/hacsfiles/*', if you use the UI to manage your lovelace configuration, you can update this by going to the settings tab in HACS, if you use YAML to manage your lovelace configuration, you manually need to replace the URL in your resources." + ) + + return await get_file_response(requested_file) + + +async def get_file_response(requested_file): + """Get file.""" + hacs = get_hacs() + + if requested_file.startswith("frontend-"): + if hacs.configuration.debug: + servefile = await hacs.hass.async_add_executor_job(locate_debug_gz) + hacs.logger.debug("Serving DEBUG frontend") + else: + servefile = await hacs.hass.async_add_executor_job(locate_gz) + + if os.path.exists(servefile): + return web.FileResponse(servefile) + elif requested_file == "iconset.js": + return web.FileResponse( + f"{hacs.system.config_path}/custom_components/hacs/iconset.js" + ) + + try: + if requested_file.startswith("themes"): + file = f"{hacs.system.config_path}/{requested_file}" + else: + file = f"{hacs.system.config_path}/www/community/{requested_file}" + + # Serve .gz if it exist + if os.path.exists(file + ".gz"): + file += ".gz" + + if os.path.exists(file): + hacs.logger.debug("Serving {} from {}".format(requested_file, file)) + response = web.FileResponse(file) + response.headers["Cache-Control"] = "no-store, max-age=0" + response.headers["Pragma"] = "no-store" + return response + else: + hacs.logger.error(f"Tried to serve up '{file}' but it does not exist") + + except Exception as error: # pylint: disable=broad-except + hacs.logger.debug( + "there was an issue trying to serve {} - {}".format(requested_file, error) + ) + + return web.Response(status=404) diff --git a/custom_components/hacs/iconset.js b/custom_components/hacs/iconset.js new file mode 100644 index 0000000..ab225c7 --- /dev/null +++ b/custom_components/hacs/iconset.js @@ -0,0 +1,34 @@ +const iconset = document.createElement("ha-iconset-svg"); +iconset.name = "hacs"; +iconset.size = "1024"; +iconset.innerHTML = ` + + + + + + + + + + + + + + + +` +document.body.appendChild(iconset); \ No newline at end of file diff --git a/custom_components/hacs/manifest.json b/custom_components/hacs/manifest.json new file mode 100644 index 0000000..652a743 --- /dev/null +++ b/custom_components/hacs/manifest.json @@ -0,0 +1,24 @@ +{ + "codeowners": [ + "@ludeeus" + ], + "config_flow": true, + "dependencies": [ + "websocket_api", + "frontend", + "persistent_notification", + "lovelace" + ], + "documentation": "https://hacs.xyz", + "domain": "hacs", + "issues": "https://hacs.xyz/docs/issues", + "name": "HACS (Home Assistant Community Store)", + "requirements": [ + "aiofiles==0.4.0", + "aiogithubapi==0.5.0", + "backoff==1.10.0", + "hacs_frontend==20200229193422", + "integrationhelper==0.2.2", + "semantic_version==2.8.4" + ] +} \ No newline at end of file diff --git a/custom_components/hacs/repositories/__init__.py b/custom_components/hacs/repositories/__init__.py new file mode 100644 index 0000000..e992d78 --- /dev/null +++ b/custom_components/hacs/repositories/__init__.py @@ -0,0 +1,14 @@ +"""Initialize repositories.""" +from custom_components.hacs.repositories.theme import HacsTheme +from custom_components.hacs.repositories.integration import HacsIntegration +from custom_components.hacs.repositories.python_script import HacsPythonScript +from custom_components.hacs.repositories.appdaemon import HacsAppdaemon +from custom_components.hacs.repositories.plugin import HacsPlugin + +RERPOSITORY_CLASSES = { + "theme": HacsTheme, + "integration": HacsIntegration, + "python_script": HacsPythonScript, + "appdaemon": HacsAppdaemon, + "plugin": HacsPlugin, +} diff --git a/custom_components/hacs/repositories/appdaemon.py b/custom_components/hacs/repositories/appdaemon.py new file mode 100644 index 0000000..92d19e4 --- /dev/null +++ b/custom_components/hacs/repositories/appdaemon.py @@ -0,0 +1,85 @@ +"""Class for appdaemon apps in HACS.""" +from aiogithubapi import AIOGitHubException +from integrationhelper import Logger + +from .repository import HacsRepository +from ..hacsbase.exceptions import HacsException + + +class HacsAppdaemon(HacsRepository): + """Appdaemon apps in HACS.""" + + def __init__(self, full_name): + """Initialize.""" + super().__init__() + self.data.full_name = full_name + self.data.category = "appdaemon" + self.content.path.local = self.localpath + self.content.path.remote = "apps" + self.logger = Logger(f"hacs.repository.{self.data.category}.{full_name}") + + @property + def localpath(self): + """Return localpath.""" + return f"{self.hacs.system.config_path}/appdaemon/apps/{self.data.name}" + + async def validate_repository(self): + """Validate.""" + await self.common_validate() + + # Custom step 1: Validate content. + try: + addir = await self.repository_object.get_contents("apps", self.ref) + except AIOGitHubException: + raise HacsException( + f"Repostitory structure for {self.ref.replace('tags/','')} is not compliant" + ) + + if not isinstance(addir, list): + self.validate.errors.append("Repostitory structure not compliant") + + self.content.path.remote = addir[0].path + self.content.objects = await self.repository_object.get_contents( + self.content.path.remote, self.ref + ) + + # Handle potential errors + if self.validate.errors: + for error in self.validate.errors: + if not self.hacs.system.status.startup: + self.logger.error(error) + return self.validate.success + + async def registration(self): + """Registration.""" + if not await self.validate_repository(): + return False + + # Run common registration steps. + await self.common_registration() + + # Set local path + self.content.path.local = self.localpath + + async def update_repository(self): + """Update.""" + if self.hacs.github.ratelimits.remaining == 0: + return + await self.common_update() + + # Get appdaemon objects. + if self.repository_manifest: + if self.data.content_in_root: + self.content.path.remote = "" + + if self.content.path.remote == "apps": + addir = await self.repository_object.get_contents( + self.content.path.remote, self.ref + ) + self.content.path.remote = addir[0].path + self.content.objects = await self.repository_object.get_contents( + self.content.path.remote, self.ref + ) + + # Set local path + self.content.path.local = self.localpath diff --git a/custom_components/hacs/repositories/integration.py b/custom_components/hacs/repositories/integration.py new file mode 100644 index 0000000..7b9f874 --- /dev/null +++ b/custom_components/hacs/repositories/integration.py @@ -0,0 +1,93 @@ +"""Class for integrations in HACS.""" +from integrationhelper import Logger + +from homeassistant.loader import async_get_custom_components + +from custom_components.hacs.hacsbase.exceptions import HacsException +from custom_components.hacs.helpers.filters import get_first_directory_in_directory +from custom_components.hacs.helpers.information import get_integration_manifest +from custom_components.hacs.repositories.repository import HacsRepository + + +class HacsIntegration(HacsRepository): + """Integrations in HACS.""" + + def __init__(self, full_name): + """Initialize.""" + super().__init__() + self.data.full_name = full_name + self.data.category = "integration" + self.content.path.remote = "custom_components" + self.content.path.local = self.localpath + self.logger = Logger(f"hacs.repository.{self.data.category}.{full_name}") + + @property + def localpath(self): + """Return localpath.""" + return f"{self.hacs.system.config_path}/custom_components/{self.data.domain}" + + async def validate_repository(self): + """Validate.""" + await self.common_validate() + + # Custom step 1: Validate content. + if self.data.content_in_root: + self.content.path.remote = "" + + if self.content.path.remote == "custom_components": + name = get_first_directory_in_directory(self.tree, "custom_components") + if name is None: + raise HacsException( + f"Repostitory structure for {self.ref.replace('tags/','')} is not compliant" + ) + self.content.path.remote = f"custom_components/{name}" + + try: + await get_integration_manifest(self) + except HacsException as exception: + self.logger.error(exception) + + # Handle potential errors + if self.validate.errors: + for error in self.validate.errors: + if not self.hacs.system.status.startup: + self.logger.error(error) + return self.validate.success + + async def registration(self): + """Registration.""" + if not await self.validate_repository(): + return False + + # Run common registration steps. + await self.common_registration() + + # Set local path + self.content.path.local = self.localpath + + async def update_repository(self): + """Update.""" + if self.hacs.github.ratelimits.remaining == 0: + return + await self.common_update() + + if self.data.content_in_root: + self.content.path.remote = "" + + if self.content.path.remote == "custom_components": + name = get_first_directory_in_directory(self.tree, "custom_components") + self.content.path.remote = f"custom_components/{name}" + + try: + await get_integration_manifest(self) + except HacsException as exception: + self.logger.error(exception) + + # Set local path + self.content.path.local = self.localpath + + async def reload_custom_components(self): + """Reload custom_components (and config flows)in HA.""" + self.logger.info("Reloading custom_component cache") + del self.hacs.hass.data["custom_components"] + await async_get_custom_components(self.hacs.hass) diff --git a/custom_components/hacs/repositories/manifest.py b/custom_components/hacs/repositories/manifest.py new file mode 100644 index 0000000..4213d33 --- /dev/null +++ b/custom_components/hacs/repositories/manifest.py @@ -0,0 +1,42 @@ +""" +Manifest handling of a repository. + +https://hacs.xyz/docs/publish/start#hacsjson +""" +from typing import List +import attr + +from custom_components.hacs.hacsbase.exceptions import HacsException + + +@attr.s(auto_attribs=True) +class HacsManifest: + """HacsManifest class.""" + + name: str = None + content_in_root: bool = False + zip_release: bool = False + filename: str = None + manifest: dict = {} + hacs: str = None + hide_default_branch: bool = False + domains: List[str] = [] + country: List[str] = [] + homeassistant: str = None + persistent_directory: str = None + iot_class: str = None + render_readme: bool = False + + @staticmethod + def from_dict(manifest: dict): + """Set attributes from dicts.""" + if manifest is None: + raise HacsException("Missing manifest data") + + manifest_data = HacsManifest() + + manifest_data.manifest = manifest + + for key in manifest: + setattr(manifest_data, key, manifest[key]) + return manifest_data diff --git a/custom_components/hacs/repositories/plugin.py b/custom_components/hacs/repositories/plugin.py new file mode 100644 index 0000000..277e604 --- /dev/null +++ b/custom_components/hacs/repositories/plugin.py @@ -0,0 +1,107 @@ +"""Class for plugins in HACS.""" +import json +from integrationhelper import Logger + +from .repository import HacsRepository +from ..hacsbase.exceptions import HacsException + +from custom_components.hacs.helpers.information import find_file_name + + +class HacsPlugin(HacsRepository): + """Plugins in HACS.""" + + def __init__(self, full_name): + """Initialize.""" + super().__init__() + self.data.full_name = full_name + self.data.file_name = None + self.data.category = "plugin" + self.information.javascript_type = None + self.content.path.local = ( + f"{self.hacs.system.config_path}/www/community/{full_name.split('/')[-1]}" + ) + self.logger = Logger(f"hacs.repository.{self.data.category}.{full_name}") + + async def validate_repository(self): + """Validate.""" + # Run common validation steps. + await self.common_validate() + + # Custom step 1: Validate content. + find_file_name(self) + + if self.content.path.remote is None: + raise HacsException( + f"Repostitory structure for {self.ref.replace('tags/','')} is not compliant" + ) + + if self.content.path.remote == "release": + self.content.single = True + + # Handle potential errors + if self.validate.errors: + for error in self.validate.errors: + if not self.hacs.system.status.startup: + self.logger.error(error) + return self.validate.success + + async def registration(self): + """Registration.""" + if not await self.validate_repository(): + return False + + # Run common registration steps. + await self.common_registration() + + async def update_repository(self): + """Update.""" + if self.hacs.github.ratelimits.remaining == 0: + return + # Run common update steps. + await self.common_update() + + # Get plugin objects. + find_file_name(self) + + # Get JS type + await self.parse_readme_for_jstype() + + if self.content.path.remote is None: + self.validate.errors.append("Repostitory structure not compliant") + + if self.content.path.remote == "release": + self.content.single = True + + async def get_package_content(self): + """Get package content.""" + try: + package = await self.repository_object.get_contents("package.json") + package = json.loads(package.content) + + if package: + self.data.authors = package["author"] + except Exception: # pylint: disable=broad-except + pass + + async def parse_readme_for_jstype(self): + """Parse the readme looking for js type.""" + readme = None + readme_files = ["readme", "readme.md"] + root = await self.repository_object.get_contents("") + for file in root: + if file.name.lower() in readme_files: + readme = await self.repository_object.get_contents(file.name) + break + + if readme is None: + return + + readme = readme.content + for line in readme.splitlines(): + if "type: module" in line: + self.information.javascript_type = "module" + break + elif "type: js" in line: + self.information.javascript_type = "js" + break diff --git a/custom_components/hacs/repositories/python_script.py b/custom_components/hacs/repositories/python_script.py new file mode 100644 index 0000000..c9f2db7 --- /dev/null +++ b/custom_components/hacs/repositories/python_script.py @@ -0,0 +1,87 @@ +"""Class for python_scripts in HACS.""" +from integrationhelper import Logger + +from .repository import HacsRepository +from ..hacsbase.exceptions import HacsException +from ..helpers.information import find_file_name + + +class HacsPythonScript(HacsRepository): + """python_scripts in HACS.""" + + category = "python_script" + + def __init__(self, full_name): + """Initialize.""" + super().__init__() + self.data.full_name = full_name + self.data.category = "python_script" + self.content.path.remote = "python_scripts" + self.content.path.local = f"{self.hacs.system.config_path}/python_scripts" + self.content.single = True + self.logger = Logger(f"hacs.repository.{self.data.category}.{full_name}") + + async def validate_repository(self): + """Validate.""" + # Run common validation steps. + await self.common_validate() + + # Custom step 1: Validate content. + if self.data.content_in_root: + self.content.path.remote = "" + + compliant = False + for treefile in self.treefiles: + if treefile.startswith(f"{self.content.path.remote}") and treefile.endswith( + ".py" + ): + compliant = True + break + if not compliant: + raise HacsException( + f"Repostitory structure for {self.ref.replace('tags/','')} is not compliant" + ) + + # Handle potential errors + if self.validate.errors: + for error in self.validate.errors: + if not self.hacs.system.status.startup: + self.logger.error(error) + return self.validate.success + + async def registration(self): + """Registration.""" + if not await self.validate_repository(): + return False + + # Run common registration steps. + await self.common_registration() + + # Set name + find_file_name(self) + + async def update_repository(self): # lgtm[py/similar-function] + """Update.""" + if self.hacs.github.ratelimits.remaining == 0: + return + # Run common update steps. + await self.common_update() + + # Get python_script objects. + if self.data.content_in_root: + self.content.path.remote = "" + + compliant = False + for treefile in self.treefiles: + if treefile.startswith(f"{self.content.path.remote}") and treefile.endswith( + ".py" + ): + compliant = True + break + if not compliant: + raise HacsException( + f"Repostitory structure for {self.ref.replace('tags/','')} is not compliant" + ) + + # Update name + find_file_name(self) diff --git a/custom_components/hacs/repositories/removed.py b/custom_components/hacs/repositories/removed.py new file mode 100644 index 0000000..69955eb --- /dev/null +++ b/custom_components/hacs/repositories/removed.py @@ -0,0 +1,17 @@ +"""Object for removed repositories.""" +import attr + + +@attr.s(auto_attribs=True) +class RemovedRepository: + repository: str = None + reason: str = None + link: str = None + removal_type: str = None # archived, not_compliant, critical, dev, broken + acknowledged: bool = False + + def update_data(self, data: dict): + """Update data of the repository.""" + for key in data: + if key in self.__dict__: + setattr(self, key, data[key]) diff --git a/custom_components/hacs/repositories/repository.py b/custom_components/hacs/repositories/repository.py new file mode 100644 index 0000000..b471683 --- /dev/null +++ b/custom_components/hacs/repositories/repository.py @@ -0,0 +1,424 @@ +"""Repository.""" +# pylint: disable=broad-except, bad-continuation, no-member +import json +import os +import tempfile +import zipfile +from integrationhelper import Validate +from aiogithubapi import AIOGitHubException +from .manifest import HacsManifest +from ..helpers.misc import get_repository_name +from ..handler.download import async_download_file, async_save_file +from ..helpers.misc import version_left_higher_then_right +from ..helpers.install import install_repository, version_to_install + +from custom_components.hacs.globals import get_hacs +from custom_components.hacs.helpers.information import ( + get_info_md_content, + get_repository, +) +from custom_components.hacs.helpers.validate_repository import ( + common_validate, + common_update_data, +) +from custom_components.hacs.repositories.repositorydata import RepositoryData + + +class RepositoryVersions: + """Versions.""" + + available = None + available_commit = None + installed = None + installed_commit = None + + +class RepositoryStatus: + """Repository status.""" + + hide = False + installed = False + last_updated = None + new = True + selected_tag = None + show_beta = False + track = True + updated_info = False + first_install = True + + +class RepositoryInformation: + """RepositoryInformation.""" + + additional_info = None + authors = [] + category = None + default_branch = None + description = "" + state = None + full_name = None + file_name = None + javascript_type = None + homeassistant_version = None + last_updated = None + uid = None + stars = 0 + info = None + name = None + topics = [] + + +class RepositoryReleases: + """RepositoyReleases.""" + + last_release = None + last_release_object = None + last_release_object_downloads = None + published_tags = [] + objects = [] + releases = False + downloads = None + + +class RepositoryPath: + """RepositoryPath.""" + + local = None + remote = None + + +class RepositoryContent: + """RepositoryContent.""" + + path = None + files = [] + objects = [] + single = False + + +class HacsRepository: + """HacsRepository.""" + + def __init__(self): + """Set up HacsRepository.""" + self.hacs = get_hacs() + self.data = RepositoryData() + self.content = RepositoryContent() + self.content.path = RepositoryPath() + self.information = RepositoryInformation() + self.repository_object = None + self.status = RepositoryStatus() + self.state = None + self.integration_manifest = {} + self.repository_manifest = HacsManifest.from_dict({}) + self.validate = Validate() + self.releases = RepositoryReleases() + self.versions = RepositoryVersions() + self.pending_restart = False + self.tree = [] + self.treefiles = [] + self.ref = None + + @property + def pending_upgrade(self): + """Return pending upgrade.""" + if self.status.installed: + if self.status.selected_tag is not None: + if self.status.selected_tag == self.data.default_branch: + if self.versions.installed_commit != self.versions.available_commit: + return True + return False + if self.display_installed_version != self.display_available_version: + return True + return False + + @property + def config_flow(self): + """Return bool if integration has config_flow.""" + if self.integration_manifest: + if self.data.full_name == "hacs/integration": + return False + return self.integration_manifest.get("config_flow", False) + return False + + @property + def custom(self): + """Return flag if the repository is custom.""" + if self.data.full_name.split("/")[0] in ["custom-components", "custom-cards"]: + return False + if self.data.full_name.lower() in [x.lower() for x in self.hacs.common.default]: + return False + if self.data.full_name == "hacs/integration": + return False + return True + + @property + def can_install(self): + """Return bool if repository can be installed.""" + target = None + if self.information.homeassistant_version is not None: + target = self.information.homeassistant_version + if self.repository_manifest is not None: + if self.data.homeassistant is not None: + target = self.data.homeassistant + + if target is not None: + if self.releases.releases: + if not version_left_higher_then_right( + self.hacs.system.ha_version, target + ): + return False + return True + + @property + def display_name(self): + """Return display name.""" + return get_repository_name(self) + + @property + def display_status(self): + """Return display_status.""" + if self.status.new: + status = "new" + elif self.pending_restart: + status = "pending-restart" + elif self.pending_upgrade: + status = "pending-upgrade" + elif self.status.installed: + status = "installed" + else: + status = "default" + return status + + @property + def display_status_description(self): + """Return display_status_description.""" + description = { + "default": "Not installed.", + "pending-restart": "Restart pending.", + "pending-upgrade": "Upgrade pending.", + "installed": "No action required.", + "new": "This is a newly added repository.", + } + return description[self.display_status] + + @property + def display_installed_version(self): + """Return display_authors""" + if self.versions.installed is not None: + installed = self.versions.installed + else: + if self.versions.installed_commit is not None: + installed = self.versions.installed_commit + else: + installed = "" + return installed + + @property + def display_available_version(self): + """Return display_authors""" + if self.versions.available is not None: + available = self.versions.available + else: + if self.versions.available_commit is not None: + available = self.versions.available_commit + else: + available = "" + return available + + @property + def display_version_or_commit(self): + """Does the repositoriy use releases or commits?""" + if self.releases.releases: + version_or_commit = "version" + else: + version_or_commit = "commit" + return version_or_commit + + @property + def main_action(self): + """Return the main action.""" + actions = { + "new": "INSTALL", + "default": "INSTALL", + "installed": "REINSTALL", + "pending-restart": "REINSTALL", + "pending-upgrade": "UPGRADE", + } + return actions[self.display_status] + + async def common_validate(self): + """Common validation steps of the repository.""" + await common_validate(self) + + async def common_registration(self): + """Common registration steps of the repository.""" + # Attach repository + if self.repository_object is None: + self.repository_object = await get_repository( + self.hacs.session, self.hacs.configuration.token, self.data.full_name + ) + self.data.update_data(self.repository_object.attributes) + + # Set id + self.information.uid = str(self.data.id) + + # Set topics + self.data.topics = self.data.topics + + # Set stargazers_count + self.data.stargazers_count = self.data.stargazers_count + + # Set description + self.data.description = self.data.description + + async def common_update(self): + """Common information update steps of the repository.""" + self.logger.debug("Getting repository information") + + # Attach repository + await common_update_data(self) + + # Update last updaeted + self.information.last_updated = self.repository_object.attributes.get( + "pushed_at", 0 + ) + + # Update last available commit + await self.repository_object.set_last_commit() + self.versions.available_commit = self.repository_object.last_commit + + # Get the content of hacs.json + await self.get_repository_manifest_content() + + # Update "info.md" + self.information.additional_info = await get_info_md_content(self) + + async def install(self): + """Common installation steps of the repository.""" + await install_repository(self) + + async def download_zip(self, validate): + """Download ZIP archive from repository release.""" + try: + contents = False + + for release in self.releases.objects: + self.logger.info(f"ref: {self.ref} --- tag: {release.tag_name}") + if release.tag_name == self.ref.split("/")[1]: + contents = release.assets + + if not contents: + return validate + + for content in contents or []: + filecontent = await async_download_file(content.download_url) + + if filecontent is None: + validate.errors.append(f"[{content.name}] was not downloaded.") + continue + + result = await async_save_file( + f"{tempfile.gettempdir()}/{self.data.filename}", filecontent + ) + with zipfile.ZipFile( + f"{tempfile.gettempdir()}/{self.data.filename}", "r" + ) as zip_file: + zip_file.extractall(self.content.path.local) + + if result: + self.logger.info(f"download of {content.name} complete") + continue + validate.errors.append(f"[{content.name}] was not downloaded.") + except Exception: + validate.errors.append(f"Download was not complete.") + + return validate + + async def download_content(self, validate, directory_path, local_directory, ref): + """Download the content of a directory.""" + from custom_components.hacs.helpers.download import download_content + + validate = await download_content(self) + return validate + + async def get_repository_manifest_content(self): + """Get the content of the hacs.json file.""" + if not "hacs.json" in [x.filename for x in self.tree]: + return + if self.ref is None: + self.ref = version_to_install(self) + try: + manifest = await self.repository_object.get_contents("hacs.json", self.ref) + self.repository_manifest = HacsManifest.from_dict( + json.loads(manifest.content) + ) + self.data.update_data(json.loads(manifest.content)) + except (AIOGitHubException, Exception): # Gotta Catch 'Em All + pass + + def remove(self): + """Run remove tasks.""" + self.logger.info("Starting removal") + + if self.information.uid in self.hacs.common.installed: + self.hacs.common.installed.remove(self.information.uid) + for repository in self.hacs.repositories: + if repository.information.uid == self.information.uid: + self.hacs.repositories.remove(repository) + + async def uninstall(self): + """Run uninstall tasks.""" + self.logger.info("Uninstalling") + await self.remove_local_directory() + self.status.installed = False + if self.data.category == "integration": + if self.config_flow: + await self.reload_custom_components() + else: + self.pending_restart = True + elif self.data.category == "theme": + try: + await self.hacs.hass.services.async_call( + "frontend", "reload_themes", {} + ) + except Exception: # pylint: disable=broad-except + pass + if self.data.full_name in self.hacs.common.installed: + self.hacs.common.installed.remove(self.data.full_name) + self.versions.installed = None + self.versions.installed_commit = None + self.hacs.hass.bus.async_fire( + "hacs/repository", + {"id": 1337, "action": "uninstall", "repository": self.data.full_name}, + ) + + async def remove_local_directory(self): + """Check the local directory.""" + import shutil + from asyncio import sleep + + try: + if self.data.category == "python_script": + local_path = "{}/{}.py".format(self.content.path.local, self.data.name) + elif self.data.category == "theme": + local_path = "{}/{}.yaml".format( + self.content.path.local, self.data.name + ) + else: + local_path = self.content.path.local + + if os.path.exists(local_path): + self.logger.debug(f"Removing {local_path}") + + if self.data.category in ["python_script", "theme"]: + os.remove(local_path) + else: + shutil.rmtree(local_path) + + while os.path.exists(local_path): + await sleep(1) + + except Exception as exception: + self.logger.debug(f"Removing {local_path} failed with {exception}") + return diff --git a/custom_components/hacs/repositories/repositorydata.py b/custom_components/hacs/repositories/repositorydata.py new file mode 100644 index 0000000..e26ae6d --- /dev/null +++ b/custom_components/hacs/repositories/repositorydata.py @@ -0,0 +1,82 @@ +"""Repository data.""" +from datetime import datetime +from typing import List +import attr + + +@attr.s(auto_attribs=True) +class RepositoryData: + """RepositoryData class.""" + + id: int = 0 + full_name: str = "" + pushed_at: str = "" + category: str = "" + archived: bool = False + description: str = "" + manifest_name: str = None + topics: List[str] = [] + fork: bool = False + domain: str = "" + default_branch: str = None + stargazers_count: int = 0 + last_commit: str = "" + file_name: str = "" + content_in_root: bool = False + zip_release: bool = False + filename: str = "" + render_readme: bool = False + hide_default_branch: bool = False + domains: List[str] = [] + country: List[str] = [] + authors: List[str] = [] + homeassistant: str = None # Minimum Home Assistant version + hacs: str = None # Minimum HACS version + persistent_directory: str = None + iot_class: str = None + + @property + def name(self): + """Return the name.""" + if self.category == "integration": + return self.domain + return self.full_name.split("/")[-1] + + def to_json(self): + """Export to json.""" + return self.__dict__ + + @staticmethod + def create_from_dict(source: dict): + """Set attributes from dicts.""" + data = RepositoryData() + for key in source: + if key in data.__dict__: + if key == "pushed_at": + setattr( + data, key, datetime.strptime(source[key], "%Y-%m-%dT%H:%M:%SZ") + ) + elif key == "county": + if isinstance(source[key], str): + setattr(data, key, [source[key]]) + else: + setattr(data, key, source[key]) + else: + setattr(data, key, source[key]) + return data + + def update_data(self, data: dict): + """Update data of the repository.""" + for key in data: + if key in self.__dict__: + if key == "pushed_at": + setattr( + self, key, datetime.strptime(data[key], "%Y-%m-%dT%H:%M:%SZ") + ) + elif key == "county": + if isinstance(data[key], str): + setattr(self, key, [data[key]]) + else: + setattr(self, key, data[key]) + else: + setattr(self, key, data[key]) diff --git a/custom_components/hacs/repositories/theme.py b/custom_components/hacs/repositories/theme.py new file mode 100644 index 0000000..6e09657 --- /dev/null +++ b/custom_components/hacs/repositories/theme.py @@ -0,0 +1,72 @@ +"""Class for themes in HACS.""" +from integrationhelper import Logger +from .repository import HacsRepository +from ..hacsbase.exceptions import HacsException +from ..helpers.information import find_file_name + + +class HacsTheme(HacsRepository): + """Themes in HACS.""" + + def __init__(self, full_name): + """Initialize.""" + super().__init__() + self.data.full_name = full_name + self.data.category = "theme" + self.content.path.remote = "themes" + self.content.path.local = f"{self.hacs.system.config_path}/themes/" + self.content.single = False + self.logger = Logger(f"hacs.repository.{self.data.category}.{full_name}") + + async def validate_repository(self): + """Validate.""" + # Run common validation steps. + await self.common_validate() + + # Custom step 1: Validate content. + compliant = False + for treefile in self.treefiles: + if treefile.startswith("themes/") and treefile.endswith(".yaml"): + compliant = True + break + if not compliant: + raise HacsException( + f"Repostitory structure for {self.ref.replace('tags/','')} is not compliant" + ) + + if self.data.content_in_root: + self.content.path.remote = "" + + # Handle potential errors + if self.validate.errors: + for error in self.validate.errors: + if not self.hacs.system.status.startup: + self.logger.error(error) + return self.validate.success + + async def registration(self): + """Registration.""" + if not await self.validate_repository(): + return False + + # Run common registration steps. + await self.common_registration() + + # Set name + find_file_name(self) + self.content.path.local = f"{self.hacs.system.config_path}/themes/{self.data.file_name.replace('.yaml', '')}" + + async def update_repository(self): # lgtm[py/similar-function] + """Update.""" + if self.hacs.github.ratelimits.remaining == 0: + return + # Run common update steps. + await self.common_update() + + # Get theme objects. + if self.data.content_in_root: + self.content.path.remote = "" + + # Update name + find_file_name(self) + self.content.path.local = f"{self.hacs.system.config_path}/themes/{self.data.file_name.replace('.yaml', '')}" diff --git a/custom_components/hacs/sensor.py b/custom_components/hacs/sensor.py new file mode 100644 index 0000000..46f1d85 --- /dev/null +++ b/custom_components/hacs/sensor.py @@ -0,0 +1,99 @@ +"""Sensor platform for HACS.""" +# pylint: disable=unused-argument +from homeassistant.helpers.entity import Entity +from .hacsbase import Hacs as hacs +from .const import DOMAIN, VERSION, NAME_SHORT + + +async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): + """Setup sensor platform.""" + async_add_entities([HACSSensor()]) + + +async def async_setup_entry(hass, config_entry, async_add_devices): + """Setup sensor platform.""" + async_add_devices([HACSSensor()]) + + +class HACSDevice(Entity): + """HACS Device class.""" + + @property + def device_info(self): + """Return device information about HACS.""" + return { + "identifiers": {(DOMAIN, self.unique_id)}, + "name": NAME_SHORT, + "manufacturer": "hacs.xyz", + "model": "", + "sw_version": VERSION, + } + + +class HACSSensor(HACSDevice): + """HACS Sensor class.""" + + def __init__(self): + """Initialize.""" + self._state = None + self.repositories = [] + + async def async_update(self): + """Update the sensor.""" + if hacs.system.status.background_task: + return + + self.repositories = [] + + for repository in hacs.repositories: + if ( + repository.pending_upgrade + and repository.data.category in hacs.common.categories + ): + self.repositories.append(repository) + self._state = len(self.repositories) + + @property + def unique_id(self): + """Return a unique ID to use for this sensor.""" + return ( + "0717a0cd-745c-48fd-9b16-c8534c9704f9-bc944b0f-fd42-4a58-a072-ade38d1444cd" + ) + + @property + def name(self): + """Return the name of the sensor.""" + return "hacs" + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def icon(self): + """Return the icon of the sensor.""" + return "hacs:hacs" + + @property + def unit_of_measurement(self): + """Return the unit of measurement.""" + return "pending update(s)" + + @property + def device_state_attributes(self): + """Return attributes for the sensor.""" + data = [] + for repository in self.repositories: + data.append( + { + "name": repository.data.full_name, + "display_name": repository.display_name, + "installed version": repository.display_installed_version, + "available version": repository.display_available_version, + } + ) + return { + "repositories": data, + "attribution": "It is expected to see [object Object] here, for more info see https://hacs.xyz/docs/basic/sensor", + } diff --git a/custom_components/hacs/services.yaml b/custom_components/hacs/services.yaml new file mode 100644 index 0000000..982a956 --- /dev/null +++ b/custom_components/hacs/services.yaml @@ -0,0 +1,15 @@ +install: + description: This is NOT intended to be used here, this is intended for developers! + fields: + repository: + description: The repository ID + example: '"123456789"' +register: + description: This is NOT intended to be used here, this is intended for developers! + fields: + repository: + description: The full name of the repository + example: 'developer/repo' + repository_type: + description: The repository type + example: 'plugin' \ No newline at end of file diff --git a/custom_components/hacs/setup.py b/custom_components/hacs/setup.py new file mode 100644 index 0000000..af7f9a0 --- /dev/null +++ b/custom_components/hacs/setup.py @@ -0,0 +1,110 @@ +"""Setup functions for HACS.""" +# pylint: disable=bad-continuation +from hacs_frontend.version import VERSION as FE_VERSION +from homeassistant.helpers import discovery + +from custom_components.hacs.hacsbase.exceptions import HacsException +from custom_components.hacs.const import VERSION, DOMAIN +from custom_components.hacs.globals import get_hacs +from custom_components.hacs.helpers.information import get_repository +from custom_components.hacs.helpers.register_repository import register_repository + + +async def load_hacs_repository(): + """Load HACS repositroy.""" + hacs = get_hacs() + + try: + repository = hacs.get_by_name("hacs/integration") + if repository is None: + await register_repository("hacs/integration", "integration") + repository = hacs.get_by_name("hacs/integration") + if repository is None: + raise HacsException("Unknown error") + repository.status.installed = True + repository.versions.installed = VERSION + repository.status.new = False + hacs.repo = repository.repository_object + hacs.data_repo = await get_repository( + hacs.session, hacs.configuration.token, "hacs/default" + ) + except HacsException as exception: + if "403" in f"{exception}": + hacs.logger.critical("GitHub API is ratelimited, or the token is wrong.") + else: + hacs.logger.critical(f"[{exception}] - Could not load HACS!") + return False + return True + + +def setup_extra_stores(): + """Set up extra stores in HACS if enabled in Home Assistant.""" + hacs = get_hacs() + if "python_script" in hacs.hass.config.components: + if "python_script" not in hacs.common.categories: + hacs.common.categories.append("python_script") + + if hacs.hass.services.services.get("frontend", {}).get("reload_themes") is not None: + if "theme" not in hacs.common.categories: + hacs.common.categories.append("theme") + + +def add_sensor(): + """Add sensor.""" + hacs = get_hacs() + + try: + if hacs.configuration.config_type == "yaml": + hacs.hass.async_create_task( + discovery.async_load_platform( + hacs.hass, "sensor", DOMAIN, {}, hacs.configuration.config + ) + ) + else: + hacs.hass.async_add_job( + hacs.hass.config_entries.async_forward_entry_setup( + hacs.configuration.config_entry, "sensor" + ) + ) + except ValueError: + pass + + +async def setup_frontend(): + """Configure the HACS frontend elements.""" + from .http import HacsFrontend, HacsPluginViewLegacy + from .ws_api_handlers import setup_ws_api + + hacs = get_hacs() + + hacs.hass.http.register_view(HacsFrontend()) + hacs.frontend.version_running = FE_VERSION + + # Legacy views, remove with 2.0 + hacs.hass.http.register_view(HacsPluginViewLegacy()) + + # Add to sidepanel + custom_panel_config = { + "name": "hacs-frontend", + "embed_iframe": False, + "trust_external": False, + "js_url": f"/hacsfiles/frontend-{hacs.frontend.version_running}.js", + } + + config = {} + config["_panel_custom"] = custom_panel_config + + hacs.hass.components.frontend.async_register_built_in_panel( + component_name="custom", + sidebar_title=hacs.configuration.sidepanel_title, + sidebar_icon=hacs.configuration.sidepanel_icon, + frontend_url_path="hacs", + config=config, + require_admin=True, + ) + + if "frontend_extra_module_url" not in hacs.hass.data: + hacs.hass.data["frontend_extra_module_url"] = set() + hacs.hass.data["frontend_extra_module_url"].add("/hacsfiles/iconset.js") + + await setup_ws_api(hacs.hass) diff --git a/custom_components/hacs/store.py b/custom_components/hacs/store.py new file mode 100644 index 0000000..848d4af --- /dev/null +++ b/custom_components/hacs/store.py @@ -0,0 +1,19 @@ +"""Storage handers.""" +from homeassistant.helpers.json import JSONEncoder +from homeassistant.helpers.storage import Store +from .hacsbase.const import STORAGE_VERSION + + +async def async_load_from_store(hass, key): + """Load the retained data from store and return de-serialized data.""" + store = Store(hass, STORAGE_VERSION, f"hacs.{key}", encoder=JSONEncoder) + restored = await store.async_load() + if restored is None: + return {} + return restored + + +async def async_save_to_store(hass, key, data): + """Generate dynamic data to store and save it to the filesystem.""" + store = Store(hass, STORAGE_VERSION, f"hacs.{key}", encoder=JSONEncoder) + await store.async_save(data) diff --git a/custom_components/hacs/ws_api_handlers.py b/custom_components/hacs/ws_api_handlers.py new file mode 100644 index 0000000..6f52660 --- /dev/null +++ b/custom_components/hacs/ws_api_handlers.py @@ -0,0 +1,379 @@ +"""WebSocket API for HACS.""" +# pylint: disable=unused-argument +import sys +import os +import voluptuous as vol +from aiogithubapi import AIOGitHubException +from homeassistant.components import websocket_api +import homeassistant.helpers.config_validation as cv +from .hacsbase.exceptions import HacsException +from .store import async_load_from_store, async_save_to_store + +from custom_components.hacs.globals import get_hacs +from custom_components.hacs.helpers.register_repository import register_repository + + +async def setup_ws_api(hass): + """Set up WS API handlers.""" + websocket_api.async_register_command(hass, hacs_settings) + websocket_api.async_register_command(hass, hacs_config) + websocket_api.async_register_command(hass, hacs_repositories) + websocket_api.async_register_command(hass, hacs_repository) + websocket_api.async_register_command(hass, hacs_repository_data) + websocket_api.async_register_command(hass, check_local_path) + websocket_api.async_register_command(hass, hacs_status) + websocket_api.async_register_command(hass, acknowledge_critical_repository) + websocket_api.async_register_command(hass, get_critical_repositories) + + +@websocket_api.async_response +@websocket_api.websocket_command( + { + vol.Required("type"): "hacs/settings", + vol.Optional("action"): cv.string, + vol.Optional("category"): cv.string, + } +) +async def hacs_settings(hass, connection, msg): + """Handle get media player cover command.""" + hacs = get_hacs() + action = msg["action"] + hacs.logger.debug(f"WS action '{action}'") + + if action == "set_fe_grid": + hacs.configuration.frontend_mode = "Grid" + + elif action == "onboarding_done": + hacs.configuration.onboarding_done = True + + elif action == "set_fe_table": + hacs.configuration.frontend_mode = "Table" + + elif action == "set_fe_compact_true": + hacs.configuration.frontend_compact = False + + elif action == "set_fe_compact_false": + hacs.configuration.frontend_compact = True + + elif action == "reload_data": + hacs.system.status.reloading_data = True + hass.bus.async_fire("hacs/status", {}) + await hacs.recuring_tasks_all() + hacs.system.status.reloading_data = False + hass.bus.async_fire("hacs/status", {}) + + elif action == "upgrade_all": + hacs.system.status.upgrading_all = True + hacs.system.status.background_task = True + hass.bus.async_fire("hacs/status", {}) + for repository in hacs.repositories: + if repository.pending_upgrade: + repository.status.selected_tag = None + await repository.install() + hacs.system.status.upgrading_all = False + hacs.system.status.background_task = False + hass.bus.async_fire("hacs/status", {}) + hass.bus.async_fire("hacs/repository", {}) + + elif action == "clear_new": + for repo in hacs.repositories: + if msg.get("category") == repo.data.category: + if repo.status.new: + hacs.logger.debug( + f"Clearing new flag from '{repo.data.full_name}'" + ) + repo.status.new = False + else: + hacs.logger.error(f"WS action '{action}' is not valid") + hass.bus.async_fire("hacs/config", {}) + await hacs.data.async_write() + + +@websocket_api.async_response +@websocket_api.websocket_command({vol.Required("type"): "hacs/config"}) +async def hacs_config(hass, connection, msg): + """Handle get media player cover command.""" + hacs = get_hacs() + config = hacs.configuration + + content = {} + content["frontend_mode"] = config.frontend_mode + content["frontend_compact"] = config.frontend_compact + content["onboarding_done"] = config.onboarding_done + content["version"] = hacs.version + content["dev"] = config.dev + content["debug"] = config.debug + content["country"] = config.country + content["experimental"] = config.experimental + content["categories"] = hacs.common.categories + + connection.send_message(websocket_api.result_message(msg["id"], content)) + + +@websocket_api.async_response +@websocket_api.websocket_command({vol.Required("type"): "hacs/status"}) +async def hacs_status(hass, connection, msg): + """Handle get media player cover command.""" + hacs = get_hacs() + content = { + "startup": hacs.system.status.startup, + "background_task": hacs.system.status.background_task, + "lovelace_mode": hacs.system.lovelace_mode, + "reloading_data": hacs.system.status.reloading_data, + "upgrading_all": hacs.system.status.upgrading_all, + "disabled": hacs.system.disabled, + } + connection.send_message(websocket_api.result_message(msg["id"], content)) + + +@websocket_api.async_response +@websocket_api.websocket_command({vol.Required("type"): "hacs/repositories"}) +async def hacs_repositories(hass, connection, msg): + """Handle get media player cover command.""" + hacs = get_hacs() + repositories = hacs.repositories + content = [] + for repo in repositories: + if repo.data.category in hacs.common.categories: + data = { + "additional_info": repo.information.additional_info, + "authors": repo.data.authors, + "available_version": repo.display_available_version, + "beta": repo.status.show_beta, + "can_install": repo.can_install, + "category": repo.data.category, + "country": repo.data.country, + "config_flow": repo.config_flow, + "custom": repo.custom, + "default_branch": repo.data.default_branch, + "description": repo.data.description, + "domain": repo.integration_manifest.get("domain"), + "downloads": repo.releases.downloads, + "file_name": repo.data.file_name, + "first_install": repo.status.first_install, + "full_name": repo.data.full_name, + "hide": repo.status.hide, + "hide_default_branch": repo.data.hide_default_branch, + "homeassistant": repo.data.homeassistant, + "id": repo.information.uid, + "info": repo.information.info, + "installed_version": repo.display_installed_version, + "installed": repo.status.installed, + "javascript_type": repo.information.javascript_type, + "last_updated": repo.information.last_updated, + "local_path": repo.content.path.local, + "main_action": repo.main_action, + "name": repo.display_name, + "new": repo.status.new, + "pending_upgrade": repo.pending_upgrade, + "releases": repo.releases.published_tags, + "selected_tag": repo.status.selected_tag, + "stars": repo.data.stargazers_count, + "state": repo.state, + "status_description": repo.display_status_description, + "status": repo.display_status, + "topics": repo.data.topics, + "updated_info": repo.status.updated_info, + "version_or_commit": repo.display_version_or_commit, + } + + content.append(data) + + connection.send_message(websocket_api.result_message(msg["id"], content)) + + +@websocket_api.async_response +@websocket_api.websocket_command( + { + vol.Required("type"): "hacs/repository", + vol.Optional("action"): cv.string, + vol.Optional("repository"): cv.string, + } +) +async def hacs_repository(hass, connection, msg): + """Handle get media player cover command.""" + hacs = get_hacs() + try: + repo_id = msg.get("repository") + action = msg.get("action") + + if repo_id is None or action is None: + return + + repository = hacs.get_by_id(repo_id) + hacs.logger.debug(f"Running {action} for {repository.data.full_name}") + + if action == "update": + await repository.update_repository() + repository.status.updated_info = True + repository.status.new = False + + elif action == "install": + was_installed = repository.status.installed + await repository.install() + if not was_installed: + hass.bus.async_fire("hacs/reload", {"force": False}) + + elif action == "uninstall": + await repository.uninstall() + hass.bus.async_fire("hacs/reload", {"force": False}) + + elif action == "hide": + repository.status.hide = True + + elif action == "unhide": + repository.status.hide = False + + elif action == "show_beta": + repository.status.show_beta = True + await repository.update_repository() + + elif action == "hide_beta": + repository.status.show_beta = False + await repository.update_repository() + + elif action == "delete": + repository.status.show_beta = False + repository.remove() + + elif action == "set_version": + if msg["version"] == repository.data.default_branch: + repository.status.selected_tag = None + else: + repository.status.selected_tag = msg["version"] + await repository.update_repository() + + else: + hacs.logger.error(f"WS action '{action}' is not valid") + + repository.state = None + await hacs.data.async_write() + except AIOGitHubException as exception: + hass.bus.async_fire("hacs/error", {"message": str(exception)}) + except AttributeError as exception: + hass.bus.async_fire( + "hacs/error", {"message": f"Could not use repository with ID {repo_id}"} + ) + except Exception as exception: # pylint: disable=broad-except + hass.bus.async_fire("hacs/error", {"message": str(exception)}) + + +@websocket_api.async_response +@websocket_api.websocket_command( + { + vol.Required("type"): "hacs/repository/data", + vol.Optional("action"): cv.string, + vol.Optional("repository"): cv.string, + vol.Optional("data"): cv.string, + } +) +async def hacs_repository_data(hass, connection, msg): + """Handle get media player cover command.""" + hacs = get_hacs() + repo_id = msg.get("repository") + action = msg.get("action") + data = msg.get("data") + + if repo_id is None: + return + + if action == "add": + if "github." in repo_id: + repo_id = repo_id.split("github.com/")[1] + + if repo_id in hacs.common.skip: + hacs.common.skip.remove(repo_id) + + if not hacs.get_by_name(repo_id): + try: + registration = await register_repository(repo_id, data.lower()) + if registration is not None: + raise HacsException(registration) + except Exception as exception: # pylint: disable=broad-except + hass.bus.async_fire( + "hacs/error", + { + "action": "add_repository", + "exception": str(sys.exc_info()[0].__name__), + "message": str(exception), + }, + ) + else: + hass.bus.async_fire( + "hacs/error", + { + "action": "add_repository", + "message": f"Repository '{repo_id}' exists in the store.", + }, + ) + + repository = hacs.get_by_name(repo_id) + else: + repository = hacs.get_by_id(repo_id) + + if repository is None: + hass.bus.async_fire("hacs/repository", {}) + return + + hacs.logger.debug(f"Running {action} for {repository.data.full_name}") + + if action == "set_state": + repository.state = data + + elif action == "set_version": + repository.status.selected_tag = data + await repository.update_repository() + repository.state = None + + elif action == "add": + repository.state = None + + else: + repository.state = None + hacs.logger.error(f"WS action '{action}' is not valid") + + await hacs.data.async_write() + + +@websocket_api.async_response +@websocket_api.websocket_command( + {vol.Required("type"): "hacs/check_path", vol.Optional("path"): cv.string} +) +async def check_local_path(hass, connection, msg): + """Handle get media player cover command.""" + path = msg.get("path") + exist = {"exist": False} + + if path is None: + return + + if os.path.exists(path): + exist["exist"] = True + + connection.send_message(websocket_api.result_message(msg["id"], exist)) + + +@websocket_api.async_response +@websocket_api.websocket_command({vol.Required("type"): "hacs/get_critical"}) +async def get_critical_repositories(hass, connection, msg): + """Handle get media player cover command.""" + critical = await async_load_from_store(hass, "critical") + if not critical: + critical = [] + connection.send_message(websocket_api.result_message(msg["id"], critical)) + + +@websocket_api.async_response +@websocket_api.websocket_command( + {vol.Required("type"): "hacs/critical", vol.Optional("repository"): cv.string} +) +async def acknowledge_critical_repository(hass, connection, msg): + """Handle get media player cover command.""" + repository = msg["repository"] + + critical = await async_load_from_store(hass, "critical") + for repo in critical: + if repository == repo["repository"]: + repo["acknowledged"] = True + await async_save_to_store(hass, "critical", critical) + connection.send_message(websocket_api.result_message(msg["id"], critical)) diff --git a/custom_components/input_select/__init__.py b/custom_components/input_select/__init__.py new file mode 100644 index 0000000..5c8e438 --- /dev/null +++ b/custom_components/input_select/__init__.py @@ -0,0 +1,198 @@ +""" +Component to offer a way to select an option from a list. + +For more details about this component, please refer to the documentation +at https://home-assistant.io/components/input_select/ +""" +import logging + +import voluptuous as vol + +from homeassistant.const import ATTR_ENTITY_ID, CONF_ICON, CONF_NAME +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity_component import EntityComponent +from homeassistant.helpers.restore_state import RestoreEntity + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = 'input_select' +ENTITY_ID_FORMAT = DOMAIN + '.{}' + +CONF_INITIAL = 'initial' +CONF_OPTIONS = 'options' +CONF_VALUES = 'values' + +ATTR_OPTION = 'option' +ATTR_OPTIONS = 'options' + +ATTR_VALUE = 'value' +ATTR_VALUES = 'values' + +SERVICE_SELECT_OPTION = 'select_option' + +SERVICE_SELECT_OPTION_SCHEMA = vol.Schema({ + vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, + vol.Required(ATTR_OPTION): cv.string, +}) + +SERVICE_SELECT_NEXT = 'select_next' + +SERVICE_SELECT_NEXT_SCHEMA = vol.Schema({ + vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, +}) + +SERVICE_SELECT_PREVIOUS = 'select_previous' + +SERVICE_SELECT_PREVIOUS_SCHEMA = vol.Schema({ + vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, +}) + + +SERVICE_SET_OPTIONS = 'set_options' + +SERVICE_SET_OPTIONS_SCHEMA = vol.Schema({ + vol.Required(ATTR_ENTITY_ID): cv.entity_ids, + vol.Required(ATTR_OPTIONS): + vol.All(cv.ensure_list, vol.Length(min=1), [cv.string]), +}) + + +def _cv_input_select(cfg): + """Configure validation helper for input select (voluptuous).""" + options = cfg[CONF_OPTIONS] + values = cfg.get(CONF_VALUES) + initial = cfg.get(CONF_INITIAL) + if initial is not None and initial not in options: + raise vol.Invalid('initial state "{}" is not part of the options: {}' + .format(initial, ','.join(options))) + return cfg + + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + cv.slug: vol.All({ + vol.Optional(CONF_NAME): cv.string, + vol.Required(CONF_OPTIONS): + vol.All(cv.ensure_list, vol.Length(min=1), [cv.string]), + vol.Optional(CONF_VALUES): + vol.All(cv.ensure_list, vol.Length(min=1), [cv.string]), + vol.Optional(CONF_INITIAL): cv.string, + vol.Optional(CONF_ICON): cv.icon, + }, _cv_input_select)}) +}, required=True, extra=vol.ALLOW_EXTRA) + + +async def async_setup(hass, config): + """Set up an input select.""" + component = EntityComponent(_LOGGER, DOMAIN, hass) + + entities = [] + + for object_id, cfg in config[DOMAIN].items(): + name = cfg.get(CONF_NAME) + options = cfg.get(CONF_OPTIONS) + values = cfg.get(CONF_VALUES) + initial = cfg.get(CONF_INITIAL) + icon = cfg.get(CONF_ICON) + entities.append(InputSelect(object_id, name, initial, options, values, icon)) + + if not entities: + return False + + component.async_register_entity_service( + SERVICE_SELECT_OPTION, SERVICE_SELECT_OPTION_SCHEMA, + 'async_select_option' + ) + + component.async_register_entity_service( + SERVICE_SELECT_NEXT, SERVICE_SELECT_NEXT_SCHEMA, + lambda entity, call: entity.async_offset_index(1) + ) + + component.async_register_entity_service( + SERVICE_SELECT_PREVIOUS, SERVICE_SELECT_PREVIOUS_SCHEMA, + lambda entity, call: entity.async_offset_index(-1) + ) + + component.async_register_entity_service( + SERVICE_SET_OPTIONS, SERVICE_SET_OPTIONS_SCHEMA, + 'async_set_options' + ) + + await component.async_add_entities(entities) + return True + + +class InputSelect(RestoreEntity): + """Representation of a select input.""" + + def __init__(self, object_id, name, initial, options, values, icon): + """Initialize a select input.""" + self.entity_id = ENTITY_ID_FORMAT.format(object_id) + self._name = name + self._current_option = initial + self._options = options + self._values = values + self._icon = icon + + async def async_added_to_hass(self): + """Run when entity about to be added.""" + await super().async_added_to_hass() + if self._current_option is not None: + return + + state = await self.async_get_last_state() + if not state or state.state not in self._options: + self._current_option = self._options[0] + else: + self._current_option = state.state + + @property + def should_poll(self): + """If entity should be polled.""" + return False + + @property + def name(self): + """Return the name of the select input.""" + return self._name + + @property + def icon(self): + """Return the icon to be used for this entity.""" + return self._icon + + @property + def state(self): + """Return the state of the component.""" + return self._current_option + + @property + def state_attributes(self): + """Return the state attributes.""" + return { + ATTR_OPTIONS: self._options, + ATTR_VALUES: self._values, + } + + async def async_select_option(self, option): + """Select new option.""" + if option not in self._options: + _LOGGER.warning('Invalid option: %s (possible options: %s)', + option, ', '.join(self._options)) + return + self._current_option = option + await self.async_update_ha_state() + + async def async_offset_index(self, offset): + """Offset current index.""" + current_index = self._options.index(self._current_option) + new_index = (current_index + offset) % len(self._options) + self._current_option = self._options[new_index] + await self.async_update_ha_state() + + async def async_set_options(self, options): + """Set options.""" + self._current_option = options[0] + self._options = options + await self.async_update_ha_state() diff --git a/custom_components/input_select/__pycache__/__init__.cpython-37.pyc b/custom_components/input_select/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..46d6c49 Binary files /dev/null and b/custom_components/input_select/__pycache__/__init__.cpython-37.pyc differ diff --git a/custom_components/input_select/manifest.json b/custom_components/input_select/manifest.json new file mode 100644 index 0000000..a71fb53 --- /dev/null +++ b/custom_components/input_select/manifest.json @@ -0,0 +1,10 @@ +{ + "domain": "input_select", + "name": "Input select", + "documentation": "https://www.home-assistant.io/components/input_select", + "requirements": [], + "dependencies": [], + "codeowners": [ + "@home-assistant/core" + ] +} diff --git a/custom_components/input_select/readme b/custom_components/input_select/readme new file mode 100644 index 0000000..0579203 --- /dev/null +++ b/custom_components/input_select/readme @@ -0,0 +1,2 @@ +I have modified the input_select to my liking + diff --git a/custom_components/input_select/services.yaml b/custom_components/input_select/services.yaml new file mode 100644 index 0000000..8084e56 --- /dev/null +++ b/custom_components/input_select/services.yaml @@ -0,0 +1,22 @@ +select_next: + description: Select the next options of an input select entity. + fields: + entity_id: {description: Entity id of the input select to select the next value + for., example: input_select.my_select} +select_option: + description: Select an option of an input select entity. + fields: + entity_id: {description: Entity id of the input select to select the value., example: input_select.my_select} + option: {description: Option to be selected., example: '"Item A"'} +select_previous: + description: Select the previous options of an input select entity. + fields: + entity_id: {description: Entity id of the input select to select the previous + value for., example: input_select.my_select} +set_options: + description: Set the options of an input select entity. + fields: + entity_id: {description: Entity id of the input select to set the new options + for., example: input_select.my_select} + options: {description: Options for the input select entity., example: '["Item + A", "Item B", "Item C"]'} diff --git a/custom_components/var/__init__.py b/custom_components/var/__init__.py new file mode 100644 index 0000000..00b3831 --- /dev/null +++ b/custom_components/var/__init__.py @@ -0,0 +1,453 @@ +"""Allows the creation of generic variable entities.""" + +import logging +from typing import Union, Sequence +import asyncio +import json + +import voluptuous as vol + +from homeassistant.core import callback +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.config_validation import make_entity_service_schema +from homeassistant.const import ( + ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE, + CONF_ICON, CONF_ICON_TEMPLATE, ATTR_ENTITY_PICTURE, + CONF_ENTITY_PICTURE_TEMPLATE, ATTR_ENTITY_ID, + EVENT_HOMEASSISTANT_START, CONF_FRIENDLY_NAME_TEMPLATE, MATCH_ALL, + EVENT_STATE_CHANGED) +from homeassistant.exceptions import TemplateError +from homeassistant.helpers.entity import async_generate_entity_id +from homeassistant.helpers.entity_component import EntityComponent +from homeassistant.helpers.restore_state import RestoreEntity +from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.json import JSONEncoder +from homeassistant.components import recorder +from homeassistant.components.recorder.models import Events + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = 'var' +ENTITY_ID_FORMAT = DOMAIN + '.{}' + +CONF_INITIAL_VALUE = 'initial_value' +CONF_RESTORE = 'restore' +CONF_FORCE_UPDATE = 'force_update' +CONF_QUERY = 'query' +CONF_COLUMN = 'column' +CONF_TRACKED_ENTITY_ID = 'tracked_entity_id' +CONF_TRACKED_EVENT_TYPE = 'tracked_event_type' + +ATTR_VALUE = 'value' + +def validate_event_types(value: Union[str, Sequence]) -> Sequence[str]: + """Validate event types.""" + if value is None: + raise vol.Invalid('Event types can not be None') + if isinstance(value, str): + value = [event_type.strip() for event_type in value.split(',')] + + return [event_type for event_type in value] + +def validate_sql_select(value): + """Validate that value is a SQL SELECT query.""" + if not value.lstrip().lower().startswith('select'): + raise vol.Invalid('Only SELECT queries allowed') + return value + +SERVICE_SET = "set" +SERVICE_SET_SCHEMA = make_entity_service_schema({ + vol.Optional(ATTR_VALUE): cv.match_all, + vol.Optional(CONF_VALUE_TEMPLATE): cv.template, + vol.Optional(CONF_QUERY): vol.All(cv.string, validate_sql_select), + vol.Optional(CONF_COLUMN): cv.string, + vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string, + vol.Optional(CONF_RESTORE): cv.boolean, + vol.Optional(CONF_FORCE_UPDATE): cv.boolean, + vol.Optional(ATTR_FRIENDLY_NAME): cv.string, + vol.Optional(CONF_FRIENDLY_NAME_TEMPLATE): cv.template, + vol.Optional(CONF_ICON): cv.icon, + vol.Optional(CONF_ICON_TEMPLATE): cv.template, + vol.Optional(ATTR_ENTITY_PICTURE): cv.string, + vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template, + vol.Optional(CONF_TRACKED_ENTITY_ID): cv.entity_ids, + vol.Optional(CONF_TRACKED_EVENT_TYPE): validate_event_types, +}) + +SERVICE_UPDATE = "update" +SERVICE_UPDATE_SCHEMA = make_entity_service_schema({}) + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + cv.slug: vol.Any({ + vol.Optional(CONF_INITIAL_VALUE): cv.match_all, + vol.Optional(CONF_VALUE_TEMPLATE): cv.template, + vol.Optional(CONF_QUERY): vol.All(cv.string, validate_sql_select), + vol.Optional(CONF_COLUMN): cv.string, + vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string, + vol.Optional(CONF_RESTORE): cv.boolean, + vol.Optional(CONF_FORCE_UPDATE): cv.boolean, + vol.Optional(ATTR_FRIENDLY_NAME): cv.string, + vol.Optional(CONF_FRIENDLY_NAME_TEMPLATE): cv.template, + vol.Optional(CONF_ICON): cv.icon, + vol.Optional(CONF_ICON_TEMPLATE): cv.template, + vol.Optional(ATTR_ENTITY_PICTURE): cv.string, + vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template, + vol.Optional(CONF_TRACKED_ENTITY_ID): cv.entity_ids, + vol.Optional(CONF_TRACKED_EVENT_TYPE): validate_event_types, + }, None) + }) +}, extra=vol.ALLOW_EXTRA) + +async def async_setup(hass, config): + """Set up variables from config.""" + component = EntityComponent(_LOGGER, DOMAIN, hass) + await component.async_setup(config) + + entities = [] + + for object_id, cfg in config[DOMAIN].items(): + if not cfg: + cfg = {} + + initial_value = cfg.get(CONF_INITIAL_VALUE) + unit = cfg.get(ATTR_UNIT_OF_MEASUREMENT) + restore = cfg.get(CONF_RESTORE, True) + force_update = cfg.get(CONF_FORCE_UPDATE, False) + friendly_name = cfg.get(ATTR_FRIENDLY_NAME, object_id) + icon = cfg.get(CONF_ICON) + entity_picture = cfg.get(ATTR_ENTITY_PICTURE) + + value_template = cfg.get(CONF_VALUE_TEMPLATE) + friendly_name_template = cfg.get(CONF_FRIENDLY_NAME_TEMPLATE) + icon_template = cfg.get(CONF_ICON_TEMPLATE) + entity_picture_template = cfg.get(CONF_ENTITY_PICTURE_TEMPLATE) + for template in (value_template, + icon_template, + entity_picture_template, + friendly_name_template, + ): + if template is not None: + template.hass = hass + + manual_entity_ids = cfg.get(CONF_TRACKED_ENTITY_ID) + + tracked_entity_ids = list() + if manual_entity_ids is not None: + tracked_entity_ids = list(set(manual_entity_ids)) + + tracked_event_types = cfg.get(CONF_TRACKED_EVENT_TYPE) + if tracked_event_types is not None: + tracked_event_types = list(set(tracked_event_types)) + + query = cfg.get(CONF_QUERY) + column = cfg.get(CONF_COLUMN) + + session = hass.data[recorder.DATA_INSTANCE].get_session() + + entities.append( + Variable( + hass, + object_id, + initial_value, + value_template, + session, + query, + column, + unit, + restore, + force_update, + friendly_name, + friendly_name_template, + icon, + icon_template, + entity_picture, + entity_picture_template, + tracked_entity_ids, + tracked_event_types) + ) + + if not entities: + return False + + component.async_register_entity_service( + SERVICE_SET, SERVICE_SET_SCHEMA, + 'async_set' + ) + + component.async_register_entity_service( + SERVICE_UPDATE, SERVICE_UPDATE_SCHEMA, + 'async_force_update' + ) + + await component.async_add_entities(entities) + return True + +class Variable(RestoreEntity): + """Representation of a variable.""" + + def __init__(self, hass, object_id, initial_value, value_template, + session, query, column, unit, restore, force_update, + friendly_name, friendly_name_template, icon, + icon_template, entity_picture, entity_picture_template, + tracked_entity_ids, tracked_event_types): + """Initialize a variable.""" + self.hass = hass + self.entity_id = ENTITY_ID_FORMAT.format(object_id) + self._value = initial_value + self._initial_value = initial_value + self._value_template = value_template + self._session = session + if query is not None and not 'LIMIT' in query: + self._query = query.replace(";", " LIMIT 1;") + else: + self._query = query + self._column = column + self._unit = unit + self._restore = restore + self._force_update = force_update + self._friendly_name = friendly_name + self._friendly_name_template = friendly_name_template + self._icon = icon + self._icon_template = icon_template + self._entity_picture = entity_picture + self._entity_picture_template = entity_picture_template + self._tracked_entity_ids = tracked_entity_ids + self._stop_track_state_change = None + self._tracked_event_types = tracked_event_types + self._stop_track_events = [] + + def _is_event_in_db(self, event): + """Query the database to see if the event has been written.""" + event_id = self._session.query(Events.event_id).filter_by( + event_type=event.event_type, time_fired=event.time_fired, + event_data=json.dumps(event.data, cls=JSONEncoder)).scalar() + return event_id is not None + + def _get_variable_event_listener(self): + @callback + def listener(event): + """Update variable once monitored event fires and is recorded to the database.""" + # Skip untracked state changes + if (event.event_type == EVENT_STATE_CHANGED and + self._tracked_entity_ids is not None and + event.data['entity_id'] not in self._tracked_entity_ids): + return + + # Schedule update immediately if there is no query + if self._query is None: + self.async_schedule_update_ha_state(True) + return + + # Otherwise poll the database scheduling update once event has been committed + async def update_var(): + """Poll the database until the event shows up.""" + while not self._is_event_in_db(event): + await asyncio.sleep(1) + self.async_schedule_update_ha_state(True) + + self.hass.add_job(update_var) + return listener + + async def async_added_to_hass(self): + """Register callbacks.""" + + @callback + def variable_startup(event): + """Update variable event listeners on startup.""" + if self._tracked_entity_ids is not None: + listener = self._get_variable_event_listener() + stop = self.hass.bus.async_listen(EVENT_STATE_CHANGED, listener) + self._stop_track_state_change = stop + if self._tracked_event_types is not None: + listener = self._get_variable_event_listener() + for event_type in self._tracked_event_types: + stop = self.hass.bus.async_listen(event_type, listener) + self._stop_track_events.append(stop) + + self.hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_START, variable_startup) + + # Restore previous value on startup + await super().async_added_to_hass() + if self._restore == True: + state = await self.async_get_last_state() + if state: + self._value = state.state + + @property + def should_poll(self): + """If entity should be polled.""" + return False + + @property + def force_update(self): + """Return True if state updates should be forced. + If True, a state change will be triggered anytime the state property is + updated, not just when the value changes. + """ + return self._force_update + + @property + def name(self): + """Return the name of the variable.""" + return self._friendly_name + + @property + def icon(self): + """Return the icon to be used for this entity.""" + return self._icon + + @property + def entity_picture(self): + """Return the entity_picture to be used for this entity.""" + return self._entity_picture + + @property + def state(self): + """Return the state of the component.""" + return self._value + + @property + def unit_of_measurement(self): + """Return the unit the value is expressed in.""" + return self._unit + + @property + def _templates_dict(self): + return {'_value': self._value_template, + '_name': self._friendly_name_template, + '_icon': self._icon_template, + '_entity_picture': self._entity_picture_template} + + async def async_set(self, + value=None, + value_template=None, + query=None, + column=None, + unit=None, + restore=None, + force_update=None, + friendly_name=None, + friendly_name_template=None, + icon=None, + icon_template=None, + entity_picture=None, + entity_picture_template=None, + manual_tracked_entity_ids=None, + tracked_event_types=None): + """Set new attributes for the variable.""" + if value is not None: + self._value = value + if unit is not None: + self._unit = unit + if restore is not None: + self._restore = restore + if force_update is not None: + self._force_update = force_update + if friendly_name is not None: + self._friendly_name = friendly_name + if icon is not None: + self._icon = icon + if entity_picture is not None: + self._entity_picture = entity_picture + templates_dict = { + '_value': value_template, + '_name': friendly_name_template, + '_icon': icon_template, + '_entity_picture': entity_picture_template} + for property_name, template in templates_dict.items(): + if template is not None: + template.hass = self.hass + setattr(self, property_name, template.async_render()) + if query is not None: + self._query = query + if column is not None: + self._column = column + + tracked_entity_ids = None + if manual_tracked_entity_ids is not None: + tracked_entity_ids = manual_tracked_entity_ids + + if tracked_entity_ids is not None: + if self._stop_track_state_change: + self._stop_track_state_change() + self._tracked_entity_ids = tracked_entity_ids + listener = self._get_variable_event_listener() + stop = self.hass.bus.async_listen(EVENT_STATE_CHANGED, listener) + self._stop_track_state_change = stop + + if tracked_event_types is not None: + if self._stop_track_events: + for stop in self._stop_track_events: + stop() + self._tracked_event_types = tracked_event_types + listener = self._get_variable_event_listener() + for event_type in self._tracked_event_types: + stop = self.hass.bus.async_listen(event_type, listener) + self._stop_track_events.append(stop) + + await self.async_update_ha_state() + + async def async_force_update(self): + await self.async_update_ha_state(True) + + async def async_update(self): + """Update the state and attributes from the templates.""" + + # Run the db query + db_value = None + if self._query is not None: + import sqlalchemy + try: + result = self._session.execute(self._query) + + if not result.returns_rows or result.rowcount == 0: + _LOGGER.warning("%s returned no results", self._query) + self._state = None + return + + for res in result: + _LOGGER.debug("result = %s", res.items()) + db_value = res[self._column] + except sqlalchemy.exc.SQLAlchemyError as err: + _LOGGER.error("Error executing query %s: %s", self._query, err) + return + finally: + self._session.close() + + # Update the state and attributes from their templates + for property_name, template in self._templates_dict.items(): + if property_name != '_value' and template is None: + continue + elif property_name == '_value' and template is None and db_value is None: + continue + + try: + rendered_template = None + if template is not None: + if db_value is not None: + rendered_template = template.async_render_with_possible_json_value(db_value, None) + else: + rendered_template = template.async_render() + + if rendered_template is not None: + setattr(self, property_name, rendered_template) + elif property_name == '_value' and db_value is not None: + setattr(self, property_name, db_value) + except TemplateError as ex: + friendly_property_name = property_name[1:].replace('_', ' ') + if ex.args and ex.args[0].startswith( + "UndefinedError: 'None' has no attribute"): + # Common during HA startup - so just a warning + _LOGGER.warning('Could not render %s template %s,' + ' the state is unknown.', + friendly_property_name, self._friendly_name) + continue + + try: + setattr(self, property_name, getattr(super(), property_name)) + except AttributeError: + _LOGGER.error('Could not render %s template %s: %s', + friendly_property_name, self._friendly_name, ex) + diff --git a/custom_components/var/manifest.json b/custom_components/var/manifest.json new file mode 100644 index 0000000..ce743c2 --- /dev/null +++ b/custom_components/var/manifest.json @@ -0,0 +1,8 @@ +{ + "domain": "var", + "name": "Variable", + "documentation": "https://github.com/snarky-snark/home-assistant-variables/blob/master/README.md", + "dependencies": ["recorder"], + "codeowners": ["@snarky-snark"], + "requirements": [] +} diff --git a/custom_components/var/services.yaml b/custom_components/var/services.yaml new file mode 100644 index 0000000..ad39945 --- /dev/null +++ b/custom_components/var/services.yaml @@ -0,0 +1,49 @@ +set: + description: Set attributes of a variable entity. + fields: + entity_id: + description: Entity id of the variable whose value will be set. + example: var.x + value: + description: The new value for the variable entity. + example: 13 + value_template: + description: The new value template for the variable entity. + example: "{{ input_boolean.foo }}" + tracked_entity_id: + description: The new list of entities for the variable entity to track. + example: input_boolean.bar + tracked_event_type: + description: The new list of event types for the variable entity to track. + example: sunset + query: + description: An SQL QUERY string, should return 1 result at most. + example: "SELECT COUNT(*) AS todays_diaper_count FROM events WHERE event_type = 'diaper_event' AND time_fired BETWEEN DATETIME('now', 'start of day') and DATETIME('now');" + column: + description: The SQL COLUMN to select from the result of the SQL QUERY. + example: 'todays_diaper_count' + restore: + description: The new restore value for the variable entity. + example: false + force_update: + description: The new force_update value for the variable entity. + example: true + unit_of_measurement: + description: The new unit of measurement for the variable entity. + example: 'ounces' + icon: + description: The new icon for the variable entity. + example: 'mdi:baby-bottle-outline' + icon_template: + description: The new icon template for the variable entity. + entity_picture: + description: The new picture for the variable entity. + example: 'local/pic.png' + entity_picture_template: + description: The new picture template for the variable entity. +update: + description: Force a variable to update its state and atttributes. + fields: + entity_id: + description: Entity id of the variable that will be updated. + example: var.x diff --git a/custom_components/variable/__init__.py b/custom_components/variable/__init__.py new file mode 100644 index 0000000..dd0c48d --- /dev/null +++ b/custom_components/variable/__init__.py @@ -0,0 +1,207 @@ +import asyncio +import logging +import json + +import voluptuous as vol + +from homeassistant.const import (CONF_NAME, ATTR_ICON) +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers import template +from homeassistant.exceptions import TemplateError +from homeassistant.loader import bind_hass +from homeassistant.helpers.entity import Entity +from homeassistant.helpers.entity_component import EntityComponent +from homeassistant.helpers.restore_state import RestoreEntity + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = 'variable' +ENTITY_ID_FORMAT = DOMAIN + '.{}' + +CONF_ATTRIBUTES = "attributes" +CONF_VALUE = "value" +CONF_RESTORE = "restore" + +ATTR_VARIABLE = "variable" +ATTR_VALUE = 'value' +ATTR_VALUE_TEMPLATE = 'value_template' +ATTR_ATTRIBUTES = "attributes" +ATTR_ATTRIBUTES_TEMPLATE = "attributes_template" +ATTR_REPLACE_ATTRIBUTES = "replace_attributes" + +SERVICE_SET_VARIABLE = "set_variable" +SERVICE_SET_VARIABLE_SCHEMA = vol.Schema({ + vol.Required(ATTR_VARIABLE): cv.string, + vol.Optional(ATTR_VALUE): cv.match_all, + vol.Optional(ATTR_VALUE_TEMPLATE): cv.template, + vol.Optional(ATTR_ATTRIBUTES): dict, + vol.Optional(ATTR_ATTRIBUTES_TEMPLATE): cv.template, + vol.Optional(ATTR_REPLACE_ATTRIBUTES): cv.boolean +}) + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + cv.slug: vol.Any({ + vol.Optional(CONF_NAME): cv.string, + vol.Optional(CONF_VALUE): cv.match_all, + vol.Optional(CONF_ATTRIBUTES): dict, + vol.Optional(CONF_RESTORE): cv.boolean, + }, None) + }) +}, extra=vol.ALLOW_EXTRA) + +@bind_hass +def set_variable(hass, variable, value, value_template, attributes, attributes_template, replace_attributes): + """Set input_boolean to True.""" + hass.services.call(DOMAIN, SERVICE_SET_VARIABLE, { + ATTR_VARIABLE: variable, + ATTR_VALUE: value, + ATTR_VALUE_TEMPLATE: value_template, + ATTR_ATTRIBUTES: attributes, + ATTR_ATTRIBUTES_TEMPLATE: attributes_template, + ATTR_REPLACE_ATTRIBUTES: replace_attributes, + }) + +async def async_setup(hass, config): + """Set up variables.""" + component = EntityComponent(_LOGGER, DOMAIN, hass) + + entities = [] + + for variable_id, variable_config in config[DOMAIN].items(): + if not variable_config: + variable_config = {} + + name = variable_config.get(CONF_NAME) + value = variable_config.get(CONF_VALUE) + attributes = variable_config.get(CONF_ATTRIBUTES) + restore = variable_config.get(CONF_RESTORE, False) + + entities.append(Variable(variable_id, name, value, attributes, restore)) + + @asyncio.coroutine + def async_set_variable_service(call): + """Handle calls to the set_variable service.""" + + entity_id = ENTITY_ID_FORMAT.format(call.data.get(ATTR_VARIABLE)) + entity = component.get_entity(entity_id) + + if entity: + target_variables = [ entity ] + tasks = [variable.async_set_variable( + call.data.get(ATTR_VALUE), + call.data.get(ATTR_VALUE_TEMPLATE), + call.data.get(ATTR_ATTRIBUTES), + call.data.get(ATTR_ATTRIBUTES_TEMPLATE), + call.data.get(ATTR_REPLACE_ATTRIBUTES, False)) + for variable in target_variables] + if tasks: + yield from asyncio.wait(tasks, loop=hass.loop) + + else: + _LOGGER.warning('Failed to set unknown variable: %s', entity_id) + + hass.services.async_register( + DOMAIN, SERVICE_SET_VARIABLE, async_set_variable_service, + schema=SERVICE_SET_VARIABLE_SCHEMA) + + await component.async_add_entities(entities) + return True + +class Variable(RestoreEntity): + """Representation of a variable.""" + + def __init__(self, variable_id, name, value, attributes, restore): + """Initialize a variable.""" + self.entity_id = ENTITY_ID_FORMAT.format(variable_id) + self._name = name + self._value = value + self._attributes = attributes + self._restore = restore + + async def async_added_to_hass(self): + """Run when entity about to be added.""" + await super().async_added_to_hass() + if self._restore == True: + state = await self.async_get_last_state() + if state: + self._value = state.state + + @property + def should_poll(self): + """If entity should be polled.""" + return False + + @property + def name(self): + """Return the name of the variable.""" + return self._name + + @property + def icon(self): + """Return the icon to be used for this entity.""" + if self._attributes is not None: + return self._attributes.get(ATTR_ICON) + else: + return None + + @property + def state(self): + """Return the state of the component.""" + return self._value + + @property + def state_attributes(self): + """Return the state attributes.""" + return self._attributes + + @asyncio.coroutine + def async_set_variable(self, value, value_template, attributes, attributes_template, replace_attributes): + """Update variable.""" + + current_state = self.hass.states.get(self.entity_id) + updated_attributes = None + updated_value = None + + if not replace_attributes and self._attributes is not None: + updated_attributes = dict(self._attributes) + + if attributes is not None: + if updated_attributes is not None: + updated_attributes.update(attributes) + else: + updated_attributes = attributes + + elif attributes_template is not None: + attributes_template.hass = self.hass + + try: + attributes = json.loads(attributes_template.async_render({ 'variable': current_state })) + + if isinstance(attributes, dict): + if updated_attributes is not None: + updated_attributes.update(attributes) + else: + updated_attributes = attributes + + except TemplateError as ex: + _LOGGER.error('Could not render attribute_template %s: %s', + self.entity_id, ex) + + if value is not None: + updated_value = value + + elif value_template is not None: + try: + value_template.hass = self.hass + updated_value = value_template.async_render({ 'variable': current_state }) + except TemplateError as ex: + _LOGGER.error('Could not render value_template %s: %s', + self.entity_id, ex) + + self._attributes = updated_attributes; + + if updated_value is not None: + self._value = updated_value; + + yield from self.async_update_ha_state() \ No newline at end of file diff --git a/custom_components/variable/__pycache__/__init__.cpython-37.pyc b/custom_components/variable/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..9970ea6 Binary files /dev/null and b/custom_components/variable/__pycache__/__init__.cpython-37.pyc differ diff --git a/custom_components/variable/manifest.json b/custom_components/variable/manifest.json new file mode 100644 index 0000000..5dda300 --- /dev/null +++ b/custom_components/variable/manifest.json @@ -0,0 +1,10 @@ +{ + "domain": "variable", + "name": "variable", + "documentation": "https://github.com/rogro82/hass-variables", + "requirements": [], + "dependencies": [], + "codeowners": [ + "@rogro82" + ] +} \ No newline at end of file diff --git a/custom_components/variable/services.yaml b/custom_components/variable/services.yaml new file mode 100644 index 0000000..8c46598 --- /dev/null +++ b/custom_components/variable/services.yaml @@ -0,0 +1,25 @@ +# Example services.yaml entry + +set_variable: + # Description of the service + description: Update a variables value and/or its attributes. + # Different fields that your service accepts + fields: + # Key of the field + variable: + description: string (required) The name of the variable to update + value: + description: any (optional) New value to set + value_template: + description: template (optional) New value to set from a template + attributes: + description: dictionary (optional) Attributes to set or update + attributes_template: + description: template (optional) Attributes to set or update from a template ( should return a json object ) + replace_attributes: + description: boolean ( optional ) Replace or merge current attributes (default false = merge) + + + + + diff --git a/esphome/.gitignore b/esphome/.gitignore new file mode 100644 index 0000000..0bbf89c --- /dev/null +++ b/esphome/.gitignore @@ -0,0 +1,10 @@ +# Gitignore settings for ESPHome +# This is an example and may include too much for your use-case. +# You can modify this file to suit your needs. +/.esphome/ +**/.pioenvs/ +**/.piolibdeps/ +**/lib/ +**/src/ +**/platformio.ini +/secrets.yaml diff --git a/esphome/outside-temp-humd.yaml b/esphome/outside-temp-humd.yaml new file mode 100644 index 0000000..52e935e --- /dev/null +++ b/esphome/outside-temp-humd.yaml @@ -0,0 +1,37 @@ +esphome: + name: outside_temp_humd + platform: ESP8266 + board: d1_mini + +wifi: + ssid: "zoesplace" + password: "PalDgk19591960" + + # Enable fallback hotspot (captive portal) in case wifi connection fails + ap: + ssid: "Outside-Temp-Humd" + password: "1kmZsOcaCm44" + +captive_portal: + +# Enable logging +logger: + +# Enable Home Assistant API +api: + +ota: + +i2c: + sda: D2 + scl: D1 + scan: True + id: bus_a + +sensor: + - platform: dht12 + temperature: + name: "Outside Temperature" + humidity: + name: "Outside Humidity" + update_interval: 60s diff --git a/lovelace/cameras.yaml b/lovelace/cameras.yaml new file mode 100644 index 0000000..75e8b4c --- /dev/null +++ b/lovelace/cameras.yaml @@ -0,0 +1,10 @@ +title: Video Cameras +icon: mdi:video +cards: + - type: markdown + content: > + ## Roo Cam 1 + - type: picture + image: http://doggie_cam_1.kebler.net + # - type: picture-entity + # entity: camera.roo_cam_1 diff --git a/lovelace/closet.yaml b/lovelace/closet.yaml new file mode 100644 index 0000000..54bfd3b --- /dev/null +++ b/lovelace/closet.yaml @@ -0,0 +1,12 @@ +title: Electronics Closet +name: Closet +icon: mdi:door +cards: + - type: entities + show_header_toggle: false + title: Temperature Control + entities: + - sensor.closet_temperature + - input_number.fan_on_temp + - switch.fan_automation_enabled + - fan.fan_state diff --git a/lovelace/fio.yaml b/lovelace/fio.yaml new file mode 100644 index 0000000..fc77801 --- /dev/null +++ b/lovelace/fio.yaml @@ -0,0 +1,33 @@ +title: UCI Base FIO Example +cards: + - type: vertical-stack + cards: + - type: markdown + content: > + # Four In One UCI Base Example + - type: horizontal-stack + cards: + - type: entity-button + entity: switch.fio_switch_1 + icon: mdi:water + name: Switch 1 + tap_action: + action: toggle + - type: entity-button + entity: switch.fio_switch_2 + icon: mdi:water + name: Switch 2 + tap_action: + action: toggle + - type: entity-button + entity: switch.fio_switch_3 + icon: mdi:water + name: Switch 3 + tap_action: + action: toggle + - type: entity-button + entity: switch.fio_switch_4 + icon: mdi:water + name: Switch 4 + tap_action: + action: toggle diff --git a/lovelace/gios.yaml b/lovelace/gios.yaml new file mode 100644 index 0000000..9e4ec53 --- /dev/null +++ b/lovelace/gios.yaml @@ -0,0 +1,59 @@ +title: UCI Gpio Example +cards: +- type: vertical-stack + cards: + - type: markdown + content: > + # Control GPIO Relays on Pine64 + - type: horizontal-stack + cards: + - type: entity-button + entity: switch.pine64_switch_1 + icon: mdi:water + name: Relay 1 + tap_action: + action: toggle + - type: entity-button + entity: switch.pine64_switch_2 + icon: mdi:water + name: Relay 2 + tap_action: + action: toggle + - type: entity-button + entity: switch.pine64_switch_3 + icon: mdi:water + name: Relay 3 + tap_action: + action: toggle + - type: entity-button + entity: switch.pine64_switch_4 + icon: mdi:water + name: Relay 4 + tap_action: + action: toggle + - type: horizontal-stack + cards: + - type: entity-button + entity: switch.pine64_switch_5 + icon: mdi:water + name: Relay 5 + tap_action: + action: toggle + - type: entity-button + entity: switch.pine64_switch_6 + icon: mdi:water + name: Relay 6 + tap_action: + action: toggle + - type: entity-button + entity: switch.pine64_switch_7 + icon: mdi:water + name: Relay 7 + tap_action: + action: toggle + - type: entity-button + entity: switch.pine64_switch_8 + icon: mdi:water + name: Relay 8 + tap_action: + action: toggle diff --git a/lovelace/irrigation.yaml b/lovelace/irrigation.yaml new file mode 100644 index 0000000..ef3a9eb --- /dev/null +++ b/lovelace/irrigation.yaml @@ -0,0 +1,43 @@ +# --------------- IRRIGATION ------------- +title: Irrigation +icon: mdi:water +cards: + - type: vertical-stack + cards: + - type: markdown + content: > + # Irrigation + - type: vertical-stack + cards: + - type: vertical-stack + cards: + - type: markdown + content: > + ## Front Yard North + - type: entities + entities: + - entity: sensor.zone_1_last_run + icon: mdi:clock + - entity: sensor.zone_1_repeat + icon: mdi:timer + - entity: sensor.zone_1_next_run + icon: mdi:clock + - type: horizontal-stack + cards: + - type: entity-button + entity: switch.zone_1 + icon: mdi:water + name: on/off + tap_action: + action: toggle + - type: entity-button + entity: switch.zone_1_timer + icon: mdi:timer + name: timer + tap_action: + action: toggle + - type: entity-button + name: set + entity: group.zone_1_set + tap_action: + action: more-info diff --git a/lovelace/lights.yaml b/lovelace/lights.yaml new file mode 100644 index 0000000..51dcc52 --- /dev/null +++ b/lovelace/lights.yaml @@ -0,0 +1,21 @@ +title: Lights +cards: + - type: entities + show_header_toggle: false + title: Raw Sonoff Control + entities: + - switch.sonoff_white + - switch.sonoff_brown + - switch.all_sonoffs + - type: entities + show_header_toggle: false + title: Indoor Security Lights + entities: + - entity: switch.all_sonoffs + name: Lights State (manual override) + - entity: input_datetime.security_lights_interior_time_off + name: Time Of Day To Turn Off (24HR) + - entity: automation.security_lights_interior_time_on + name: Lights On At Sunset Automation Enabled + - entity: automation.security_lights_interior_time_off + name: Lights Off Automation Enabled diff --git a/lovelace/node_testing.yaml b/lovelace/node_testing.yaml new file mode 100644 index 0000000..adf25e7 --- /dev/null +++ b/lovelace/node_testing.yaml @@ -0,0 +1,12 @@ +title: Node Testing +cards: + - type: vertical-stack + cards: + # - type: horizontal-stack + - type: entities + entities: + - entity: input_number.node_test_first_number + - entity: input_number.node_test_second_number + - entity: variable.node_test_sum + - entity: variable.node_test_switch + name: Dummy Switch (on>100) diff --git a/lovelace/scheduler.yaml b/lovelace/scheduler.yaml new file mode 100644 index 0000000..1dccc9b --- /dev/null +++ b/lovelace/scheduler.yaml @@ -0,0 +1,43 @@ +# Exampe Scheduler +title: Scheduler +cards: + - type: markdown + content: > + ## Scheduler Example + - type: vertical-stack + cards: + - type: entities + entities: + # - entity: input_datetime.test_schedule_base + - type: section + label: 'Base Time of Day' + - entity: input_number.test_schedule_base_hour + - entity: input_number.test_schedule_base_minute + - type: divider + - entity: input_select.test_schedule_repeatin + # - entity: variable.test_schedule_countdown + # - entity: variable.test_schedule_next_timestamp + - type: divider + - entity: variable.test_schedule_next + - entity: sensor.test_schedule_next + - entity: variable.test_schedule_countdown + - type: horizontal-stack + cards: + - type: entity-button + name: Enable Scheduler + icon: mdi:timer + tap_action: + action: call-service + service: script.turn_on + service_data: + entity_id: script.enable_test_schedule + entity: script.enable_test_schedule + - type: entity-button + name: Disable Scheduler + icon: mdi:timer + tap_action: + action: call-service + service: script.turn_on + service_data: + entity_id: script.disable_test_schedule + entity: script.disable_test_schedule diff --git a/lovelace/temps.yaml b/lovelace/temps.yaml new file mode 100644 index 0000000..9828a06 --- /dev/null +++ b/lovelace/temps.yaml @@ -0,0 +1,24 @@ +title: Temps - Humidity +icon: mdi:thermometer-lines +cards: + - type: entities + show_header_toggle: false + title: Outside + entities: + - sensor.outside_temperature + - sensor.outside_humidity + - type: weather-forecast + entity: weather.praire_city_weather + # - type: custom:banner-card + # heading: + # - mdi:door + # - Closet + # background: "#B0C2ED" + # link: /lovelace/2 + # entities: + # - entity: sensor.closet_temperature + # name: Temperature + # - entity: input_number.fan_on_temp + # name: Set Temperature + # - entity: fan.fan_state + # name: Fan State diff --git a/lovelace/timer.yaml b/lovelace/timer.yaml new file mode 100644 index 0000000..8eb1bf1 --- /dev/null +++ b/lovelace/timer.yaml @@ -0,0 +1,30 @@ +title: Timer +cards: + - type: vertical-stack + cards: + - type: horizontal-stack + cards: + - type: entity-button + name: start timer countdown + icon: mdi:timer + tap_action: + action: call-service + service: script.turn_on + service_data: + entity_id: script.start_test_timer + entity: script.start_test_timer + - type: entity-button + name: abort timer + icon: mdi:timer-off + tap_action: + action: call-service + service: script.turn_on + service_data: + entity_id: script.abort_test_timer + entity: script.abort_test_timer + - type: entities + entities: + - entity: variable.test_timer_countdown + - entity: variable.test_timer_device + name: dummy device + - entity: input_number.test_timer_duration diff --git a/lovelace/uci-lighting.yaml b/lovelace/uci-lighting.yaml new file mode 100644 index 0000000..e06d773 --- /dev/null +++ b/lovelace/uci-lighting.yaml @@ -0,0 +1,20 @@ +title: UCI Lighting System +icon: mdi:lightbulb +cards: + - type: glance + entities: + - entity: light.panic + icon: mdi:lightbulb + name: Panic + tap_action: + action: toggle + - entity: sensor.panic_on_percent + icon: mdi:flash + name: 'circuits on' + show_state: true + - type: glance + entities: + - entity: switch.circuit_1 + name: Circuit 1 + tap_action: + action: toggle diff --git a/packages/.gitignore b/packages/.gitignore new file mode 100644 index 0000000..4490a31 --- /dev/null +++ b/packages/.gitignore @@ -0,0 +1 @@ +/archive/ diff --git a/packages/aa_system.yaml b/packages/aa_system.yaml new file mode 100644 index 0000000..6b91c36 --- /dev/null +++ b/packages/aa_system.yaml @@ -0,0 +1,30 @@ +homeassistant: + name: 238 McHaley + # latitude: !secret latitude_loc_home + # longitude: !secret longitude_loc_home + # elevation: !secret evelation_loc_home + time_zone: America/Los_Angeles + # temperature_unit: F + # unit_system: metric + # customize_glob: !include config/customize_glob.yaml +# enables system health component +websocket_api: +system_health: + # Enables the frontend +frontend: +# Enables configuration UI +config: + # Show links to resources in log and frontend +# introduction: +# Discover some devices automatically +#discovery: +# Allows you to issue voice commands from the frontend in enabled browsers +#conversation: +# Enables support for tracking state changes over time +#history: +# View all events in a logbook +#logbook: +# Enables a map showing the location of tracked devices +#map: +# Track the sun +sun: diff --git a/packages/cameras.yaml.off b/packages/cameras.yaml.off new file mode 100644 index 0000000..4840ed5 --- /dev/null +++ b/packages/cameras.yaml.off @@ -0,0 +1,4 @@ +camera: + - platform: generic + name: Roo Cam 1 + stream_source: "http://doggie_cam_1.kebler.net" diff --git a/packages/closet.yaml b/packages/closet.yaml new file mode 100644 index 0000000..a7db8d8 --- /dev/null +++ b/packages/closet.yaml @@ -0,0 +1,70 @@ +homeassistant: + customize: + fan.fan_state: + friendly_name: 'Fan State (manual override)' + sensor.closet_temperature: + friendly_name: 'Closet Temperature (C)' + input_number.fan_on_temp: + friendly_name: 'Fan On Set Temperature (C)' + +# CLOSET ENTITIES +fan: + - platform: mqtt + name: "fan state" + state_topic: "closet/status/fan/" + command_topic: "closet/fan" + +switch: + - platform: mqtt + name: "Fan Automation Enabled" + state_topic: "closet/status/fan/automation" + command_topic: "closet/fan/automation" + state_on: "enabled" + state_off: "disabled" + payload_on: "enable" + payload_off: "disable" + icon: mdi:fan + +sensor: + - platform: mqtt + name: "closet temperature" + state_topic: "closet/status/temp" + unit_of_measurement: 'Celcius' + +# Example configuration.yaml entry using 'input_number' in an action in an automation +input_number: + fan_on_temp: + name: Fan On Temp + initial: 25 + min: 20 + max: 30 + step: 0.5 + unit_of_measurement: Celcuis + icon: mdi:target + mode: "box" + +# This automation script runs when a value is received via MQTT on retained topic: setTemperature +# It sets the value slider on the GUI. This slides also had its own automation when the value is changed. +automation: + - alias: Closet Fan On Set Temperature slider + trigger: + platform: mqtt + topic: 'closet/status/fan/automation/temp' + action: + service: input_number.set_value + data_template: + entity_id: input_number.fan_on_temp + value: "{{ trigger.payload }}" + +# This second automation script runs when the target temperature slider is moved. +# It publishes its value to the same MQTT topic it is also subscribed to. + - alias: Closet Fan Temp Slider Moved + trigger: + platform: state + entity_id: input_number.fan_on_temp + action: + service: mqtt.publish + data_template: + topic: 'closet/fan/automation/temp' + retain: true + payload: "{{ states('input_number.fan_on_temp') }}" diff --git a/packages/indoor_security_light.yaml b/packages/indoor_security_light.yaml new file mode 100644 index 0000000..356b5ca --- /dev/null +++ b/packages/indoor_security_light.yaml @@ -0,0 +1,29 @@ +input_datetime: + security_lights_interior_time_off: + has_time: true + initial: '22:30' + + # + # security_lights_interior_on_offset: + # has_time: true + # initial: ':00' + +automation: + - alias: security_lights_interior_time_off + trigger: + - platform: template + value_template: "{{ states('sensor.time') == states('input_datetime.security_lights_interior_time_off')[0:5] }}" + action: + - service: switch.turn_off + data: + entity_id: group.sonoffs + + - alias: security_lights_interior_time_on + trigger: + platform: sun + event: sunset + offset: "-00:30:00" + action: + - service: switch.turn_on + data: + entity_id: group.sonoffs diff --git a/packages/node_test.yaml b/packages/node_test.yaml new file mode 100644 index 0000000..9aa0caf --- /dev/null +++ b/packages/node_test.yaml @@ -0,0 +1,23 @@ +# values to store within HA +variable: + node_test_sum: + value: 0 +# dummy device value to turn on and off + node_test_switch: + value: 'OFF' + attributes: + icon: mdi:flash + +input_number: + node_test_first_number: + name: "First Number" + initial: 25 + min: 0 + max: 100 + step: 1 + node_test_second_number: + name: "Second Number" + initial: 75 + min: 0 + max: 100 + step: 1 diff --git a/packages/scheduler.yaml b/packages/scheduler.yaml new file mode 100644 index 0000000..4ae900f --- /dev/null +++ b/packages/scheduler.yaml @@ -0,0 +1,203 @@ + +# values to store within HA +variable: + test_schedule_countdown: + value: 0 + attributes: + icon: mdi:clock + test_schedule_next: + value: 0 + +# input_boolean: +# test_schedule_enable: + +input_number: + test_schedule_base_hour: + name: "Hour of Day" + unit_of_measurement: hours + initial: 6 + min: 0 + max: 24 + step: 1 + icon: mdi:camera-timer + test_schedule_base_minute: + name: "Minute of Day" + unit_of_measurement: minutes + initial: 0 + min: 0 + max: 45 + step: 15 + icon: mdi:camera-timer + +# use sensor.test_schedule_repeatin to get corresponding value for option +input_select: + test_schedule_repeatin: + name: Repeat Every + icon: mdi:repeat + initial: 12 Hours + options: + - 3 Hours + - 6 Hours + - 12 Hours + - Day + - Every Other Day + - Every Third Day + - Every Week + values: + - 3 + - 6 + - 12 + - 24 + - 48 + - 72 + - 168 + +sensor: + - platform: template # Derived variables + sensors: + # format the next timestamp for humans + test_schedule_next: + friendly_name: Next run to start at + entity_id: variable.test_schedule_next_timestamp + value_template: '{{ states.variable.test_schedule_next.state | int | timestamp_custom("%A, %d %h %H:%M") }}' + test_schedule_countdown: + friendly_name: Countdown to next run + entity_id: variable.test_schedule_countdown + # value_template: '{{ states.variable.test_schedule_countdown.state | int | timestamp_custom("%A, %d %h %H:%M") }}' + value_template: >- + {% set time = states.variable.test_schedule_countdown.state | int %} + {% set minutes = ((time % 3600) / 60) | int %} + {% set hours = ((time % 86400) / 3600) | int %} + {% set days = (time / 86400) | int %} + {{time}} seconds is {{ days }}:{{ hours }}:{{minutes}} (D:H:M) + test_schedule_delta: + entity_id: input_select.test_schedule_repeatin + unit_of_measurement: 'hours' + value_template: > + {% for option in state_attr("input_select.test_schedule_repeatin", "options") -%} + {% if is_state("input_select.test_schedule_repeatin", option) -%} + {{ state_attr("input_select.test_schedule_repeatin", 'values')[loop.index - 1] }} + {%- endif %} + {%- endfor %} + + +switch: + - platform: mqtt + name: "Scheduler Test Solenoid" + state_topic: "status/irrigation/zone_1" + command_topic: "set/irrigation/zone_1" + payload_on: "ON" + payload_off: "OFF" + qos: 0 + retain: true + + +script: + enable_test_schedule: + sequence: + - event: ENABLE_SCHEDULE + - service: variable.set_variable + data: + variable: test_schedule_countdown + value_template: '{{ states.variable.test_schedule_next_timestamp.state | int - as_timestamp(now()) }}' + # value_template: "{{ as_timestamp(now()) }}" + # - service: variable.set_variable + # data: + # variable: test_schedule_dummy_device + # value: 'ON' + # - service: automation.turn_on + # entity_id: automation.test_schedule_countdown + # - service: automation.turn_on + # data: + # entity_id: automation.test_timer_zero_trigger + disable_test_schedule: + sequence: + - service: automation.turn_off + data: + entity_id: automation.test_schedule_countdown + - service: variable.set_variable + data: + variable: test_schedule_countdown + value: 0 + + +automation: + - alias: test_schedule_countdown + initial_state: false + trigger: + platform: time_pattern + seconds: '/1' + action: + - service: variable.set_variable + data: + variable: test_schedule_countdown + value_template: '{{ [((variable.state | int) - 1), 0] | max }}' + # - alias: test_schedule_changed + # # initial_state: false + # trigger: + # platform: state + # action: + # - service: variable.set_variable + # data: + # variable: test_schedule_countdown + # value_template: '{{ states.variable.test_schedule_next_timestamp.state | int - as_timestamp(now()) }}' + + + # - service: automation.turn_off + # data: + # entity_id: automation.test_timer_zero_trigger + # - service: variable.set_variable + # data: + # variable: test_timer_device + # value: 'OFF' + +# +# + +# - alias: test_timer_zero_trigger +# initial_state: false +# trigger: +# platform: numeric_state +# entity_id: variable.test_timer_countdown +# below: 1 +# action: +# - service: automation.turn_off +# entity_id: automation.test_timer_countdown +# - service: variable.set_variable +# data: +# variable: test_timer_device +# value: 'OFF' +# # + + + + + + # return corresponding value from input_select option of same name + # test_schedule_delta: + # entity_id: input_select.test_schedule_repeatin + # value_template: > + # {% for option in state_attr("input_select.test_schedule_repeatin", "options") -%} + # {% if is_state("input_select.test_schedule_repeatin", option) -%} + # {{ state_attr("input_select.test_schedule_repeatin", 'values')[loop.index - 1] }} + # {%- endif %} + # {%- endfor %} + # test_schedule_base_timestamp: + # entity_id: input_datetime.test_schedule_base + # value_template: > + # {{ + # as_timestamp(now()) + # - ( now().second + now().minute | int * 60 + now().hour | int * 3600 ) + # + state_attr('input_datetime.test_schedule_base','hour')|int * 3600 + # + state_attr('input_datetime.test_schedule_base','minute') * 60 + # }} + # value_template: "{{ (state_attr('input_datetime.test_schedule_base','hour')|int * 3600 + state_attr('input_datetime.test_schedule_base','minute')|int * 60) | timestamp_custom('%A, %d %h %H:%M') }}" + # value_template: > + # "{{ (state_attr('input_datetime.test_schedule_base','hour')|int * 3600 + + # state_attr('input_datetime.test_schedule_base','minute')|int * 60 + + # as_timestamp(now())) + # | timestamp_custom("%A, %d %h %H:%M") }}" + + # value_template: "{{ states(input_datetime.test_schedule_base) }}" +# {{ as_timestamp(now()) + as_timestamp(states.input_datetime.test_schedule_base) }} +# {{ as_timestamp(now()) + as_timestamp(states.input_datetime.test_schedule_base) }} diff --git a/packages/sonoff.yaml b/packages/sonoff.yaml new file mode 100644 index 0000000..866ef58 --- /dev/null +++ b/packages/sonoff.yaml @@ -0,0 +1,85 @@ + +homeassistant: + customize: + switch.all_sonoffs: + friendly_name: "All Sonoffs on Network" + +switch: + - platform: mqtt + name: "Sonoff White" + state_topic: "stat/sonoff/white/RESULT" + value_template: "{{ value_json.POWER }}" + command_topic: "cmnd/sonoff/white/POWER" + # availability_topic: "tele/sonoff/STATE" + qos: 1 + payload_on: "ON" + payload_off: "OFF" + # payload_available: "Online" + # payload_not_available: "Offline" + retain: true + + - platform: mqtt + name: "Sonoff Brown" + state_topic: "stat/sonoff/brown/RESULT" + value_template: "{{ value_json.POWER }}" + command_topic: "cmnd/sonoff/brown/POWER" + # availability_topic: "tele/sonoff/STATE" + qos: 1 + payload_on: "ON" + payload_off: "OFF" + # payload_available: "Online" + # payload_not_available: "Offline" + retain: true + + - platform: template + switches: + all_sonoffs: + value_template: "{{ is_state('group.sonoffs','on') }}" + turn_on: + - service: homeassistant.turn_on + entity_id: group.sonoffs + turn_off: + - service: homeassistant.turn_off + entity_id: group.sonoffs + +group: + sonoffs: + entities: + - switch.sonoff_white + - switch.sonoff_brown + + +automation: + - alias: “Power state on HA start-up” + trigger: + platform: homeassistant + event: start + action: + - service: mqtt.publish + data: + topic: “cmd/sonoff/white/state” + payload: “” + - service: mqtt.publish + data: + topic: cmd/sonoff/white/POWER + + +# automation: +# - alias: "Power State Sonoffs" +# trigger: +# platform: homeassistant +# event: start +# action: +# - service: mqtt.publish +# data: +# topic: "cmnd/sonoff/white/state" +# payload: "" +# - service: mqtt.publish +# data: +# topic: cmnd/tasmotas/POWER +# - service: mqtt.publish +# data: +# topic: cmnd/tasmotas/POWER2 +# - service: mqtt.publish +# data: +# topic: cmnd/tasmotas/POWER3 diff --git a/packages/timer.yaml b/packages/timer.yaml new file mode 100644 index 0000000..8e25d4a --- /dev/null +++ b/packages/timer.yaml @@ -0,0 +1,117 @@ + +# values to store within HA +variable: + test_timer_countdown: + value: 0 + attributes: + icon: mdi:timer +# dummy device value to turn on and off + test_timer_device: + value: 'OFF' + attributes: + icon: mdi:flash + +input_number: + test_timer_duration: + name: "Duration" + unit_of_measurement: seconds + initial: 3 + min: 1 + max: 15 + step: 1 + icon: mdi:camera-timer + +script: + start_test_timer: + sequence: + - service: variable.set_variable + data: + variable: test_timer_countdown + value_template: '{{ states.input_number.test_timer_duration.state | int }}' + - service: variable.set_variable + data: + variable: test_timer_device + value: 'ON' + - service: automation.turn_on + data: + entity_id: automation.test_timer_countdown + - service: automation.turn_on + data: + entity_id: automation.test_timer_zero_trigger + + + abort_test_timer: + sequence: + - service: automation.turn_off + data: + entity_id: automation.test_timer_countdown + - service: automation.turn_off + data: + entity_id: automation.test_timer_zero_trigger + - service: variable.set_variable + data: + variable: test_timer_device + value: 'OFF' + - service: persistent_notification.create + data: + message: "Timer was Aborted" + title: "ALERT" + +# +automation: + - alias: test_timer_zero_trigger + initial_state: false + trigger: + platform: numeric_state + entity_id: variable.test_timer_countdown + below: 1 + action: + - service: automation.turn_off + entity_id: automation.test_timer_countdown + - service: variable.set_variable + data: + variable: test_timer_device + value: 'OFF' +# + - alias: test_timer_countdown + initial_state: false + trigger: + platform: time_pattern + seconds: '/1' + action: + - service: variable.set_variable + data: + variable: test_timer_countdown + value_template: '{{ [((variable.state | int) - 1), 0] | max }}' + +# lovelace ui view for timer, copy into ui-lovelace.yaml as a view (lovelace in yaml mode) + # - title: Timer + # cards: + # - type: vertical-stack + # cards: + # - type: horizontal-stack + # cards: + # - type: entity-button + # name: start timer countdown + # icon: mdi:timer + # tap_action: + # action: call-service + # service: script.turn_on + # service_data: + # entity_id: script.start_test_timer + # entity: script.start_test_timer + # - type: entity-button + # name: abort timer + # icon: mdi:timer-off + # tap_action: + # action: call-service + # service: script.turn_on + # service_data: + # entity_id: script.abort_test_timer + # entity: script.abort_test_timer + # - type: entities + # entities: + # - entity: variable.test_timer_countdown + # - entity: variable.test_timer_device + # name: dummy device + # - entity: input_number.test_timer_duration diff --git a/packages/uci_base_fio.yaml b/packages/uci_base_fio.yaml new file mode 100644 index 0000000..54089aa --- /dev/null +++ b/packages/uci_base_fio.yaml @@ -0,0 +1,73 @@ +# package of switches to interface the the fio (four in one) socket example of @uci/base +switch: + - platform: mqtt + name: "FIO Switch 1" + state_topic: "fio/switch/status/1" + command_topic: "fio/switch/set/1" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "FIO Switch 2" + state_topic: "fio/switch/status/2" + command_topic: "fio/switch/set/2" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "FIO Switch 3" + state_topic: "fio/switch/status/3" + command_topic: "fio/switch/set/3" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "FIO Switch 4" + state_topic: "fio/switch/status/4" + command_topic: "fio/switch/set/4" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + + # copy and paste and uncomment below as a view under views: in ui-lovelace.yaml + # - title: UCI Base FIO Example + # cards: + # - type: vertical-stack + # cards: + # - type: markdown + # content: > + # # Four In One UCI Base Example + # - type: horizontal-stack + # cards: + # - type: entity-button + # entity: switch.fio_switch_1 + # icon: mdi:water + # name: Switch 1 + # tap_action: + # action: toggle + # - type: entity-button + # entity: switch.fio_switch_2 + # icon: mdi:water + # name: Switch 2 + # tap_action: + # action: toggle + # - type: entity-button + # entity: switch.fio_switch_3 + # icon: mdi:water + # name: Switch 3 + # tap_action: + # action: toggle + # - type: entity-button + # entity: switch.fio_switch_4 + # icon: mdi:water + # name: Switch 4 + # tap_action: + # action: toggle diff --git a/packages/uci_gpio_example.yaml b/packages/uci_gpio_example.yaml new file mode 100644 index 0000000..bda5003 --- /dev/null +++ b/packages/uci_gpio_example.yaml @@ -0,0 +1,78 @@ +# package of switches to test gpio pins/relays for @uci/gpio example +# gpio pins on pine64 in order for relays 1-8 const PINS = [80,73,69,230,229,75,74,70] +switch: + - platform: mqtt + name: "pine64 Switch 1" + state_topic: "relay/status/80" + command_topic: "relay/set/80" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "pine64 Switch 2" + state_topic: "relay/status/73" + command_topic: "relay/set/73" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "pine64 Switch 3" + state_topic: "relay/status/69" + command_topic: "relay/set/69" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "pine64 Switch 4" + state_topic: "relay/status/230" + command_topic: "relay/set/230" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "pine64 Switch 5" + state_topic: "relay/status/229" + command_topic: "relay/set/229" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "pine64 Switch 6" + state_topic: "relay/status/75" + command_topic: "relay/set/75" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "pine64 Switch 7" + state_topic: "relay/status/74" + command_topic: "relay/set/74" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + - platform: mqtt + name: "pine64 Switch 8" + state_topic: "relay/status/70" + command_topic: "relay/set/70" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + icon: mdi:lightbulb + + + # copy and paste and uncomment below as a view under views: in ui-lovelace.yaml diff --git a/packages/uci_lights.yaml b/packages/uci_lights.yaml new file mode 100644 index 0000000..eacf3d1 --- /dev/null +++ b/packages/uci_lights.yaml @@ -0,0 +1,37 @@ +homeassistant: + customize: + fan.fan_state: + friendly_name: 'Fan State (manual override)' + sensor.closet_temperature: + friendly_name: 'Closet Temperature (C)' + input_number.fan_on_temp: + friendly_name: 'Fan On Set Temperature (C)' + +switch: + - platform: mqtt + name: "Circuit 1" + state_topic: "lighting/status/circuits/x1-Erof" + command_topic: "lighting/set/circuits/x1-Erof" + state_on: "on" + state_off: "off" + payload_on: "on" + payload_off: "off" + +light: + - platform: mqtt + name: "Panic" + command_topic: "lighting/set/switches/panic-xr2R" + state_topic: "lighting/status/switches/panic-xr2R" + # brightness_state_topic: "lighting/status/switches/panic-xr2R" + # brightness_command_topic: "bogus" + # brightness_scale: 100 + qos: 0 + payload_on: "on" + payload_off: "off" + optimistic: true + +sensor: + - platform: mqtt + name: "Panic On Percent" + unit_of_measurement: '%' + state_topic: "lighting/status/switches/panic-xr2R" diff --git a/packages/weather.yaml b/packages/weather.yaml new file mode 100644 index 0000000..ee28807 --- /dev/null +++ b/packages/weather.yaml @@ -0,0 +1,34 @@ +weather: + - platform: darksky + api_key: 21b1870b9e52cf1ce234798ddfd0fbce + name: Praire City Weather + mode: daily + + +sensor: + - platform: darksky + api_key: 21b1870b9e52cf1ce234798ddfd0fbce + name: forecast + forecast: + - 0 + - 2 + - 3 + hourly_forecast: + - 0 + - 1 + - 2 + - 3 + monitored_conditions: + - summary + - icon + - temperature + - dew_point + - wind_speed + - wind_bearing + - cloud_cover + - temperature_high + - temperature_low + - moon_phase + - sunset_time + - alerts + scan_interval: 00:10 diff --git a/packages/zone1.yaml b/packages/zone1.yaml new file mode 100644 index 0000000..b9c6aa4 --- /dev/null +++ b/packages/zone1.yaml @@ -0,0 +1,204 @@ +sensor: + - platform: template + sensors: +# values used for run + zone_1_base_time: + value_template: '{{ "{:02d}".format(states.input_number.zone_1_timer_hours.state|int) }}:{{ "{:02d}".format(states.input_number.zone_1_timer_minutes.state|int) }}' + friendly_name: "Base TOD HH:MM" + zone_1_duration: + value_template: '{{states.input_number.zone_1_duration.state | int}}mins' + friendly_name: "Duration" + zone_1_repeat: + value_template: '{{ "{:02d}".format(states.input_number.zone_1_repeat.state|int)}} hrs' + friendly_name: "Repeat in" +# run computed/state values + zone_1_time_delta: + friendly_name: "Next Run (secs)" + value_template: '{{states.input_number.zone_1_repeat.state|int * 3600}}' + zone_1_next_run: + friendly_name: "Next Run" + value_template: '{{states.sensor.zone_1_timer_next.state}}' + zone_1_last_run: + friendly_name: "Last Run" + value_template: '{{ (as_timestamp(states.switch.zone_1.last_changed)) | timestamp_custom("%A, %d %h %H:%M") }}' + + # zone_1_rain_sensor: + # friendly_name: "Rainfall Threshold Sensitivity" + # value_template: >- + # {% if states.sensor.pws_precip_1d.state <= states.input_number.pws_precip_1d_sensitivity.state and states.sensor.pws_precip_today_metric.state <= states.input_number.pws_precip_today_metric_sensitivity.state %} + # dry + # {% else %} + # too wet + # {% endif %} + +input_number: + zone_1_timer_minutes: + name: "Minutes" + initial: 0 + min: 0 + max: 55 + step: 5 + icon: mdi:timer + zone_1_timer_hours: + name: "Hour" + initial: 6 + min: 0 + max: 23 + step: 1 + icon: mdi:timer + zone_1_duration: + name: "Duration" + initial: 3 + min: 1 + max: 15 + step: 1 + icon: mdi:camera-timer + zone_1_repeat: + name: "Repeat Every - HH" + initial: 24 + min: 0 + max: 48 + step: 6 + icon: mdi:repeat +# # +# # +group: + zone_1_status: + icon: mdi:clock + entities: + - sensor.zone_1_last_run + - sensor.zone_1_next_run + # - sensor.zone_1_duration + # - sensor.zone_1_repeat + zone_1_set: + icon: mdi:settings + entities: + - sensor.zone_1_base_time + - input_number.zone_1_timer_hours + - input_number.zone_1_timer_minutes + - input_number.zone_1_duration + - input_number.zone_1_repeat + #- input_boolean.zone_1_reset_zone + +# Zone switch, trigger, and run script + +sensor mqtt: + - platform: mqtt + name: "Zone 1 Timer Next" + state_topic: "run/irrigation/zone1" + +input_boolean: + zone_1_reset_zone: + name: Reset Next Run Timer + initial: off + icon: mdi:lock-reset + +switch mqtt: + - platform: mqtt + name: "Zone 1" + state_topic: "status/irrigation/zone_1" + command_topic: "set/irrigation/zone_1" + payload_on: "ON" + payload_off: "OFF" + qos: 0 + retain: true + +switch: + - platform: mqtt + name: "Zone 1 Timer" + state_topic: "status/irrigation/timer/zone_1" + command_topic: "set/irrigation/timer/zone_1" + state_on: "enabled" + state_off: "disabled" + payload_on: "enable" + payload_off: "disable" + icon: mdi:timer + +automation zone_1_trigger: + alias: "Trigger Zone 1 Run" + trigger: + - platform: time_pattern + minutes: '/1' + condition: + condition: and + conditions: + - condition: template + value_template: '{{(as_timestamp(now()) | timestamp_custom("%A, %d %h %H:%M")) == states.sensor.zone_1_next_run.state}}' + # - condition: state + # entity_id: sensor.rain_sensor + # state: 'dry' + action: + - service: script.turn_on + entity_id: script.zone_1_run + +script: + zone_1_run: + sequence: + - alias: "Switch on Zone 1" + service: switch.turn_on + entity_id: switch.zone_1 + - delay: '00:{{ states.input_number.zone_1_duration.state | int }}:00' + - alias: "Switch off Zone 1" + service: switch.turn_off + entity_id: switch.zone_1 + - alias: "Update Next Run Time" + service: mqtt.publish + data: + topic: "run/irrigation/zone1" + retain: 1 + payload_template: '{{ (as_timestamp(states.switch.zone_1.last_changed) + states.sensor.zone_1_time_delta.state | int) | timestamp_custom("%A, %d %h %H:%M") }}' + + + +# automation zone_1_2: +# alias: "Zone 1 Active Notification" +# hide_entity: False +# trigger: +# - platform: state +# entity_id: switch.zone_1 +# from: 'off' +# to: 'on' +# action: +# - service: notify.pushbullet +# data: +# title: "Irrigation Zone 1" +# message: "Watering has started" +# +# automation zone_1_3: +# alias: "Zone 1 Completed Notification" +# hide_entity: False +# trigger: +# - platform: state +# entity_id: switch.zone_1 +# from: 'on' +# to: 'off' +# action: +# - service: notify.pushbullet +# data: +# title: "Irrigation Zone 1" +# message: "Watering has completed" +# +# automation zone_1_4: +# alias: "Zone 1 Timer Reset" +# trigger: +# - platform: state +# entity_id: input_boolean.reset_zone_1 +# from: 'off' +# to: 'on' +# action: +# - service: mqtt.publish +# data: +# topic: "cmnd/zone_1_control/TIMER" +# retain: 1 +# payload_template: >- +# {%if now().strftime("%H:%M") > states.sensor.zone_1_timer.state %} +# {{(as_timestamp(now() )+24*3600 ) | timestamp_custom("%A, %d %h ")}}{{states.sensor.zone_1_timer.state}} +# {%else%} +# {{(as_timestamp(now() ) ) | timestamp_custom("%A, %d %h ")}}{{states.sensor.zone_1_timer.state}} +# {%endif%} +# - delay: +# seconds: 1 +# - service: input_boolean.turn_off +# data: +# entity_id: input_boolean.reset_zone_1 +# diff --git a/ui-lovelace.yaml b/ui-lovelace.yaml new file mode 100644 index 0000000..afeaa43 --- /dev/null +++ b/ui-lovelace.yaml @@ -0,0 +1,13 @@ +title: 238 McHaley +# custom cards registration +resources: + - url: /hacsfiles/banner-card/banner-card.js + type: module + - url: /hacsfiles/button-card/button-card.js + type: module + +views: + - !include /config/lovelace/uci-lighting.yaml + - !include /config/lovelace/temps.yaml + - !include /config/lovelace/closet.yaml + - !include /config/lovelace/lights.yaml diff --git a/www/community/banner-card/.gitignore b/www/community/banner-card/.gitignore new file mode 100644 index 0000000..dcbcbde --- /dev/null +++ b/www/community/banner-card/.gitignore @@ -0,0 +1 @@ +*.js.gz diff --git a/www/community/banner-card/banner-card.js b/www/community/banner-card/banner-card.js new file mode 100644 index 0000000..1f6b096 --- /dev/null +++ b/www/community/banner-card/banner-card.js @@ -0,0 +1,4 @@ +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).BannerCard=e()}(this,function(){"use strict";const t=new WeakMap,e=e=>"function"==typeof e&&t.has(e),i=void 0!==window.customElements&&void 0!==window.customElements.polyfillWrapFlushCallback,s=(t,e,i=null)=>{let s=e;for(;s!==i;){const e=s.nextSibling;t.removeChild(s),s=e}},n={},r={},o=`{{lit-${String(Math.random()).slice(2)}}}`,a=`\x3c!--${o}--\x3e`,c=new RegExp(`${o}|${a}`),l="$lit$";class h{constructor(t,e){this.parts=[],this.element=e;let i=-1,s=0;const n=[],r=e=>{const a=e.content,h=document.createTreeWalker(a,133,null,!1);let d=0;for(;h.nextNode();){i++;const e=h.currentNode;if(1===e.nodeType){if(e.hasAttributes()){const n=e.attributes;let r=0;for(let t=0;t=0&&r++;for(;r-- >0;){const n=t.strings[s],r=p.exec(n)[2],o=r.toLowerCase()+l,a=e.getAttribute(o).split(c);this.parts.push({type:"attribute",index:i,name:r,strings:a}),e.removeAttribute(o),s+=a.length-1}}"TEMPLATE"===e.tagName&&r(e)}else if(3===e.nodeType){const t=e.data;if(t.indexOf(o)>=0){const r=e.parentNode,o=t.split(c),a=o.length-1;for(let t=0;t-1!==t.index,u=()=>document.createComment(""),p=/([ \x09\x0a\x0c\x0d])([^\0-\x1F\x7F-\x9F \x09\x0a\x0c\x0d"'>=/]+)([ \x09\x0a\x0c\x0d]*=[ \x09\x0a\x0c\x0d]*(?:[^ \x09\x0a\x0c\x0d"'`<>=]*|"[^"]*|'[^']*))$/;class m{constructor(t,e,i){this._parts=[],this.template=t,this.processor=e,this.options=i}update(t){let e=0;for(const i of this._parts)void 0!==i&&i.setValue(t[e]),e++;for(const t of this._parts)void 0!==t&&t.commit()}_clone(){const t=i?this.template.element.content.cloneNode(!0):document.importNode(this.template.element.content,!0),e=this.template.parts;let s=0,n=0;const r=t=>{const i=document.createTreeWalker(t,133,null,!1);let o=i.nextNode();for(;snull===t||!("object"==typeof t||"function"==typeof t);class g{constructor(t,e,i){this.dirty=!0,this.element=t,this.name=e,this.strings=i,this.parts=[];for(let t=0;tthis.handleEvent(t)}setValue(t){this._pendingValue=t}commit(){for(;e(this._pendingValue);){const t=this._pendingValue;this._pendingValue=n,t(this)}if(this._pendingValue===n)return;const t=this._pendingValue,i=this.value,s=null==t||null!=i&&(t.capture!==i.capture||t.once!==i.once||t.passive!==i.passive),r=null!=t&&(null==i||s);s&&this.element.removeEventListener(this.eventName,this._boundHandleEvent,this._options),r&&(this._options=C(t),this.element.addEventListener(this.eventName,this._boundHandleEvent,this._options)),this.value=t,this._pendingValue=n}handleEvent(t){"function"==typeof this.value?this.value.call(this.eventContext||this.element,t):this.value.handleEvent(t)}}const C=t=>t&&(x?{capture:t.capture,passive:t.passive,once:t.once}:t.capture);const N=new class{handleAttributeExpressions(t,e,i,s){const n=e[0];if("."===n){return new w(t,e.slice(1),i).parts}return"@"===n?[new P(t,e.slice(1),s.eventContext)]:"?"===n?[new b(t,e.slice(1),i)]:new g(t,e,i).parts}handleTextExpression(t){return new _(t)}};function $(t){let e=k.get(t.type);void 0===e&&(e={stringsArray:new WeakMap,keyString:new Map},k.set(t.type,e));let i=e.stringsArray.get(t.strings);if(void 0!==i)return i;const s=t.strings.join(o);return void 0===(i=e.keyString.get(s))&&(i=new h(t,t.getTemplateElement()),e.keyString.set(s,i)),e.stringsArray.set(t.strings,i),i}const k=new Map,A=new WeakMap;(window.litHtmlVersions||(window.litHtmlVersions=[])).push("1.0.0");const E=(t,...e)=>new f(t,e,"html",N),T=133;function V(t,e){const{element:{content:i},parts:s}=t,n=document.createTreeWalker(i,T,null,!1);let r=O(s),o=s[r],a=-1,c=0;const l=[];let h=null;for(;n.nextNode();){a++;const t=n.currentNode;for(t.previousSibling===h&&(h=null),e.has(t)&&(l.push(t),null===h&&(h=t)),null!==h&&c++;void 0!==o&&o.index===a;)o.index=null!==h?-1:o.index-c,o=s[r=O(s,r)]}l.forEach(t=>t.parentNode.removeChild(t))}const z=t=>{let e=11===t.nodeType?0:1;const i=document.createTreeWalker(t,T,null,!1);for(;i.nextNode();)e++;return e},O=(t,e=-1)=>{for(let i=e+1;i`${t}--${e}`;let M=!0;void 0===window.ShadyCSS?M=!1:void 0===window.ShadyCSS.prepareTemplateDom&&(console.warn("Incompatible ShadyCSS version detected.Please update to at least @webcomponents/webcomponentsjs@2.0.2 and@webcomponents/shadycss@1.3.1."),M=!1);const U=t=>e=>{const i=j(e.type,t);let s=k.get(i);void 0===s&&(s={stringsArray:new WeakMap,keyString:new Map},k.set(i,s));let n=s.stringsArray.get(e.strings);if(void 0!==n)return n;const r=e.strings.join(o);if(void 0===(n=s.keyString.get(r))){const i=e.getTemplateElement();M&&window.ShadyCSS.prepareTemplateDom(i,t),n=new h(e,i),s.keyString.set(r,n)}return s.stringsArray.set(e.strings,n),n},R=["html","svg"],q=new Set,I=(t,e,i)=>{q.add(i);const s=t.querySelectorAll("style");if(0===s.length)return void window.ShadyCSS.prepareTemplateStyles(e.element,i);const n=document.createElement("style");for(let t=0;t{R.forEach(e=>{const i=k.get(j(e,t));void 0!==i&&i.keyString.forEach(t=>{const{element:{content:e}}=t,i=new Set;Array.from(e.querySelectorAll("style")).forEach(t=>{i.add(t)}),V(t,i)})})})(i),function(t,e,i=null){const{element:{content:s},parts:n}=t;if(null==i)return void s.appendChild(e);const r=document.createTreeWalker(s,T,null,!1);let o=O(n),a=0,c=-1;for(;r.nextNode();){for(c++,r.currentNode===i&&(a=z(e),i.parentNode.insertBefore(e,i));-1!==o&&n[o].index===c;){if(a>0){for(;-1!==o;)n[o].index+=a,o=O(n,o);return}o=O(n,o)}}}(e,n,e.element.content.firstChild),window.ShadyCSS.prepareTemplateStyles(e.element,i),window.ShadyCSS.nativeShadow){const i=e.element.content.querySelector("style");t.insertBefore(i.cloneNode(!0),t.firstChild)}else{e.element.content.insertBefore(n,e.element.content.firstChild);const t=new Set;t.add(n),V(e,t)}};window.JSCompiler_renameProperty=(t,e)=>t;const F={toAttribute(t,e){switch(e){case Boolean:return t?"":null;case Object:case Array:return null==t?t:JSON.stringify(t)}return t},fromAttribute(t,e){switch(e){case Boolean:return null!==t;case Number:return null===t?null:Number(t);case Object:case Array:return JSON.parse(t)}return t}},H=(t,e)=>e!==t&&(e==e||t==t),B={attribute:!0,type:String,converter:F,reflect:!1,hasChanged:H},D=Promise.resolve(!0),L=1,W=4,J=8,Y=16,G=32,K="finalized";class Q extends HTMLElement{constructor(){super(),this._updateState=0,this._instanceProperties=void 0,this._updatePromise=D,this._hasConnectedResolver=void 0,this._changedProperties=new Map,this._reflectingProperties=void 0,this.initialize()}static get observedAttributes(){this.finalize();const t=[];return this._classProperties.forEach((e,i)=>{const s=this._attributeNameForProperty(i,e);void 0!==s&&(this._attributeToPropertyMap.set(s,i),t.push(s))}),t}static _ensureClassProperties(){if(!this.hasOwnProperty(JSCompiler_renameProperty("_classProperties",this))){this._classProperties=new Map;const t=Object.getPrototypeOf(this)._classProperties;void 0!==t&&t.forEach((t,e)=>this._classProperties.set(e,t))}}static createProperty(t,e=B){if(this._ensureClassProperties(),this._classProperties.set(t,e),e.noAccessor||this.prototype.hasOwnProperty(t))return;const i="symbol"==typeof t?Symbol():`__${t}`;Object.defineProperty(this.prototype,t,{get(){return this[i]},set(e){const s=this[t];this[i]=e,this._requestUpdate(t,s)},configurable:!0,enumerable:!0})}static finalize(){const t=Object.getPrototypeOf(this);if(t.hasOwnProperty(K)||t.finalize(),this[K]=!0,this._ensureClassProperties(),this._attributeToPropertyMap=new Map,this.hasOwnProperty(JSCompiler_renameProperty("properties",this))){const t=this.properties,e=[...Object.getOwnPropertyNames(t),..."function"==typeof Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(t):[]];for(const i of e)this.createProperty(i,t[i])}}static _attributeNameForProperty(t,e){const i=e.attribute;return!1===i?void 0:"string"==typeof i?i:"string"==typeof t?t.toLowerCase():void 0}static _valueHasChanged(t,e,i=H){return i(t,e)}static _propertyValueFromAttribute(t,e){const i=e.type,s=e.converter||F,n="function"==typeof s?s:s.fromAttribute;return n?n(t,i):t}static _propertyValueToAttribute(t,e){if(void 0===e.reflect)return;const i=e.type,s=e.converter;return(s&&s.toAttribute||F.toAttribute)(t,i)}initialize(){this._saveInstanceProperties(),this._requestUpdate()}_saveInstanceProperties(){this.constructor._classProperties.forEach((t,e)=>{if(this.hasOwnProperty(e)){const t=this[e];delete this[e],this._instanceProperties||(this._instanceProperties=new Map),this._instanceProperties.set(e,t)}})}_applyInstanceProperties(){this._instanceProperties.forEach((t,e)=>this[e]=t),this._instanceProperties=void 0}connectedCallback(){this._updateState=this._updateState|G,this._hasConnectedResolver&&(this._hasConnectedResolver(),this._hasConnectedResolver=void 0)}disconnectedCallback(){}attributeChangedCallback(t,e,i){e!==i&&this._attributeToProperty(t,i)}_propertyToAttribute(t,e,i=B){const s=this.constructor,n=s._attributeNameForProperty(t,i);if(void 0!==n){const t=s._propertyValueToAttribute(e,i);if(void 0===t)return;this._updateState=this._updateState|J,null==t?this.removeAttribute(n):this.setAttribute(n,t),this._updateState=this._updateState&~J}}_attributeToProperty(t,e){if(this._updateState&J)return;const i=this.constructor,s=i._attributeToPropertyMap.get(t);if(void 0!==s){const t=i._classProperties.get(s)||B;this._updateState=this._updateState|Y,this[s]=i._propertyValueFromAttribute(e,t),this._updateState=this._updateState&~Y}}_requestUpdate(t,e){let i=!0;if(void 0!==t){const s=this.constructor,n=s._classProperties.get(t)||B;s._valueHasChanged(this[t],e,n.hasChanged)?(this._changedProperties.has(t)||this._changedProperties.set(t,e),!0!==n.reflect||this._updateState&Y||(void 0===this._reflectingProperties&&(this._reflectingProperties=new Map),this._reflectingProperties.set(t,n))):i=!1}!this._hasRequestedUpdate&&i&&this._enqueueUpdate()}requestUpdate(t,e){return this._requestUpdate(t,e),this.updateComplete}async _enqueueUpdate(){let t,e;this._updateState=this._updateState|W;const i=this._updatePromise;this._updatePromise=new Promise((i,s)=>{t=i,e=s});try{await i}catch(t){}this._hasConnected||await new Promise(t=>this._hasConnectedResolver=t);try{const t=this.performUpdate();null!=t&&await t}catch(t){e(t)}t(!this._hasRequestedUpdate)}get _hasConnected(){return this._updateState&G}get _hasRequestedUpdate(){return this._updateState&W}get hasUpdated(){return this._updateState&L}performUpdate(){this._instanceProperties&&this._applyInstanceProperties();let t=!1;const e=this._changedProperties;try{(t=this.shouldUpdate(e))&&this.update(e)}catch(e){throw t=!1,e}finally{this._markUpdated()}t&&(this._updateState&L||(this._updateState=this._updateState|L,this.firstUpdated(e)),this.updated(e))}_markUpdated(){this._changedProperties=new Map,this._updateState=this._updateState&~W}get updateComplete(){return this._getUpdateComplete()}_getUpdateComplete(){return this._updatePromise}shouldUpdate(t){return!0}update(t){void 0!==this._reflectingProperties&&this._reflectingProperties.size>0&&(this._reflectingProperties.forEach((t,e)=>this._propertyToAttribute(e,this[e],t)),this._reflectingProperties=void 0)}updated(t){}firstUpdated(t){}}Q[K]=!0;const X="adoptedStyleSheets"in Document.prototype&&"replace"in CSSStyleSheet.prototype,Z=Symbol();class tt{constructor(t,e){if(e!==Z)throw new Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead.");this.cssText=t}get styleSheet(){return void 0===this._styleSheet&&(X?(this._styleSheet=new CSSStyleSheet,this._styleSheet.replaceSync(this.cssText)):this._styleSheet=null),this._styleSheet}toString(){return this.cssText}}(window.litElementVersions||(window.litElementVersions=[])).push("2.2.1");const et=t=>t.flat?t.flat(1/0):function t(e,i=[]){for(let s=0,n=e.length;s(t.add(e),t),new Set).forEach(t=>e.unshift(t))}else t&&e.push(t);return e}initialize(){super.initialize(),this.renderRoot=this.createRenderRoot(),window.ShadowRoot&&this.renderRoot instanceof window.ShadowRoot&&this.adoptStyles()}createRenderRoot(){return this.attachShadow({mode:"open"})}adoptStyles(){const t=this.constructor._styles;0!==t.length&&(void 0===window.ShadyCSS||window.ShadyCSS.nativeShadow?X?this.renderRoot.adoptedStyleSheets=t.map(t=>t.styleSheet):this._needsShimAdoptedStyleSheets=!0:window.ShadyCSS.ScopingShim.prepareAdoptedCssText(t.map(t=>t.cssText),this.localName))}connectedCallback(){super.connectedCallback(),this.hasUpdated&&void 0!==window.ShadyCSS&&window.ShadyCSS.styleElement(this)}update(t){super.update(t);const e=this.render();e instanceof f&&this.constructor.render(e,this.renderRoot,{scopeName:this.localName,eventContext:this}),this._needsShimAdoptedStyleSheets&&(this._needsShimAdoptedStyleSheets=!1,this.constructor._styles.forEach(t=>{const e=document.createElement("style");e.textContent=t.cssText,this.renderRoot.appendChild(e)}))}render(){}}it.finalized=!0,it.render=(t,e,i)=>{const n=i.scopeName,r=A.has(e),o=e instanceof ShadowRoot&&M&&t instanceof f,a=o&&!q.has(n),c=a?document.createDocumentFragment():e;if(((t,e,i)=>{let n=A.get(e);void 0===n&&(s(e,e.firstChild),A.set(e,n=new _(Object.assign({templateFactory:$},i))),n.appendInto(e)),n.setValue(t),n.commit()})(t,c,Object.assign({templateFactory:U(n)},i)),a){const t=A.get(c);A.delete(c),t.value instanceof m&&I(c,t.value.template,n),s(e,e.firstChild),e.appendChild(c),A.set(e,t)}!r&&o&&window.ShadyCSS.styleElement(e.host)};var st=((t,...e)=>{const i=e.reduce((e,i,s)=>e+(t=>{if(t instanceof tt)return t.cssText;if("number"==typeof t)return t;throw new Error(`Value passed to 'css' function must be a 'css' function result: ${t}. Use 'unsafeCSS' to pass non-literal values, but\n take care to ensure page security.`)})(i)+t[s+1],t[0]);return new tt(i,Z)})`:host{--bc-font-size-heading:var(--banner-card-heading-size, 3em);--bc-font-size-entity-value:var(--banner-card-entity-value-size, 1.5em);--bc-font-size-media-title:var(--banner-card-media-title-size, 0.9em);--bc-spacing:var(--banner-card-spacing, 4px);--bc-button-size:var(--banner-card-button-size, 32px);--bc-heading-color-dark:var( + --banner-card-heading-color-dark, + var(--primary-text-color) + );--bc-heading-color-light:var(--banner-card-heading-color-light, #fff)}ha-card{display:flex;flex-direction:column;align-items:center}a{cursor:pointer}paper-icon-button{width:var(--bc-button-size);height:var(--bc-button-size);padding:var(--bc-spacing)}.heading{display:flex;justify-content:center;align-items:center;font-size:var(--bc-font-size-heading);font-weight:300;cursor:pointer}ha-icon.heading-icon{--iron-icon-width:1em;--iron-icon-height:1em;margin:0 var(--bc-spacing)}.overlay-strip{background:rgba(0,0,0,.3);overflow:hidden;width:100%}.entities{padding:calc(var(--bc-spacing) * 2) 0;color:#fff;display:grid}.entity-state{display:flex;flex-direction:column;align-items:center;margin-top:var(--bc-spacing);margin-bottom:var(--bc-spacing);box-shadow:-1px 0 0 0 #fff;width:100%}.media-title{flex:1 0;overflow:hidden;font-weight:300;font-size:var(--bc-font-size-media-title);white-space:nowrap;text-overflow:ellipsis}.media-controls{display:flex;flex:0 0 calc(var(--bc-button-size) * 3)}.entity-state.expand .entity-value{width:100%}.entity-state-left{margin-right:auto;margin-left:16px}.entity-state-right{margin-left:auto;margin-right:16px}.entity-name{font-weight:700;white-space:nowrap;padding-top:calc(var(--bc-spacing) * 2);padding-bottom:calc(var(--bc-spacing) * 2)}.entity-value{display:flex;width:100%;flex:1 0;font-size:var(--bc-font-size-entity-value);align-items:center;justify-content:center}.entity-value.error{display:inline-block;word-wrap:break-word;font-size:16px;width:90%}.entity-value ha-icon{color:#fff}mwc-button{--mdc-theme-primary:white}mwc-switch{--mdc-theme-secondary:white}`;function nt({state:t,attributes:e},i=!1){return"string"==typeof i&&e.hasOwnProperty(i)?e[i]:t}function rt(t){return"object"==typeof t?(e=t,i=t=>!1===t?null:t,Object.entries(e).reduce((t,[e,s])=>({...t,[e]:i(s,e)}),{})):{entity:t};var e,i}const ot={"=":(t,e)=>e.includes(t),">":(t,e)=>t>e[0],"<":(t,e)=>t!e.includes(t)};function at(t,e){if(["string","number","boolean"].includes(typeof t))return ot["="](e,[t]);if(Array.isArray(t)){const[i,...s]=t;return ot.hasOwnProperty(i)?ot[i](e,s):ot["="](e,s)}throw new Error(`Couldn't find a valid matching strategy for '${t}'`)}var ct="banner-card";!function(t){console.info(`%c${ct}: ${t}`,"font-weight: bold")}("0.9.0");const lt=/^(mdi|hass):/;function ht(t){return"string"==typeof t&&t.match(lt)}function dt(t,e=null){return e?E`${t}`:E`${t}`}class ut extends it{static get properties(){return{config:Object,color:String,entities:Array,entityValues:Array,rowSize:Number,_hass:Object}}static get styles(){return[st]}constructor(){super(),this.config={},this.entities=[],this._hass={}}setConfig(t){if(void 0===t.heading)throw new Error("You need to define a heading");if(this.entities=(t.entities||[]).map(rt),this.config=t,this.color=t.color||function(t,e,i){if(!t||"#"!==t[0])return i;if(3===(t=t.substring(1)).length){const[e,i,s]=t;t=[e,e,i,i,s,s].join("")}if(6!==t.length)return i;const s=[parseInt(t.slice(0,2),16)/255,parseInt(t.slice(2,4),16)/255,parseInt(t.slice(4,6),16)/255],[n,r,o]=s.map(t=>t<=.03928?t/12.92:Math.pow((t+.055)/1.055,2.4));return.2126*n+.7152*r+.0722*o>.179?i:e}(t.background,"var(--bc-heading-color-light)","var(--bc-heading-color-dark)"),void 0!==t.row_size){if(t.row_size<1)throw new Error("row_size must be at least 1");this.rowSize=t.row_size}this.rowSize=this.rowSize||3}set hass(t){this._hass=t,this.entityValues=this.entities.filter(e=>(function(t,e){if(!e.hasOwnProperty(t.entity))return!1;if(t.when){const{state:i,entity:s=t.entity,attributes:n}="string"==typeof t.when?{state:t.when}:t.when,r=e[s];return!(void 0!==i&&!at(i,r.state))&&Object.entries(n||{}).every(([t,e])=>{return at(e,r.attributes[t])})}return!0})(e,t.states)).map(t=>this.parseEntity(t))}parseEntity(t){const e=this._hass;if(!e.states.hasOwnProperty(t.entity))return{...t,error:"Entity not ready"};const i=e.states[t.entity],s=i.attributes,n={};if(t.map_state&&i.state in t.map_state){const e=t.map_state[i.state],s=typeof e;"string"===s?n.value=e:"object"===s&&Object.entries(e).forEach(([t,e])=>{n[t]=e})}const r={name:s.friendly_name,state:i.state,value:nt(i,t.attribute),unit:s.unit_of_measurement,attributes:s,domain:t.entity.split(".")[0]};return s.hasOwnProperty("current_position")&&(r.state=s.current_position),{...r,...t,...n}}grid(t=1){return"full"===t||t>this.rowSize?`grid-column: span ${this.rowSize};`:`grid-column: span ${t};`}_service(t,e,i){return()=>this._hass.callService(t,e,{entity_id:i})}render(){return E`${this.renderHeading()} ${this.renderEntities()}`}renderHeading(){let t=this.config.heading;if(!1===t)return null;Array.isArray(t)||(t=[t]);return E`

this.config.link&&this.navigate(this.config.link)} style=color:${this.color}>${t.map(t=>ht(t)?E``:E`${t}`)}

`}renderEntities(){return 0===this.entityValues.length?null:E`
${this.entityValues.map(t=>{if(t.error)return E`
${dt(t.error)} ${t.entity}
`;const e={...t,onClick:()=>this.openEntityPopover(t.entity)};if(t.action)return this.renderCustom({...e,action:()=>{const{service:e,...i}=t.action,[s,n]=e.split(".");this._hass.callService(s,n,{entity_id:t.entity,...i})}});if(!t.attribute)switch(t.domain){case"light":case"switch":case"input_boolean":return this.renderAsToggle(e);case"cover":return this.renderDomainCover(e);case"media_player":return this.renderDomainMediaPlayer(e)}return this.renderDomainDefault(e)})}
`}renderValue({icon:t,value:e,image:i,action:s,click:n},r){return t||ht(e)?E``:!0===i?E``:r()}renderDomainDefault({value:t,unit:e,image:i,icon:s,name:n,size:r,onClick:o}){const a=this.renderValue({icon:s,image:i,value:t,click:o},()=>E`${t} ${e}`);return E`${dt(n,o)} ${a}`}renderCustom({value:t,unit:e,action:i,image:s,icon:n,name:r,size:o,onClick:a}){const c=this.renderValue({icon:n,image:s,value:t,click:i},()=>E`${t} ${e}`);return E`
${dt(r,a)} ${c}
`}renderDomainMediaPlayer({onClick:t,attributes:e,size:i,name:s,state:n,entity:r,domain:o}){const a="playing"===n,c=a?"media_pause":"media_play",l=[e.media_artist,e.media_title].join(" – ");return E`
${dt(s,t)}
${l}
`}renderAsToggle({onClick:t,size:e,name:i,state:s,domain:n,entity:r}){return E`
${dt(i,t)}
`}renderDomainCover({onClick:t,size:e,name:i,state:s,entity:n}){const r="closed"===s||0===s,o="open"===s||100===s;return E`
${dt(i,t)}
`}getCardSize(){return 3}navigate(t){history.pushState(null,"",t),this.fire("location-changed",{replace:!0})}openEntityPopover(t){this.fire("hass-more-info",{entityId:t})}fire(t,e,i){i=i||{},e=null==e?{}:e;const s=new Event(t,{bubbles:void 0===i.bubbles||i.bubbles,cancelable:Boolean(i.cancelable),composed:void 0===i.composed||i.composed});return s.detail=e,this.dispatchEvent(s),s}}return window.customElements.define("banner-card",ut),ut}); diff --git a/www/community/button-card/.gitignore b/www/community/button-card/.gitignore new file mode 100644 index 0000000..dcbcbde --- /dev/null +++ b/www/community/button-card/.gitignore @@ -0,0 +1 @@ +*.js.gz diff --git a/www/community/button-card/button-card.js b/www/community/button-card/button-card.js new file mode 100644 index 0000000..77be32e --- /dev/null +++ b/www/community/button-card/button-card.js @@ -0,0 +1,1749 @@ +function t(t, e, n, i) { + var s, + r = arguments.length, + a = r < 3 ? e : null === i ? i = Object.getOwnPropertyDescriptor(e, n) : i;if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) a = Reflect.decorate(t, e, n, i);else for (var o = t.length - 1; o >= 0; o--) (s = t[o]) && (a = (r < 3 ? s(a) : r > 3 ? s(e, n, a) : s(e, n)) || a);return r > 3 && a && Object.defineProperty(e, n, a), a; + /** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ +}const e = new WeakMap(), + n = t => (...n) => { + const i = t(...n);return e.set(i, !0), i; +}, + i = t => "function" == typeof t && e.has(t), + s = void 0 !== window.customElements && void 0 !== window.customElements.polyfillWrapFlushCallback, + r = (t, e, n = null) => { + for (; e !== n;) { + const n = e.nextSibling;t.removeChild(e), e = n; + } +}, + a = {}, + o = {}, + l = `{{lit-${String(Math.random()).slice(2)}}}`, + c = `\x3c!--${l}--\x3e`, + h = new RegExp(`${l}|${c}`);class u { + constructor(t, e) { + this.parts = [], this.element = e;const n = [], + i = [], + s = document.createTreeWalker(e.content, 133, null, !1);let r = 0, + a = -1, + o = 0;const { strings: c, values: { length: u } } = t;for (; o < u;) { + const t = s.nextNode();if (null !== t) { + if (a++, 1 === t.nodeType) { + if (t.hasAttributes()) { + const e = t.attributes, + { length: n } = e;let i = 0;for (let t = 0; t < n; t++) d(e[t].name, "$lit$") && i++;for (; i-- > 0;) { + const e = c[o], + n = m.exec(e)[2], + i = n.toLowerCase() + "$lit$", + s = t.getAttribute(i);t.removeAttribute(i);const r = s.split(h);this.parts.push({ type: "attribute", index: a, name: n, strings: r }), o += r.length - 1; + } + }"TEMPLATE" === t.tagName && (i.push(t), s.currentNode = t.content); + } else if (3 === t.nodeType) { + const e = t.data;if (e.indexOf(l) >= 0) { + const i = t.parentNode, + s = e.split(h), + r = s.length - 1;for (let e = 0; e < r; e++) { + let n, + r = s[e];if ("" === r) n = p();else { + const t = m.exec(r);null !== t && d(t[2], "$lit$") && (r = r.slice(0, t.index) + t[1] + t[2].slice(0, -"$lit$".length) + t[3]), n = document.createTextNode(r); + }i.insertBefore(n, t), this.parts.push({ type: "node", index: ++a }); + }"" === s[r] ? (i.insertBefore(p(), t), n.push(t)) : t.data = s[r], o += r; + } + } else if (8 === t.nodeType) if (t.data === l) { + const e = t.parentNode;null !== t.previousSibling && a !== r || (a++, e.insertBefore(p(), t)), r = a, this.parts.push({ type: "node", index: a }), null === t.nextSibling ? t.data = "" : (n.push(t), a--), o++; + } else { + let e = -1;for (; -1 !== (e = t.data.indexOf(l, e + 1));) this.parts.push({ type: "node", index: -1 }), o++; + } + } else s.currentNode = i.pop(); + }for (const l of n) l.parentNode.removeChild(l); + } +}const d = (t, e) => { + const n = t.length - e.length;return n >= 0 && t.slice(n) === e; +}, + f = t => -1 !== t.index, + p = () => document.createComment(""), + m = /([ \x09\x0a\x0c\x0d])([^\0-\x1F\x7F-\x9F "'>=/]+)([ \x09\x0a\x0c\x0d]*=[ \x09\x0a\x0c\x0d]*(?:[^ \x09\x0a\x0c\x0d"'`<>=]*|"[^"]*|'[^']*))$/; +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ +class g { + constructor(t, e, n) { + this.__parts = [], this.template = t, this.processor = e, this.options = n; + }update(t) { + let e = 0;for (const n of this.__parts) void 0 !== n && n.setValue(t[e]), e++;for (const n of this.__parts) void 0 !== n && n.commit(); + }_clone() { + const t = s ? this.template.element.content.cloneNode(!0) : document.importNode(this.template.element.content, !0), + e = [], + n = this.template.parts, + i = document.createTreeWalker(t, 133, null, !1);let r, + a = 0, + o = 0, + l = i.nextNode();for (; a < n.length;) if (r = n[a], f(r)) { + for (; o < r.index;) o++, "TEMPLATE" === l.nodeName && (e.push(l), i.currentNode = l.content), null === (l = i.nextNode()) && (i.currentNode = e.pop(), l = i.nextNode());if ("node" === r.type) { + const t = this.processor.handleTextExpression(this.options);t.insertAfterNode(l.previousSibling), this.__parts.push(t); + } else this.__parts.push(...this.processor.handleAttributeExpressions(l, r.name, r.strings, this.options));a++; + } else this.__parts.push(void 0), a++;return s && (document.adoptNode(t), customElements.upgrade(t)), t; + } +} +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */const b = ` ${l} `;class _ { + constructor(t, e, n, i) { + this.strings = t, this.values = e, this.type = n, this.processor = i; + }getHTML() { + const t = this.strings.length - 1;let e = "", + n = !1;for (let i = 0; i < t; i++) { + const t = this.strings[i], + s = t.lastIndexOf("\x3c!--");n = (s > -1 || n) && -1 === t.indexOf("--\x3e", s + 1);const r = m.exec(t);e += null === r ? t + (n ? b : c) : t.substr(0, r.index) + r[1] + r[2] + "$lit$" + r[3] + l; + }return e += this.strings[t], e; + }getTemplateElement() { + const t = document.createElement("template");return t.innerHTML = this.getHTML(), t; + } +} +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */const y = t => null === t || !("object" == typeof t || "function" == typeof t), + v = t => Array.isArray(t) || !(!t || !t[Symbol.iterator]);class w { + constructor(t, e, n) { + this.dirty = !0, this.element = t, this.name = e, this.strings = n, this.parts = [];for (let i = 0; i < n.length - 1; i++) this.parts[i] = this._createPart(); + }_createPart() { + return new S(this); + }_getValue() { + const t = this.strings, + e = t.length - 1;let n = "";for (let i = 0; i < e; i++) { + n += t[i];const e = this.parts[i];if (void 0 !== e) { + const t = e.value;if (y(t) || !v(t)) n += "string" == typeof t ? t : String(t);else for (const e of t) n += "string" == typeof e ? e : String(e); + } + }return n += t[e], n; + }commit() { + this.dirty && (this.dirty = !1, this.element.setAttribute(this.name, this._getValue())); + } +}class S { + constructor(t) { + this.value = void 0, this.committer = t; + }setValue(t) { + t === a || y(t) && t === this.value || (this.value = t, i(t) || (this.committer.dirty = !0)); + }commit() { + for (; i(this.value);) { + const t = this.value;this.value = a, t(this); + }this.value !== a && this.committer.commit(); + } +}class x { + constructor(t) { + this.value = void 0, this.__pendingValue = void 0, this.options = t; + }appendInto(t) { + this.startNode = t.appendChild(p()), this.endNode = t.appendChild(p()); + }insertAfterNode(t) { + this.startNode = t, this.endNode = t.nextSibling; + }appendIntoPart(t) { + t.__insert(this.startNode = p()), t.__insert(this.endNode = p()); + }insertAfterPart(t) { + t.__insert(this.startNode = p()), this.endNode = t.endNode, t.endNode = this.startNode; + }setValue(t) { + this.__pendingValue = t; + }commit() { + for (; i(this.__pendingValue);) { + const t = this.__pendingValue;this.__pendingValue = a, t(this); + }const t = this.__pendingValue;t !== a && (y(t) ? t !== this.value && this.__commitText(t) : t instanceof _ ? this.__commitTemplateResult(t) : t instanceof Node ? this.__commitNode(t) : v(t) ? this.__commitIterable(t) : t === o ? (this.value = o, this.clear()) : this.__commitText(t)); + }__insert(t) { + this.endNode.parentNode.insertBefore(t, this.endNode); + }__commitNode(t) { + this.value !== t && (this.clear(), this.__insert(t), this.value = t); + }__commitText(t) { + const e = this.startNode.nextSibling, + n = "string" == typeof (t = null == t ? "" : t) ? t : String(t);e === this.endNode.previousSibling && 3 === e.nodeType ? e.data = n : this.__commitNode(document.createTextNode(n)), this.value = t; + }__commitTemplateResult(t) { + const e = this.options.templateFactory(t);if (this.value instanceof g && this.value.template === e) this.value.update(t.values);else { + const n = new g(e, t.processor, this.options), + i = n._clone();n.update(t.values), this.__commitNode(i), this.value = n; + } + }__commitIterable(t) { + Array.isArray(this.value) || (this.value = [], this.clear());const e = this.value;let n, + i = 0;for (const s of t) n = e[i], void 0 === n && (n = new x(this.options), e.push(n), 0 === i ? n.appendIntoPart(this) : n.insertAfterPart(e[i - 1])), n.setValue(s), n.commit(), i++;i < e.length && (e.length = i, this.clear(n && n.endNode)); + }clear(t = this.startNode) { + r(this.startNode.parentNode, t.nextSibling, this.endNode); + } +}class k { + constructor(t, e, n) { + if (this.value = void 0, this.__pendingValue = void 0, 2 !== n.length || "" !== n[0] || "" !== n[1]) throw new Error("Boolean attributes can only contain a single expression");this.element = t, this.name = e, this.strings = n; + }setValue(t) { + this.__pendingValue = t; + }commit() { + for (; i(this.__pendingValue);) { + const t = this.__pendingValue;this.__pendingValue = a, t(this); + }if (this.__pendingValue === a) return;const t = !!this.__pendingValue;this.value !== t && (t ? this.element.setAttribute(this.name, "") : this.element.removeAttribute(this.name), this.value = t), this.__pendingValue = a; + } +}class M extends w { + constructor(t, e, n) { + super(t, e, n), this.single = 2 === n.length && "" === n[0] && "" === n[1]; + }_createPart() { + return new T(this); + }_getValue() { + return this.single ? this.parts[0].value : super._getValue(); + }commit() { + this.dirty && (this.dirty = !1, this.element[this.name] = this._getValue()); + } +}class T extends S {}let C = !1;try { + const t = { get capture() { + return C = !0, !1; + } };window.addEventListener("test", t, t), window.removeEventListener("test", t, t); +} catch (oe) {}class N { + constructor(t, e, n) { + this.value = void 0, this.__pendingValue = void 0, this.element = t, this.eventName = e, this.eventContext = n, this.__boundHandleEvent = t => this.handleEvent(t); + }setValue(t) { + this.__pendingValue = t; + }commit() { + for (; i(this.__pendingValue);) { + const t = this.__pendingValue;this.__pendingValue = a, t(this); + }if (this.__pendingValue === a) return;const t = this.__pendingValue, + e = this.value, + n = null == t || null != e && (t.capture !== e.capture || t.once !== e.once || t.passive !== e.passive), + s = null != t && (null == e || n);n && this.element.removeEventListener(this.eventName, this.__boundHandleEvent, this.__options), s && (this.__options = E(t), this.element.addEventListener(this.eventName, this.__boundHandleEvent, this.__options)), this.value = t, this.__pendingValue = a; + }handleEvent(t) { + "function" == typeof this.value ? this.value.call(this.eventContext || this.element, t) : this.value.handleEvent(t); + } +}const E = t => t && (C ? { capture: t.capture, passive: t.passive, once: t.once } : t.capture) +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */;const O = new class { + handleAttributeExpressions(t, e, n, i) { + const s = e[0];if ("." === s) { + return new M(t, e.slice(1), n).parts; + }return "@" === s ? [new N(t, e.slice(1), i.eventContext)] : "?" === s ? [new k(t, e.slice(1), n)] : new w(t, e, n).parts; + }handleTextExpression(t) { + return new x(t); + } +}(); +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */function P(t) { + let e = A.get(t.type);void 0 === e && (e = { stringsArray: new WeakMap(), keyString: new Map() }, A.set(t.type, e));let n = e.stringsArray.get(t.strings);if (void 0 !== n) return n;const i = t.strings.join(l);return n = e.keyString.get(i), void 0 === n && (n = new u(t, t.getTemplateElement()), e.keyString.set(i, n)), e.stringsArray.set(t.strings, n), n; +}const A = new Map(), + $ = new WeakMap(); +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ +(window.litHtmlVersions || (window.litHtmlVersions = [])).push("1.1.2");const j = (t, ...e) => new _(t, e, "html", O) +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */;function V(t, e) { + const { element: { content: n }, parts: i } = t, + s = document.createTreeWalker(n, 133, null, !1);let r = H(i), + a = i[r], + o = -1, + l = 0;const c = [];let h = null;for (; s.nextNode();) { + o++;const t = s.currentNode;for (t.previousSibling === h && (h = null), e.has(t) && (c.push(t), null === h && (h = t)), null !== h && l++; void 0 !== a && a.index === o;) a.index = null !== h ? -1 : a.index - l, r = H(i, r), a = i[r]; + }c.forEach(t => t.parentNode.removeChild(t)); +}const R = t => { + let e = 11 === t.nodeType ? 0 : 1;const n = document.createTreeWalker(t, 133, null, !1);for (; n.nextNode();) e++;return e; +}, + H = (t, e = -1) => { + for (let n = e + 1; n < t.length; n++) { + const e = t[n];if (f(e)) return n; + }return -1; +}; +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ +const D = (t, e) => `${t}--${e}`;let F = !0;void 0 === window.ShadyCSS ? F = !1 : void 0 === window.ShadyCSS.prepareTemplateDom && (console.warn("Incompatible ShadyCSS version detected. Please update to at least @webcomponents/webcomponentsjs@2.0.2 and @webcomponents/shadycss@1.3.1."), F = !1);const I = t => e => { + const n = D(e.type, t);let i = A.get(n);void 0 === i && (i = { stringsArray: new WeakMap(), keyString: new Map() }, A.set(n, i));let s = i.stringsArray.get(e.strings);if (void 0 !== s) return s;const r = e.strings.join(l);if (s = i.keyString.get(r), void 0 === s) { + const n = e.getTemplateElement();F && window.ShadyCSS.prepareTemplateDom(n, t), s = new u(e, n), i.keyString.set(r, s); + }return i.stringsArray.set(e.strings, s), s; +}, + L = ["html", "svg"], + z = new Set(), + U = (t, e, n) => { + z.add(t);const i = n ? n.element : document.createElement("template"), + s = e.querySelectorAll("style"), + { length: r } = s;if (0 === r) return void window.ShadyCSS.prepareTemplateStyles(i, t);const a = document.createElement("style");for (let c = 0; c < r; c++) { + const t = s[c];t.parentNode.removeChild(t), a.textContent += t.textContent; + }(t => { + L.forEach(e => { + const n = A.get(D(e, t));void 0 !== n && n.keyString.forEach(t => { + const { element: { content: e } } = t, + n = new Set();Array.from(e.querySelectorAll("style")).forEach(t => { + n.add(t); + }), V(t, n); + }); + }); + })(t);const o = i.content;n ? function (t, e, n = null) { + const { element: { content: i }, parts: s } = t;if (null == n) return void i.appendChild(e);const r = document.createTreeWalker(i, 133, null, !1);let a = H(s), + o = 0, + l = -1;for (; r.nextNode();) { + for (l++, r.currentNode === n && (o = R(e), n.parentNode.insertBefore(e, n)); -1 !== a && s[a].index === l;) { + if (o > 0) { + for (; -1 !== a;) s[a].index += o, a = H(s, a);return; + }a = H(s, a); + } + } + }(n, a, o.firstChild) : o.insertBefore(a, o.firstChild), window.ShadyCSS.prepareTemplateStyles(i, t);const l = o.querySelector("style");if (window.ShadyCSS.nativeShadow && null !== l) e.insertBefore(l.cloneNode(!0), e.firstChild);else if (n) { + o.insertBefore(a, o.firstChild);const t = new Set();t.add(a), V(n, t); + } +};window.JSCompiler_renameProperty = (t, e) => t;const Y = { toAttribute(t, e) { + switch (e) {case Boolean: + return t ? "" : null;case Object:case Array: + return null == t ? t : JSON.stringify(t);}return t; + }, fromAttribute(t, e) { + switch (e) {case Boolean: + return null !== t;case Number: + return null === t ? null : Number(t);case Object:case Array: + return JSON.parse(t);}return t; + } }, + q = (t, e) => e !== t && (e == e || t == t), + B = { attribute: !0, type: String, converter: Y, reflect: !1, hasChanged: q }, + W = Promise.resolve(!0);class G extends HTMLElement { + constructor() { + super(), this._updateState = 0, this._instanceProperties = void 0, this._updatePromise = W, this._hasConnectedResolver = void 0, this._changedProperties = new Map(), this._reflectingProperties = void 0, this.initialize(); + }static get observedAttributes() { + this.finalize();const t = [];return this._classProperties.forEach((e, n) => { + const i = this._attributeNameForProperty(n, e);void 0 !== i && (this._attributeToPropertyMap.set(i, n), t.push(i)); + }), t; + }static _ensureClassProperties() { + if (!this.hasOwnProperty(JSCompiler_renameProperty("_classProperties", this))) { + this._classProperties = new Map();const t = Object.getPrototypeOf(this)._classProperties;void 0 !== t && t.forEach((t, e) => this._classProperties.set(e, t)); + } + }static createProperty(t, e = B) { + if (this._ensureClassProperties(), this._classProperties.set(t, e), e.noAccessor || this.prototype.hasOwnProperty(t)) return;const n = "symbol" == typeof t ? Symbol() : `__${t}`;Object.defineProperty(this.prototype, t, { get() { + return this[n]; + }, set(e) { + const i = this[t];this[n] = e, this._requestUpdate(t, i); + }, configurable: !0, enumerable: !0 }); + }static finalize() { + const t = Object.getPrototypeOf(this);if (t.hasOwnProperty("finalized") || t.finalize(), this.finalized = !0, this._ensureClassProperties(), this._attributeToPropertyMap = new Map(), this.hasOwnProperty(JSCompiler_renameProperty("properties", this))) { + const t = this.properties, + e = [...Object.getOwnPropertyNames(t), ...("function" == typeof Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(t) : [])];for (const n of e) this.createProperty(n, t[n]); + } + }static _attributeNameForProperty(t, e) { + const n = e.attribute;return !1 === n ? void 0 : "string" == typeof n ? n : "string" == typeof t ? t.toLowerCase() : void 0; + }static _valueHasChanged(t, e, n = q) { + return n(t, e); + }static _propertyValueFromAttribute(t, e) { + const n = e.type, + i = e.converter || Y, + s = "function" == typeof i ? i : i.fromAttribute;return s ? s(t, n) : t; + }static _propertyValueToAttribute(t, e) { + if (void 0 === e.reflect) return;const n = e.type, + i = e.converter;return (i && i.toAttribute || Y.toAttribute)(t, n); + }initialize() { + this._saveInstanceProperties(), this._requestUpdate(); + }_saveInstanceProperties() { + this.constructor._classProperties.forEach((t, e) => { + if (this.hasOwnProperty(e)) { + const t = this[e];delete this[e], this._instanceProperties || (this._instanceProperties = new Map()), this._instanceProperties.set(e, t); + } + }); + }_applyInstanceProperties() { + this._instanceProperties.forEach((t, e) => this[e] = t), this._instanceProperties = void 0; + }connectedCallback() { + this._updateState = 32 | this._updateState, this._hasConnectedResolver && (this._hasConnectedResolver(), this._hasConnectedResolver = void 0); + }disconnectedCallback() {}attributeChangedCallback(t, e, n) { + e !== n && this._attributeToProperty(t, n); + }_propertyToAttribute(t, e, n = B) { + const i = this.constructor, + s = i._attributeNameForProperty(t, n);if (void 0 !== s) { + const t = i._propertyValueToAttribute(e, n);if (void 0 === t) return;this._updateState = 8 | this._updateState, null == t ? this.removeAttribute(s) : this.setAttribute(s, t), this._updateState = -9 & this._updateState; + } + }_attributeToProperty(t, e) { + if (8 & this._updateState) return;const n = this.constructor, + i = n._attributeToPropertyMap.get(t);if (void 0 !== i) { + const t = n._classProperties.get(i) || B;this._updateState = 16 | this._updateState, this[i] = n._propertyValueFromAttribute(e, t), this._updateState = -17 & this._updateState; + } + }_requestUpdate(t, e) { + let n = !0;if (void 0 !== t) { + const i = this.constructor, + s = i._classProperties.get(t) || B;i._valueHasChanged(this[t], e, s.hasChanged) ? (this._changedProperties.has(t) || this._changedProperties.set(t, e), !0 !== s.reflect || 16 & this._updateState || (void 0 === this._reflectingProperties && (this._reflectingProperties = new Map()), this._reflectingProperties.set(t, s))) : n = !1; + }!this._hasRequestedUpdate && n && this._enqueueUpdate(); + }requestUpdate(t, e) { + return this._requestUpdate(t, e), this.updateComplete; + }async _enqueueUpdate() { + let t, e;this._updateState = 4 | this._updateState;const n = this._updatePromise;this._updatePromise = new Promise((n, i) => { + t = n, e = i; + });try { + await n; + } catch (i) {}this._hasConnected || (await new Promise(t => this._hasConnectedResolver = t));try { + const t = this.performUpdate();null != t && (await t); + } catch (i) { + e(i); + }t(!this._hasRequestedUpdate); + }get _hasConnected() { + return 32 & this._updateState; + }get _hasRequestedUpdate() { + return 4 & this._updateState; + }get hasUpdated() { + return 1 & this._updateState; + }performUpdate() { + this._instanceProperties && this._applyInstanceProperties();let t = !1;const e = this._changedProperties;try { + t = this.shouldUpdate(e), t && this.update(e); + } catch (n) { + throw t = !1, n; + } finally { + this._markUpdated(); + }t && (1 & this._updateState || (this._updateState = 1 | this._updateState, this.firstUpdated(e)), this.updated(e)); + }_markUpdated() { + this._changedProperties = new Map(), this._updateState = -5 & this._updateState; + }get updateComplete() { + return this._getUpdateComplete(); + }_getUpdateComplete() { + return this._updatePromise; + }shouldUpdate(t) { + return !0; + }update(t) { + void 0 !== this._reflectingProperties && this._reflectingProperties.size > 0 && (this._reflectingProperties.forEach((t, e) => this._propertyToAttribute(e, this[e], t)), this._reflectingProperties = void 0); + }updated(t) {}firstUpdated(t) {} +}G.finalized = !0; +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ +const J = (t, e) => "method" !== e.kind || !e.descriptor || "value" in e.descriptor ? { kind: "field", key: Symbol(), placement: "own", descriptor: {}, initializer() { + "function" == typeof e.initializer && (this[e.key] = e.initializer.call(this)); + }, finisher(n) { + n.createProperty(e.key, t); + } } : Object.assign({}, e, { finisher(n) { + n.createProperty(e.key, t); + } });function Z(t) { + return (e, n) => void 0 !== n ? ((t, e, n) => { + e.constructor.createProperty(n, t); + })(t, e, n) : J(t, e); +} +/** +@license +Copyright (c) 2019 The Polymer Project Authors. All rights reserved. +This code may only be used under the BSD style license found at +http://polymer.github.io/LICENSE.txt The complete set of authors may be found at +http://polymer.github.io/AUTHORS.txt The complete set of contributors may be +found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as +part of the polymer project is also subject to an additional IP rights grant +found at http://polymer.github.io/PATENTS.txt +*/const X = "adoptedStyleSheets" in Document.prototype && "replace" in CSSStyleSheet.prototype, + K = Symbol();class Q { + constructor(t, e) { + if (e !== K) throw new Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead.");this.cssText = t; + }get styleSheet() { + return void 0 === this._styleSheet && (X ? (this._styleSheet = new CSSStyleSheet(), this._styleSheet.replaceSync(this.cssText)) : this._styleSheet = null), this._styleSheet; + }toString() { + return this.cssText; + } +} +/** + * @license + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ +(window.litElementVersions || (window.litElementVersions = [])).push("2.2.1");const tt = t => t.flat ? t.flat(1 / 0) : function t(e, n = []) { + for (let i = 0, s = e.length; i < s; i++) { + const s = e[i];Array.isArray(s) ? t(s, n) : n.push(s); + }return n; +}(t);class et extends G { + static finalize() { + super.finalize.call(this), this._styles = this.hasOwnProperty(JSCompiler_renameProperty("styles", this)) ? this._getUniqueStyles() : this._styles || []; + }static _getUniqueStyles() { + const t = this.styles, + e = [];if (Array.isArray(t)) { + tt(t).reduceRight((t, e) => (t.add(e), t), new Set()).forEach(t => e.unshift(t)); + } else t && e.push(t);return e; + }initialize() { + super.initialize(), this.renderRoot = this.createRenderRoot(), window.ShadowRoot && this.renderRoot instanceof window.ShadowRoot && this.adoptStyles(); + }createRenderRoot() { + return this.attachShadow({ mode: "open" }); + }adoptStyles() { + const t = this.constructor._styles;0 !== t.length && (void 0 === window.ShadyCSS || window.ShadyCSS.nativeShadow ? X ? this.renderRoot.adoptedStyleSheets = t.map(t => t.styleSheet) : this._needsShimAdoptedStyleSheets = !0 : window.ShadyCSS.ScopingShim.prepareAdoptedCssText(t.map(t => t.cssText), this.localName)); + }connectedCallback() { + super.connectedCallback(), this.hasUpdated && void 0 !== window.ShadyCSS && window.ShadyCSS.styleElement(this); + }update(t) { + super.update(t);const e = this.render();e instanceof _ && this.constructor.render(e, this.renderRoot, { scopeName: this.localName, eventContext: this }), this._needsShimAdoptedStyleSheets && (this._needsShimAdoptedStyleSheets = !1, this.constructor._styles.forEach(t => { + const e = document.createElement("style");e.textContent = t.cssText, this.renderRoot.appendChild(e); + })); + }render() {} +}et.finalized = !0, et.render = (t, e, n) => { + if (!n || "object" != typeof n || !n.scopeName) throw new Error("The `scopeName` option is required.");const i = n.scopeName, + s = $.has(e), + a = F && 11 === e.nodeType && !!e.host, + o = a && !z.has(i), + l = o ? document.createDocumentFragment() : e;if (((t, e, n) => { + let i = $.get(e);void 0 === i && (r(e, e.firstChild), $.set(e, i = new x(Object.assign({ templateFactory: P }, n))), i.appendInto(e)), i.setValue(t), i.commit(); + })(t, l, Object.assign({ templateFactory: I(i) }, n)), o) { + const t = $.get(l);$.delete(l);const n = t.value instanceof g ? t.value.template : void 0;U(i, l, n), r(e, e.firstChild), e.appendChild(l), $.set(e, t); + }!s && a && window.ShadyCSS.styleElement(e.host); +}; +/** + * @license + * Copyright (c) 2018 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ +const nt = new WeakMap(), + it = n(t => e => { + if (!(e instanceof S) || e instanceof T || "style" !== e.committer.name || e.committer.parts.length > 1) throw new Error("The `styleMap` directive must be used in the style attribute and must be the only part in the attribute.");const { committer: n } = e, + { style: i } = n.element;nt.has(e) || (i.cssText = n.strings.join(" "));const s = nt.get(e);for (const r in s) r in t || (-1 === r.indexOf("-") ? i[r] = null : i.removeProperty(r));for (const r in t) -1 === r.indexOf("-") ? i[r] = t[r] : i.setProperty(r, t[r]);nt.set(e, t); +}), + st = new WeakMap(), + rt = n(t => e => { + if (!(e instanceof x)) throw new Error("unsafeHTML can only be used in text bindings");const n = st.get(e);if (void 0 !== n && y(t) && t === n.value && e.value === n.fragment) return;const i = document.createElement("template");i.innerHTML = t;const s = document.importNode(i.content, !0);e.setValue(s), st.set(e, { value: t, fragment: s }); +}), + at = n(t => e => { + if (void 0 === t && e instanceof S) { + if (t !== e.value) { + const t = e.committer.name;e.committer.element.removeAttribute(t); + } + } else e.setValue(t); +}), + ot = new WeakMap(), + lt = n(t => e => { + if (!(e instanceof S) || e instanceof T || "class" !== e.committer.name || e.committer.parts.length > 1) throw new Error("The `classMap` directive must be used in the `class` attribute and must be the only part in the attribute.");const { committer: n } = e, + { element: i } = n;ot.has(e) || (i.className = n.strings.join(" "));const { classList: s } = i, + r = ot.get(e);for (const a in r) a in t || s.remove(a);for (const a in t) { + const e = t[a];if (!r || e !== r[a]) { + s[e ? "add" : "remove"](a); + } + }ot.set(e, t); +});var ct = {}, + ht = /d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g, + ut = "[^\\s]+", + dt = /\[([^]*?)\]/gm, + ft = function () {};function pt(t, e) { + for (var n = [], i = 0, s = t.length; i < s; i++) n.push(t[i].substr(0, e));return n; +}function mt(t) { + return function (e, n, i) { + var s = i[t].indexOf(n.charAt(0).toUpperCase() + n.substr(1).toLowerCase());~s && (e.month = s); + }; +}function gt(t, e) { + for (t = String(t), e = e || 2; t.length < e;) t = "0" + t;return t; +}var bt = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], + _t = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], + yt = pt(_t, 3), + vt = pt(bt, 3);ct.i18n = { dayNamesShort: vt, dayNames: bt, monthNamesShort: yt, monthNames: _t, amPm: ["am", "pm"], DoFn: function (t) { + return t + ["th", "st", "nd", "rd"][t % 10 > 3 ? 0 : (t - t % 10 != 10) * t % 10]; + } };var wt = { D: function (t) { + return t.getDate(); + }, DD: function (t) { + return gt(t.getDate()); + }, Do: function (t, e) { + return e.DoFn(t.getDate()); + }, d: function (t) { + return t.getDay(); + }, dd: function (t) { + return gt(t.getDay()); + }, ddd: function (t, e) { + return e.dayNamesShort[t.getDay()]; + }, dddd: function (t, e) { + return e.dayNames[t.getDay()]; + }, M: function (t) { + return t.getMonth() + 1; + }, MM: function (t) { + return gt(t.getMonth() + 1); + }, MMM: function (t, e) { + return e.monthNamesShort[t.getMonth()]; + }, MMMM: function (t, e) { + return e.monthNames[t.getMonth()]; + }, YY: function (t) { + return gt(String(t.getFullYear()), 4).substr(2); + }, YYYY: function (t) { + return gt(t.getFullYear(), 4); + }, h: function (t) { + return t.getHours() % 12 || 12; + }, hh: function (t) { + return gt(t.getHours() % 12 || 12); + }, H: function (t) { + return t.getHours(); + }, HH: function (t) { + return gt(t.getHours()); + }, m: function (t) { + return t.getMinutes(); + }, mm: function (t) { + return gt(t.getMinutes()); + }, s: function (t) { + return t.getSeconds(); + }, ss: function (t) { + return gt(t.getSeconds()); + }, S: function (t) { + return Math.round(t.getMilliseconds() / 100); + }, SS: function (t) { + return gt(Math.round(t.getMilliseconds() / 10), 2); + }, SSS: function (t) { + return gt(t.getMilliseconds(), 3); + }, a: function (t, e) { + return t.getHours() < 12 ? e.amPm[0] : e.amPm[1]; + }, A: function (t, e) { + return t.getHours() < 12 ? e.amPm[0].toUpperCase() : e.amPm[1].toUpperCase(); + }, ZZ: function (t) { + var e = t.getTimezoneOffset();return (e > 0 ? "-" : "+") + gt(100 * Math.floor(Math.abs(e) / 60) + Math.abs(e) % 60, 4); + } }, + St = { D: ["\\d\\d?", function (t, e) { + t.day = e; + }], Do: ["\\d\\d?" + ut, function (t, e) { + t.day = parseInt(e, 10); + }], M: ["\\d\\d?", function (t, e) { + t.month = e - 1; + }], YY: ["\\d\\d?", function (t, e) { + var n = +("" + new Date().getFullYear()).substr(0, 2);t.year = "" + (e > 68 ? n - 1 : n) + e; + }], h: ["\\d\\d?", function (t, e) { + t.hour = e; + }], m: ["\\d\\d?", function (t, e) { + t.minute = e; + }], s: ["\\d\\d?", function (t, e) { + t.second = e; + }], YYYY: ["\\d{4}", function (t, e) { + t.year = e; + }], S: ["\\d", function (t, e) { + t.millisecond = 100 * e; + }], SS: ["\\d{2}", function (t, e) { + t.millisecond = 10 * e; + }], SSS: ["\\d{3}", function (t, e) { + t.millisecond = e; + }], d: ["\\d\\d?", ft], ddd: [ut, ft], MMM: [ut, mt("monthNamesShort")], MMMM: [ut, mt("monthNames")], a: [ut, function (t, e, n) { + var i = e.toLowerCase();i === n.amPm[0] ? t.isPm = !1 : i === n.amPm[1] && (t.isPm = !0); + }], ZZ: ["[^\\s]*?[\\+\\-]\\d\\d:?\\d\\d|[^\\s]*?Z", function (t, e) { + var n, + i = (e + "").match(/([+-]|\d\d)/gi);i && (n = 60 * i[1] + parseInt(i[2], 10), t.timezoneOffset = "+" === i[0] ? n : -n); + }] };function xt(t) { + var e = t.split(":").map(Number);return 3600 * e[0] + 60 * e[1] + e[2]; +}St.dd = St.d, St.dddd = St.ddd, St.DD = St.D, St.mm = St.m, St.hh = St.H = St.HH = St.h, St.MM = St.M, St.ss = St.s, St.A = St.a, ct.masks = { default: "ddd MMM DD YYYY HH:mm:ss", shortDate: "M/D/YY", mediumDate: "MMM D, YYYY", longDate: "MMMM D, YYYY", fullDate: "dddd, MMMM D, YYYY", shortTime: "HH:mm", mediumTime: "HH:mm:ss", longTime: "HH:mm:ss.SSS" }, ct.format = function (t, e, n) { + var i = n || ct.i18n;if ("number" == typeof t && (t = new Date(t)), "[object Date]" !== Object.prototype.toString.call(t) || isNaN(t.getTime())) throw new Error("Invalid Date in fecha.format");e = ct.masks[e] || e || ct.masks.default;var s = [];return (e = (e = e.replace(dt, function (t, e) { + return s.push(e), "@@@"; + })).replace(ht, function (e) { + return e in wt ? wt[e](t, i) : e.slice(1, e.length - 1); + })).replace(/@@@/g, function () { + return s.shift(); + }); +}, ct.parse = function (t, e, n) { + var i = n || ct.i18n;if ("string" != typeof e) throw new Error("Invalid format in fecha.parse");if (e = ct.masks[e] || e, t.length > 1e3) return null;var s = {}, + r = [], + a = [];e = e.replace(dt, function (t, e) { + return a.push(e), "@@@"; + });var o, + l = (o = e, o.replace(/[|\\{()[^$+*?.-]/g, "\\$&")).replace(ht, function (t) { + if (St[t]) { + var e = St[t];return r.push(e[1]), "(" + e[0] + ")"; + }return t; + });l = l.replace(/@@@/g, function () { + return a.shift(); + });var c = t.match(new RegExp(l, "i"));if (!c) return null;for (var h = 1; h < c.length; h++) r[h - 1](s, c[h], i);var u, + d = new Date();return !0 === s.isPm && null != s.hour && 12 != +s.hour ? s.hour = +s.hour + 12 : !1 === s.isPm && 12 == +s.hour && (s.hour = 0), null != s.timezoneOffset ? (s.minute = +(s.minute || 0) - +s.timezoneOffset, u = new Date(Date.UTC(s.year || d.getFullYear(), s.month || 0, s.day || 1, s.hour || 0, s.minute || 0, s.second || 0, s.millisecond || 0))) : u = new Date(s.year || d.getFullYear(), s.month || 0, s.day || 1, s.hour || 0, s.minute || 0, s.second || 0, s.millisecond || 0), u; +};(function () { + try { + new Date().toLocaleDateString("i"); + } catch (t) { + return "RangeError" === t.name; + } +})(), function () { + try { + new Date().toLocaleString("i"); + } catch (t) { + return "RangeError" === t.name; + } +}(), function () { + try { + new Date().toLocaleTimeString("i"); + } catch (t) { + return "RangeError" === t.name; + } +}();var kt = function (t) { + return t < 10 ? "0" + t : t; +};var Mt = ["closed", "locked", "off"], + Tt = function (t, e, n, i) { + i = i || {}, n = null == n ? {} : n;var s = new Event(e, { bubbles: void 0 === i.bubbles || i.bubbles, cancelable: Boolean(i.cancelable), composed: void 0 === i.composed || i.composed });return s.detail = n, t.dispatchEvent(s), s; +}, + Ct = { alert: "hass:alert", automation: "hass:playlist-play", calendar: "hass:calendar", camera: "hass:video", climate: "hass:thermostat", configurator: "hass:settings", conversation: "hass:text-to-speech", device_tracker: "hass:account", fan: "hass:fan", group: "hass:google-circles-communities", history_graph: "hass:chart-line", homeassistant: "hass:home-assistant", homekit: "hass:home-automation", image_processing: "hass:image-filter-frames", input_boolean: "hass:drawing", input_datetime: "hass:calendar-clock", input_number: "hass:ray-vertex", input_select: "hass:format-list-bulleted", input_text: "hass:textbox", light: "hass:lightbulb", mailbox: "hass:mailbox", notify: "hass:comment-alert", person: "hass:account", plant: "hass:flower", proximity: "hass:apple-safari", remote: "hass:remote", scene: "hass:google-pages", script: "hass:file-document", sensor: "hass:eye", simple_alarm: "hass:bell", sun: "hass:white-balance-sunny", switch: "hass:flash", timer: "hass:timer", updater: "hass:cloud-upload", vacuum: "hass:robot-vacuum", water_heater: "hass:thermometer", weblink: "hass:open-in-new" };var Nt = function (t, e) { + Tt(t, "haptic", e); +}, + Et = function (t, e, n, i, s) { + var r;if (s && n.double_tap_action ? r = n.double_tap_action : i && n.hold_action ? r = n.hold_action : !i && n.tap_action && (r = n.tap_action), r || (r = { action: "more-info" }), !r.confirmation || r.confirmation.exemptions && r.confirmation.exemptions.some(function (t) { + return t.user === e.user.id; + }) || confirm(r.confirmation.text || "Are you sure you want to " + r.action + "?")) switch (r.action) {case "more-info": + (n.entity || n.camera_image) && (Tt(t, "hass-more-info", { entityId: r.entity ? r.entity : n.entity ? n.entity : n.camera_image }), r.haptic && Nt(t, r.haptic));break;case "navigate": + r.navigation_path && (function (t, e, n) { + void 0 === n && (n = !1), n ? history.replaceState(null, "", e) : history.pushState(null, "", e), Tt(window, "location-changed", { replace: n }); + }(0, r.navigation_path), r.haptic && Nt(t, r.haptic));break;case "url": + r.url_path && window.open(r.url_path), r.haptic && Nt(t, r.haptic);break;case "toggle": + n.entity && (function (t, e) { + (function (t, e, n) { + void 0 === n && (n = !0);var i, + s = function (t) { + return t.substr(0, t.indexOf(".")); + }(e), + r = "group" === s ? "homeassistant" : s;switch (s) {case "lock": + i = n ? "unlock" : "lock";break;case "cover": + i = n ? "open_cover" : "close_cover";break;default: + i = n ? "turn_on" : "turn_off";}t.callService(r, i, { entity_id: e }); + })(t, e, Mt.includes(t.states[e].state)); + }(e, n.entity), r.haptic && Nt(t, r.haptic));break;case "call-service": + if (!r.service) return;var a = r.service.split(".", 2), + o = a[0], + l = a[1], + c = Object.assign({}, r.service_data);"entity" === c.entity_id && (c.entity_id = n.entity), e.callService(o, l, c), r.haptic && Nt(t, r.haptic);} +};const Ot = "ontouchstart" in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;class Pt extends HTMLElement { + constructor() { + super(), this.holdTime = 500, this.ripple = document.createElement("paper-ripple"), this.timer = void 0, this.held = !1, this.cooldownStart = !1, this.cooldownEnd = !1, this.nbClicks = 0; + }connectedCallback() { + Object.assign(this.style, { borderRadius: "50%", position: "absolute", width: Ot ? "100px" : "50px", height: Ot ? "100px" : "50px", transform: "translate(-50%, -50%)", pointerEvents: "none" }), this.appendChild(this.ripple), this.ripple.style.color = "#03a9f4", this.ripple.style.color = "var(--primary-color)", ["touchcancel", "mouseout", "mouseup", "touchmove", "mousewheel", "wheel", "scroll"].forEach(t => { + document.addEventListener(t, () => { + clearTimeout(this.timer), this.stopAnimation(), this.timer = void 0; + }, { passive: !0 }); + }); + }bind(t) { + if (t.longPress) return;t.longPress = !0, t.addEventListener("contextmenu", t => { + const e = t || window.event;return e.preventDefault && e.preventDefault(), e.stopPropagation && e.stopPropagation(), e.cancelBubble = !0, e.returnValue = !1, !1; + });const e = e => { + if (this.cooldownStart) return;let n, i;this.held = !1, e.touches ? (n = e.touches[0].pageX, i = e.touches[0].pageY) : (n = e.pageX, i = e.pageY), this.timer = window.setTimeout(() => { + this.startAnimation(n, i), this.held = !0, t.repeat && !t.isRepeating && (t.isRepeating = !0, this.repeatTimeout = setInterval(() => { + t.dispatchEvent(new Event("ha-hold")); + }, t.repeat)); + }, this.holdTime), this.cooldownStart = !0, window.setTimeout(() => this.cooldownStart = !1, 100); + }, + n = e => { + this.cooldownEnd || ["touchend", "touchcancel"].includes(e.type) && void 0 === this.timer ? t.isRepeating && this.repeatTimeout && (clearInterval(this.repeatTimeout), t.isRepeating = !1) : (clearTimeout(this.timer), t.isRepeating && this.repeatTimeout && clearInterval(this.repeatTimeout), t.isRepeating = !1, this.stopAnimation(), this.timer = void 0, this.held ? t.repeat || t.dispatchEvent(new Event("ha-hold")) : t.hasDblClick ? 0 === this.nbClicks ? (this.nbClicks += 1, this.dblClickTimeout = window.setTimeout(() => { + 1 === this.nbClicks && (this.nbClicks = 0, t.dispatchEvent(new Event("ha-click"))); + }, 250)) : (this.nbClicks = 0, clearTimeout(this.dblClickTimeout), t.dispatchEvent(new Event("ha-dblclick"))) : t.dispatchEvent(new Event("ha-click")), this.cooldownEnd = !0, window.setTimeout(() => this.cooldownEnd = !1, 100)); + };t.addEventListener("touchstart", e, { passive: !0 }), t.addEventListener("touchend", n), t.addEventListener("touchcancel", n);const i = "MacIntel" === navigator.platform && navigator.maxTouchPoints > 1 && !window.MSStream, + s = /iPad|iPhone|iPod/.test(navigator.platform);i || s || (t.addEventListener("mousedown", e, { passive: !0 }), t.addEventListener("click", n)); + }startAnimation(t, e) { + Object.assign(this.style, { left: `${t}px`, top: `${e}px`, display: null }), this.ripple.holdDown = !0, this.ripple.simulatedRipple(); + }stopAnimation() { + this.ripple.holdDown = !1, this.style.display = "none"; + } +}customElements.define("long-press-button-card", Pt);const At = t => { + const e = (() => { + const t = document.body;if (t.querySelector("long-press-button-card")) return t.querySelector("long-press-button-card");const e = document.createElement("long-press-button-card");return t.appendChild(e), e; + })();e && e.bind(t); +}, + $t = n(() => t => { + At(t.committer.element); +});function jt(t, e) { + (function (t) { + return "string" == typeof t && t.includes(".") && 1 === parseFloat(t); + })(t) && (t = "100%");var n = function (t) { + return "string" == typeof t && t.includes("%"); + }(t);return t = 360 === e ? t : Math.min(e, Math.max(0, parseFloat(t))), n && (t = parseInt(String(t * e), 10) / 100), Math.abs(t - e) < 1e-6 ? 1 : t = 360 === e ? (t < 0 ? t % e + e : t % e) / parseFloat(String(e)) : t % e / parseFloat(String(e)); +}function Vt(t) { + return Math.min(1, Math.max(0, t)); +}function Rt(t) { + return t = parseFloat(t), (isNaN(t) || t < 0 || t > 1) && (t = 1), t; +}function Ht(t) { + return t <= 1 ? 100 * Number(t) + "%" : t; +}function Dt(t) { + return 1 === t.length ? "0" + t : String(t); +}function Ft(t, e, n) { + t = jt(t, 255), e = jt(e, 255), n = jt(n, 255);var i = Math.max(t, e, n), + s = Math.min(t, e, n), + r = 0, + a = 0, + o = (i + s) / 2;if (i === s) a = 0, r = 0;else { + var l = i - s;switch (a = o > .5 ? l / (2 - i - s) : l / (i + s), i) {case t: + r = (e - n) / l + (e < n ? 6 : 0);break;case e: + r = (n - t) / l + 2;break;case n: + r = (t - e) / l + 4;}r /= 6; + }return { h: r, s: a, l: o }; +}function It(t, e, n) { + t = jt(t, 255), e = jt(e, 255), n = jt(n, 255);var i = Math.max(t, e, n), + s = Math.min(t, e, n), + r = 0, + a = i, + o = i - s, + l = 0 === i ? 0 : o / i;if (i === s) r = 0;else { + switch (i) {case t: + r = (e - n) / o + (e < n ? 6 : 0);break;case e: + r = (n - t) / o + 2;break;case n: + r = (t - e) / o + 4;}r /= 6; + }return { h: r, s: l, v: a }; +}function Lt(t, e, n, i) { + var s = [Dt(Math.round(t).toString(16)), Dt(Math.round(e).toString(16)), Dt(Math.round(n).toString(16))];return i && s[0].charAt(0) === s[0].charAt(1) && s[1].charAt(0) === s[1].charAt(1) && s[2].charAt(0) === s[2].charAt(1) ? s[0].charAt(0) + s[1].charAt(0) + s[2].charAt(0) : s.join(""); +}function zt(t) { + return Ut(t) / 255; +}function Ut(t) { + return parseInt(t, 16); +}var Yt = { aliceblue: "#f0f8ff", antiquewhite: "#faebd7", aqua: "#00ffff", aquamarine: "#7fffd4", azure: "#f0ffff", beige: "#f5f5dc", bisque: "#ffe4c4", black: "#000000", blanchedalmond: "#ffebcd", blue: "#0000ff", blueviolet: "#8a2be2", brown: "#a52a2a", burlywood: "#deb887", cadetblue: "#5f9ea0", chartreuse: "#7fff00", chocolate: "#d2691e", coral: "#ff7f50", cornflowerblue: "#6495ed", cornsilk: "#fff8dc", crimson: "#dc143c", cyan: "#00ffff", darkblue: "#00008b", darkcyan: "#008b8b", darkgoldenrod: "#b8860b", darkgray: "#a9a9a9", darkgreen: "#006400", darkgrey: "#a9a9a9", darkkhaki: "#bdb76b", darkmagenta: "#8b008b", darkolivegreen: "#556b2f", darkorange: "#ff8c00", darkorchid: "#9932cc", darkred: "#8b0000", darksalmon: "#e9967a", darkseagreen: "#8fbc8f", darkslateblue: "#483d8b", darkslategray: "#2f4f4f", darkslategrey: "#2f4f4f", darkturquoise: "#00ced1", darkviolet: "#9400d3", deeppink: "#ff1493", deepskyblue: "#00bfff", dimgray: "#696969", dimgrey: "#696969", dodgerblue: "#1e90ff", firebrick: "#b22222", floralwhite: "#fffaf0", forestgreen: "#228b22", fuchsia: "#ff00ff", gainsboro: "#dcdcdc", ghostwhite: "#f8f8ff", gold: "#ffd700", goldenrod: "#daa520", gray: "#808080", green: "#008000", greenyellow: "#adff2f", grey: "#808080", honeydew: "#f0fff0", hotpink: "#ff69b4", indianred: "#cd5c5c", indigo: "#4b0082", ivory: "#fffff0", khaki: "#f0e68c", lavender: "#e6e6fa", lavenderblush: "#fff0f5", lawngreen: "#7cfc00", lemonchiffon: "#fffacd", lightblue: "#add8e6", lightcoral: "#f08080", lightcyan: "#e0ffff", lightgoldenrodyellow: "#fafad2", lightgray: "#d3d3d3", lightgreen: "#90ee90", lightgrey: "#d3d3d3", lightpink: "#ffb6c1", lightsalmon: "#ffa07a", lightseagreen: "#20b2aa", lightskyblue: "#87cefa", lightslategray: "#778899", lightslategrey: "#778899", lightsteelblue: "#b0c4de", lightyellow: "#ffffe0", lime: "#00ff00", limegreen: "#32cd32", linen: "#faf0e6", magenta: "#ff00ff", maroon: "#800000", mediumaquamarine: "#66cdaa", mediumblue: "#0000cd", mediumorchid: "#ba55d3", mediumpurple: "#9370db", mediumseagreen: "#3cb371", mediumslateblue: "#7b68ee", mediumspringgreen: "#00fa9a", mediumturquoise: "#48d1cc", mediumvioletred: "#c71585", midnightblue: "#191970", mintcream: "#f5fffa", mistyrose: "#ffe4e1", moccasin: "#ffe4b5", navajowhite: "#ffdead", navy: "#000080", oldlace: "#fdf5e6", olive: "#808000", olivedrab: "#6b8e23", orange: "#ffa500", orangered: "#ff4500", orchid: "#da70d6", palegoldenrod: "#eee8aa", palegreen: "#98fb98", paleturquoise: "#afeeee", palevioletred: "#db7093", papayawhip: "#ffefd5", peachpuff: "#ffdab9", peru: "#cd853f", pink: "#ffc0cb", plum: "#dda0dd", powderblue: "#b0e0e6", purple: "#800080", rebeccapurple: "#663399", red: "#ff0000", rosybrown: "#bc8f8f", royalblue: "#4169e1", saddlebrown: "#8b4513", salmon: "#fa8072", sandybrown: "#f4a460", seagreen: "#2e8b57", seashell: "#fff5ee", sienna: "#a0522d", silver: "#c0c0c0", skyblue: "#87ceeb", slateblue: "#6a5acd", slategray: "#708090", slategrey: "#708090", snow: "#fffafa", springgreen: "#00ff7f", steelblue: "#4682b4", tan: "#d2b48c", teal: "#008080", thistle: "#d8bfd8", tomato: "#ff6347", turquoise: "#40e0d0", violet: "#ee82ee", wheat: "#f5deb3", white: "#ffffff", whitesmoke: "#f5f5f5", yellow: "#ffff00", yellowgreen: "#9acd32" };function qt(t) { + var e = { r: 0, g: 0, b: 0 }, + n = 1, + i = null, + s = null, + r = null, + a = !1, + o = !1;return "string" == typeof t && (t = function (t) { + if (0 === (t = t.trim().toLowerCase()).length) return !1;var e = !1;if (Yt[t]) t = Yt[t], e = !0;else if ("transparent" === t) return { r: 0, g: 0, b: 0, a: 0, format: "name" };var n = Jt.rgb.exec(t);if (n) return { r: n[1], g: n[2], b: n[3] };if (n = Jt.rgba.exec(t)) return { r: n[1], g: n[2], b: n[3], a: n[4] };if (n = Jt.hsl.exec(t)) return { h: n[1], s: n[2], l: n[3] };if (n = Jt.hsla.exec(t)) return { h: n[1], s: n[2], l: n[3], a: n[4] };if (n = Jt.hsv.exec(t)) return { h: n[1], s: n[2], v: n[3] };if (n = Jt.hsva.exec(t)) return { h: n[1], s: n[2], v: n[3], a: n[4] };if (n = Jt.hex8.exec(t)) return { r: Ut(n[1]), g: Ut(n[2]), b: Ut(n[3]), a: zt(n[4]), format: e ? "name" : "hex8" };if (n = Jt.hex6.exec(t)) return { r: Ut(n[1]), g: Ut(n[2]), b: Ut(n[3]), format: e ? "name" : "hex" };if (n = Jt.hex4.exec(t)) return { r: Ut(n[1] + n[1]), g: Ut(n[2] + n[2]), b: Ut(n[3] + n[3]), a: zt(n[4] + n[4]), format: e ? "name" : "hex8" };if (n = Jt.hex3.exec(t)) return { r: Ut(n[1] + n[1]), g: Ut(n[2] + n[2]), b: Ut(n[3] + n[3]), format: e ? "name" : "hex" };return !1; + }(t)), "object" == typeof t && (Zt(t.r) && Zt(t.g) && Zt(t.b) ? (e = function (t, e, n) { + return { r: 255 * jt(t, 255), g: 255 * jt(e, 255), b: 255 * jt(n, 255) }; + }(t.r, t.g, t.b), a = !0, o = "%" === String(t.r).substr(-1) ? "prgb" : "rgb") : Zt(t.h) && Zt(t.s) && Zt(t.v) ? (i = Ht(t.s), s = Ht(t.v), e = function (t, e, n) { + t = 6 * jt(t, 360), e = jt(e, 100), n = jt(n, 100);var i = Math.floor(t), + s = t - i, + r = n * (1 - e), + a = n * (1 - s * e), + o = n * (1 - (1 - s) * e), + l = i % 6;return { r: 255 * [n, a, r, r, o, n][l], g: 255 * [o, n, n, a, r, r][l], b: 255 * [r, r, o, n, n, a][l] }; + }(t.h, i, s), a = !0, o = "hsv") : Zt(t.h) && Zt(t.s) && Zt(t.l) && (i = Ht(t.s), r = Ht(t.l), e = function (t, e, n) { + var i, s, r;function a(t, e, n) { + return n < 0 && (n += 1), n > 1 && (n -= 1), n < 1 / 6 ? t + 6 * n * (e - t) : n < .5 ? e : n < 2 / 3 ? t + (e - t) * (2 / 3 - n) * 6 : t; + }if (t = jt(t, 360), e = jt(e, 100), n = jt(n, 100), 0 === e) s = n, r = n, i = n;else { + var o = n < .5 ? n * (1 + e) : n + e - n * e, + l = 2 * n - o;i = a(l, o, t + 1 / 3), s = a(l, o, t), r = a(l, o, t - 1 / 3); + }return { r: 255 * i, g: 255 * s, b: 255 * r }; + }(t.h, i, r), a = !0, o = "hsl"), Object.prototype.hasOwnProperty.call(t, "a") && (n = t.a)), n = Rt(n), { ok: a, format: t.format || o, r: Math.min(255, Math.max(e.r, 0)), g: Math.min(255, Math.max(e.g, 0)), b: Math.min(255, Math.max(e.b, 0)), a: n }; +}var Bt = "(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)", + Wt = "[\\s|\\(]+(" + Bt + ")[,|\\s]+(" + Bt + ")[,|\\s]+(" + Bt + ")\\s*\\)?", + Gt = "[\\s|\\(]+(" + Bt + ")[,|\\s]+(" + Bt + ")[,|\\s]+(" + Bt + ")[,|\\s]+(" + Bt + ")\\s*\\)?", + Jt = { CSS_UNIT: new RegExp(Bt), rgb: new RegExp("rgb" + Wt), rgba: new RegExp("rgba" + Gt), hsl: new RegExp("hsl" + Wt), hsla: new RegExp("hsla" + Gt), hsv: new RegExp("hsv" + Wt), hsva: new RegExp("hsva" + Gt), hex3: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/, hex6: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/, hex4: /^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/, hex8: /^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/ };function Zt(t) { + return Boolean(Jt.CSS_UNIT.exec(String(t))); +}var Xt = function () { + function t(e, n) { + if (void 0 === e && (e = ""), void 0 === n && (n = {}), e instanceof t) return e;this.originalInput = e;var i = qt(e);this.originalInput = e, this.r = i.r, this.g = i.g, this.b = i.b, this.a = i.a, this.roundA = Math.round(100 * this.a) / 100, this.format = n.format || i.format, this.gradientType = n.gradientType, this.r < 1 && (this.r = Math.round(this.r)), this.g < 1 && (this.g = Math.round(this.g)), this.b < 1 && (this.b = Math.round(this.b)), this.isValid = i.ok; + }return t.prototype.isDark = function () { + return this.getBrightness() < 128; + }, t.prototype.isLight = function () { + return !this.isDark(); + }, t.prototype.getBrightness = function () { + var t = this.toRgb();return (299 * t.r + 587 * t.g + 114 * t.b) / 1e3; + }, t.prototype.getLuminance = function () { + var t = this.toRgb(), + e = t.r / 255, + n = t.g / 255, + i = t.b / 255;return .2126 * (e <= .03928 ? e / 12.92 : Math.pow((e + .055) / 1.055, 2.4)) + .7152 * (n <= .03928 ? n / 12.92 : Math.pow((n + .055) / 1.055, 2.4)) + .0722 * (i <= .03928 ? i / 12.92 : Math.pow((i + .055) / 1.055, 2.4)); + }, t.prototype.getAlpha = function () { + return this.a; + }, t.prototype.setAlpha = function (t) { + return this.a = Rt(t), this.roundA = Math.round(100 * this.a) / 100, this; + }, t.prototype.toHsv = function () { + var t = It(this.r, this.g, this.b);return { h: 360 * t.h, s: t.s, v: t.v, a: this.a }; + }, t.prototype.toHsvString = function () { + var t = It(this.r, this.g, this.b), + e = Math.round(360 * t.h), + n = Math.round(100 * t.s), + i = Math.round(100 * t.v);return 1 === this.a ? "hsv(" + e + ", " + n + "%, " + i + "%)" : "hsva(" + e + ", " + n + "%, " + i + "%, " + this.roundA + ")"; + }, t.prototype.toHsl = function () { + var t = Ft(this.r, this.g, this.b);return { h: 360 * t.h, s: t.s, l: t.l, a: this.a }; + }, t.prototype.toHslString = function () { + var t = Ft(this.r, this.g, this.b), + e = Math.round(360 * t.h), + n = Math.round(100 * t.s), + i = Math.round(100 * t.l);return 1 === this.a ? "hsl(" + e + ", " + n + "%, " + i + "%)" : "hsla(" + e + ", " + n + "%, " + i + "%, " + this.roundA + ")"; + }, t.prototype.toHex = function (t) { + return void 0 === t && (t = !1), Lt(this.r, this.g, this.b, t); + }, t.prototype.toHexString = function (t) { + return void 0 === t && (t = !1), "#" + this.toHex(t); + }, t.prototype.toHex8 = function (t) { + return void 0 === t && (t = !1), function (t, e, n, i, s) { + var r, + a = [Dt(Math.round(t).toString(16)), Dt(Math.round(e).toString(16)), Dt(Math.round(n).toString(16)), Dt((r = i, Math.round(255 * parseFloat(r)).toString(16)))];return s && a[0].charAt(0) === a[0].charAt(1) && a[1].charAt(0) === a[1].charAt(1) && a[2].charAt(0) === a[2].charAt(1) && a[3].charAt(0) === a[3].charAt(1) ? a[0].charAt(0) + a[1].charAt(0) + a[2].charAt(0) + a[3].charAt(0) : a.join(""); + }(this.r, this.g, this.b, this.a, t); + }, t.prototype.toHex8String = function (t) { + return void 0 === t && (t = !1), "#" + this.toHex8(t); + }, t.prototype.toRgb = function () { + return { r: Math.round(this.r), g: Math.round(this.g), b: Math.round(this.b), a: this.a }; + }, t.prototype.toRgbString = function () { + var t = Math.round(this.r), + e = Math.round(this.g), + n = Math.round(this.b);return 1 === this.a ? "rgb(" + t + ", " + e + ", " + n + ")" : "rgba(" + t + ", " + e + ", " + n + ", " + this.roundA + ")"; + }, t.prototype.toPercentageRgb = function () { + var t = function (t) { + return Math.round(100 * jt(t, 255)) + "%"; + };return { r: t(this.r), g: t(this.g), b: t(this.b), a: this.a }; + }, t.prototype.toPercentageRgbString = function () { + var t = function (t) { + return Math.round(100 * jt(t, 255)); + };return 1 === this.a ? "rgb(" + t(this.r) + "%, " + t(this.g) + "%, " + t(this.b) + "%)" : "rgba(" + t(this.r) + "%, " + t(this.g) + "%, " + t(this.b) + "%, " + this.roundA + ")"; + }, t.prototype.toName = function () { + if (0 === this.a) return "transparent";if (this.a < 1) return !1;for (var t = "#" + Lt(this.r, this.g, this.b, !1), e = 0, n = Object.keys(Yt); e < n.length; e++) { + var i = n[e];if (Yt[i] === t) return i; + }return !1; + }, t.prototype.toString = function (t) { + var e = Boolean(t);t = t || this.format;var n = !1, + i = this.a < 1 && this.a >= 0;return e || !i || !t.startsWith("hex") && "name" !== t ? ("rgb" === t && (n = this.toRgbString()), "prgb" === t && (n = this.toPercentageRgbString()), "hex" !== t && "hex6" !== t || (n = this.toHexString()), "hex3" === t && (n = this.toHexString(!0)), "hex4" === t && (n = this.toHex8String(!0)), "hex8" === t && (n = this.toHex8String()), "name" === t && (n = this.toName()), "hsl" === t && (n = this.toHslString()), "hsv" === t && (n = this.toHsvString()), n || this.toHexString()) : "name" === t && 0 === this.a ? this.toName() : this.toRgbString(); + }, t.prototype.clone = function () { + return new t(this.toString()); + }, t.prototype.lighten = function (e) { + void 0 === e && (e = 10);var n = this.toHsl();return n.l += e / 100, n.l = Vt(n.l), new t(n); + }, t.prototype.brighten = function (e) { + void 0 === e && (e = 10);var n = this.toRgb();return n.r = Math.max(0, Math.min(255, n.r - Math.round(-e / 100 * 255))), n.g = Math.max(0, Math.min(255, n.g - Math.round(-e / 100 * 255))), n.b = Math.max(0, Math.min(255, n.b - Math.round(-e / 100 * 255))), new t(n); + }, t.prototype.darken = function (e) { + void 0 === e && (e = 10);var n = this.toHsl();return n.l -= e / 100, n.l = Vt(n.l), new t(n); + }, t.prototype.tint = function (t) { + return void 0 === t && (t = 10), this.mix("white", t); + }, t.prototype.shade = function (t) { + return void 0 === t && (t = 10), this.mix("black", t); + }, t.prototype.desaturate = function (e) { + void 0 === e && (e = 10);var n = this.toHsl();return n.s -= e / 100, n.s = Vt(n.s), new t(n); + }, t.prototype.saturate = function (e) { + void 0 === e && (e = 10);var n = this.toHsl();return n.s += e / 100, n.s = Vt(n.s), new t(n); + }, t.prototype.greyscale = function () { + return this.desaturate(100); + }, t.prototype.spin = function (e) { + var n = this.toHsl(), + i = (n.h + e) % 360;return n.h = i < 0 ? 360 + i : i, new t(n); + }, t.prototype.mix = function (e, n) { + void 0 === n && (n = 50);var i = this.toRgb(), + s = new t(e).toRgb(), + r = n / 100;return new t({ r: (s.r - i.r) * r + i.r, g: (s.g - i.g) * r + i.g, b: (s.b - i.b) * r + i.b, a: (s.a - i.a) * r + i.a }); + }, t.prototype.analogous = function (e, n) { + void 0 === e && (e = 6), void 0 === n && (n = 30);var i = this.toHsl(), + s = 360 / n, + r = [this];for (i.h = (i.h - (s * e >> 1) + 720) % 360; --e;) i.h = (i.h + s) % 360, r.push(new t(i));return r; + }, t.prototype.complement = function () { + var e = this.toHsl();return e.h = (e.h + 180) % 360, new t(e); + }, t.prototype.monochromatic = function (e) { + void 0 === e && (e = 6);for (var n = this.toHsv(), i = n.h, s = n.s, r = n.v, a = [], o = 1 / e; e--;) a.push(new t({ h: i, s: s, v: r })), r = (r + o) % 1;return a; + }, t.prototype.splitcomplement = function () { + var e = this.toHsl(), + n = e.h;return [this, new t({ h: (n + 72) % 360, s: e.s, l: e.l }), new t({ h: (n + 216) % 360, s: e.s, l: e.l })]; + }, t.prototype.triad = function () { + return this.polyad(3); + }, t.prototype.tetrad = function () { + return this.polyad(4); + }, t.prototype.polyad = function (e) { + for (var n = this.toHsl(), i = n.h, s = [this], r = 360 / e, a = 1; a < e; a++) s.push(new t({ h: (i + a * r) % 360, s: n.s, l: n.l }));return s; + }, t.prototype.equals = function (e) { + return this.toRgbString() === new t(e).toRgbString(); + }, t; +}();function Kt(t, e) { + return void 0 === t && (t = ""), void 0 === e && (e = {}), new Xt(t, e); +}function Qt(t) { + return t.substr(0, t.indexOf(".")); +}function te(t) { + return "var" === t.substring(0, 3) ? window.getComputedStyle(document.documentElement).getPropertyValue(t.substring(4).slice(0, -1)).trim() : t; +}function ee(t, e) { + const n = new Xt(te(t));if (n.isValid) { + const t = n.mix("black", 100 - e).toString();if (t) return t; + }return t; +}function ne(...t) { + const e = t => t && "object" == typeof t;return t.reduce((t, n) => (Object.keys(n).forEach(i => { + const s = t[i], + r = n[i];Array.isArray(s) && Array.isArray(r) ? t[i] = s.concat(...r) : e(s) && e(r) ? t[i] = ne(s, r) : t[i] = r; + }), t), {}); +}function ie(t, e) { + let n = [];return t && t.forEach(t => { + let i = t;e && e.forEach(e => { + e.id && t.id && e.id == t.id && (i = ne(i, e)); + }), n.push(i); + }), e && (n = n.concat(e.filter(e => !t || !t.find(t => !(!t.id || !e.id) && t.id == e.id)))), n; +}const se = ((t, ...e) => { + const n = e.reduce((e, n, i) => e + (t => { + if (t instanceof Q) return t.cssText;if ("number" == typeof t) return t;throw new Error(`Value passed to 'css' function must be a 'css' function result: ${t}. Use 'unsafeCSS' to pass non-literal values, but\n take care to ensure page security.`); + })(n) + t[i + 1], t[0]);return new Q(n, K); +})` + ha-card { + cursor: pointer; + overflow: hidden; + box-sizing: border-box; + position: relative; + display: flex; + justify-content: center; + align-items: center; + + -webkit-touch-callout: none; /* iOS Safari */ + -webkit-user-select: none; /* Safari */ + -khtml-user-select: none; /* Konqueror HTML */ + -moz-user-select: none; /* Old versions of Firefox */ + -ms-user-select: none; /* Internet Explorer/Edge */ + user-select: none; /* Non-prefixed version, currently + supported by Chrome, Opera and Firefox */ + } + ha-card.disabled { + pointer-events: none; + cursor: default; + } + ha-icon { + display: inline-block; + margin: auto; + } + ha-card.button-card-main { + padding: 4% 0px; + text-transform: none; + font-weight: 400; + font-size: 1.2rem; + align-items: center; + text-align: center; + letter-spacing: normal; + width: 100%; + } + .ellipsis { + text-overflow: ellipsis; + white-space: nowrap; + overflow: hidden; + } + #overlay { + align-items: flex-start; + justify-content: flex-end; + padding: 8px 7px; + opacity: 0.5; + /* DO NOT override items below */ + position: absolute; + left: 0; + right: 0; + top: 0; + bottom: 0; + z-index: 1; + display: flex; + } + #lock { + -webkit-animation-fill-mode: both; + animation-fill-mode: both; + margin: unset; + } + .invalid { + animation: blink 1s cubic-bezier(0.68, -0.55, 0.27, 1.55) infinite; + } + .hidden { + visibility: hidden; + opacity: 0; + transition: visibility 0s 1s, opacity 1s linear; + } + @keyframes blink { + 0%{opacity:0;} + 50%{opacity:1;} + 100%{opacity:0;} + } + @-webkit-keyframes rotating /* Safari and Chrome */ { + from { + -webkit-transform: rotate(0deg); + -o-transform: rotate(0deg); + transform: rotate(0deg); + } + to { + -webkit-transform: rotate(360deg); + -o-transform: rotate(360deg); + transform: rotate(360deg); + } + } + @keyframes rotating { + from { + -ms-transform: rotate(0deg); + -moz-transform: rotate(0deg); + -webkit-transform: rotate(0deg); + -o-transform: rotate(0deg); + transform: rotate(0deg); + } + to { + -ms-transform: rotate(360deg); + -moz-transform: rotate(360deg); + -webkit-transform: rotate(360deg); + -o-transform: rotate(360deg); + transform: rotate(360deg); + } + } + [rotating] { + -webkit-animation: rotating 2s linear infinite; + -moz-animation: rotating 2s linear infinite; + -ms-animation: rotating 2s linear infinite; + -o-animation: rotating 2s linear infinite; + animation: rotating 2s linear infinite; + } + + #container { + display: grid; + width: 100%; + height: 100%; + text-align: center; + align-items: center; + } + #img-cell { + display: flex; + grid-area: i; + height: 100%; + width: 100%; + max-width: 100%; + max-height: 100%; + align-self: center; + justify-self: center; + overflow: hidden; + justify-content: center; + align-items: center; + position: relative; + } + + ha-icon#icon { + height: 100%; + width: 100%; + max-height: 100%; + position: absolute; + } + img#icon { + display: block; + height: auto; + width: 100%; + position: absolute; + } + #name { + grid-area: n; + max-width: 100%; + align-self: center; + justify-self: center; + /* margin: auto; */ + } + #state { + grid-area: s; + max-width: 100%; + align-self: center; + justify-self: center; + /* margin: auto; */ + } + + #label { + grid-area: l; + max-width: 100%; + align-self: center; + justify-self: center; + } + + #container.vertical { + grid-template-areas: "i" "n" "s" "l"; + grid-template-columns: 1fr; + grid-template-rows: 1fr min-content min-content min-content; + } + /* Vertical No Icon */ + #container.vertical.no-icon { + grid-template-areas: "n" "s" "l"; + grid-template-columns: 1fr; + grid-template-rows: 1fr min-content 1fr; + } + #container.vertical.no-icon #state { + align-self: center; + } + #container.vertical.no-icon #name { + align-self: end; + } + #container.vertical.no-icon #label { + align-self: start; + } + + /* Vertical No Icon No Name */ + #container.vertical.no-icon.no-name { + grid-template-areas: "s" "l"; + grid-template-columns: 1fr; + grid-template-rows: 1fr 1fr; + } + #container.vertical.no-icon.no-name #state { + align-self: end; + } + #container.vertical.no-icon.no-name #label { + align-self: start; + } + + /* Vertical No Icon No State */ + #container.vertical.no-icon.no-state { + grid-template-areas: "n" "l"; + grid-template-columns: 1fr; + grid-template-rows: 1fr 1fr; + } + #container.vertical.no-icon.no-state #name { + align-self: end; + } + #container.vertical.no-icon.no-state #label { + align-self: start; + } + + /* Vertical No Icon No Label */ + #container.vertical.no-icon.no-label { + grid-template-areas: "n" "s"; + grid-template-columns: 1fr; + grid-template-rows: 1fr 1fr; + } + #container.vertical.no-icon.no-label #name { + align-self: end; + } + #container.vertical.no-icon.no-label #state { + align-self: start; + } + + /* Vertical No Icon No Label No Name */ + #container.vertical.no-icon.no-label.no-name { + grid-template-areas: "s"; + grid-template-columns: 1fr; + grid-template-rows: 1fr; + } + #container.vertical.no-icon.no-label.no-name #state { + align-self: center; + } + /* Vertical No Icon No Label No State */ + #container.vertical.no-icon.no-label.no-state { + grid-template-areas: "n"; + grid-template-columns: 1fr; + grid-template-rows: 1fr; + } + #container.vertical.no-icon.no-label.no-state #name { + align-self: center; + } + + /* Vertical No Icon No Name No State */ + #container.vertical.no-icon.no-name.no-state { + grid-template-areas: "l"; + grid-template-columns: 1fr; + grid-template-rows: 1fr; + } + #container.vertical.no-icon.no-name.no-state #label { + align-self: center; + } + + #container.icon_name_state { + grid-template-areas: "i n" "l l"; + grid-template-columns: 40% 1fr; + grid-template-rows: 1fr min-content; + } + + #container.icon_name { + grid-template-areas: "i n" "s s" "l l"; + grid-template-columns: 40% 1fr; + grid-template-rows: 1fr min-content min-content; + } + + #container.icon_state { + grid-template-areas: "i s" "n n" "l l"; + grid-template-columns: 40% 1fr; + grid-template-rows: 1fr min-content min-content; + } + + #container.name_state { + grid-template-areas: "i" "n" "l"; + grid-template-columns: 1fr; + grid-template-rows: 1fr min-content min-content; + } + #container.name_state.no-icon { + grid-template-areas: "n" "l"; + grid-template-columns: 1fr; + grid-template-rows: 1fr 1fr; + } + #container.name_state.no-icon #name { + align-self: end + } + #container.name_state.no-icon #label { + align-self: start + } + + #container.name_state.no-icon.no-label { + grid-template-areas: "n"; + grid-template-columns: 1fr; + grid-template-rows: 1fr; + } + #container.name_state.no-icon.no-label #name { + align-self: center + } + + /* icon_name_state2nd default */ + #container.icon_name_state2nd { + grid-template-areas: "i n" "i s" "i l"; + grid-template-columns: 40% 1fr; + grid-template-rows: 1fr min-content 1fr; + } + #container.icon_name_state2nd #name { + align-self: end; + } + #container.icon_name_state2nd #state { + align-self: center; + } + #container.icon_name_state2nd #label { + align-self: start; + } + + /* icon_name_state2nd No Label */ + #container.icon_name_state2nd.no-label { + grid-template-areas: "i n" "i s"; + grid-template-columns: 40% 1fr; + grid-template-rows: 1fr 1fr; + } + #container.icon_name_state2nd #name { + align-self: end; + } + #container.icon_name_state2nd #state { + align-self: start; + } + + /* icon_state_name2nd Default */ + #container.icon_state_name2nd { + grid-template-areas: "i s" "i n" "i l"; + grid-template-columns: 40% 1fr; + grid-template-rows: 1fr min-content 1fr; + } + #container.icon_state_name2nd #state { + align-self: end; + } + #container.icon_state_name2nd #name { + align-self: center; + } + #container.icon_state_name2nd #label { + align-self: start; + } + + /* icon_state_name2nd No Label */ + #container.icon_state_name2nd.no-label { + grid-template-areas: "i s" "i n"; + grid-template-columns: 40% 1fr; + grid-template-rows: 1fr 1fr; + } + #container.icon_state_name2nd #state { + align-self: end; + } + #container.icon_state_name2nd #name { + align-self: start; + } + + #container.icon_label { + grid-template-areas: "i l" "n n" "s s"; + grid-template-columns: 40% 1fr; + grid-template-rows: 1fr min-content min-content; + } + + [style*="--aspect-ratio"] > :first-child { + width: 100%; + } + [style*="--aspect-ratio"] > img { + height: auto; + } + @supports (--custom:property) { + [style*="--aspect-ratio"] { + position: relative; + } + [style*="--aspect-ratio"]::before { + content: ""; + display: block; + padding-bottom: calc(100% / (var(--aspect-ratio))); + } + [style*="--aspect-ratio"] > :first-child { + position: absolute; + top: 0; + left: 0; + height: 100%; + } + } +`;console.info("%c BUTTON-CARD \n%c Version 3.1.1 ", "color: orange; font-weight: bold; background: black", "color: white; font-weight: bold; background: dimgray");let re = class extends et { + disconnectedCallback() { + super.disconnectedCallback(), this._clearInterval(); + }connectedCallback() { + if (super.connectedCallback(), this.config && this.config.entity && "timer" === Qt(this.config.entity)) { + const t = this.hass.states[this.config.entity];this._startInterval(t); + } + }static get styles() { + return se; + }render() { + return this._stateObj = this.config.entity ? this.hass.states[this.config.entity] : void 0, this.config && this.hass ? this._cardHtml() : j``; + }shouldUpdate(t) { + const e = !!(this._hasTemplate || this.config.state && this.config.state.find(t => "template" === t.operator) || t.has("_timeRemaining"));return function (t, e, n) { + if (e.has("config") || n) return !0;if (t.config.entity) { + const n = e.get("hass");return !n || n.states[t.config.entity] !== t.hass.states[t.config.entity]; + }return !1; + }(this, t, e); + }updated(t) { + if (super.updated(t), this.config && this.config.entity && "timer" === Qt(this.config.entity) && t.has("hass")) { + const e = this.hass.states[this.config.entity], + n = t.get("hass");(n ? n.states[this.config.entity] : void 0) !== e ? this._startInterval(e) : e || this._clearInterval(); + } + }_clearInterval() { + this._interval && (window.clearInterval(this._interval), this._interval = void 0); + }_startInterval(t) { + this._clearInterval(), this._calculateRemaining(t), "active" === t.state && (this._interval = window.setInterval(() => this._calculateRemaining(t), 1e3)); + }_calculateRemaining(t) { + this._timeRemaining = function (t) { + var e = xt(t.attributes.remaining);if ("active" === t.state) { + var n = new Date().getTime(), + i = new Date(t.last_changed).getTime();e = Math.max(e - (n - i) / 1e3, 0); + }return e; + }(t); + }_computeTimeDisplay(t) { + if (t) return function (t) { + var e = Math.floor(t / 3600), + n = Math.floor(t % 3600 / 60), + i = Math.floor(t % 3600 % 60);return e > 0 ? e + ":" + kt(n) + ":" + kt(i) : n > 0 ? n + ":" + kt(i) : i > 0 ? "" + i : null; + }(this._timeRemaining || xt(t.attributes.duration)); + }_getMatchingConfigState(t) { + if (!this.config.state) return;const e = this.config.state.find(t => "template" === t.operator);if (!t && !e) return;let n;const i = this.config.state.find(e => { + if (!e.operator) return t && this._getTemplateOrValue(t, e.value) == t.state;switch (e.operator) {case "==": + return t && t.state == this._getTemplateOrValue(t, e.value);case "<=": + return t && t.state <= this._getTemplateOrValue(t, e.value);case "<": + return t && t.state < this._getTemplateOrValue(t, e.value);case ">=": + return t && t.state >= this._getTemplateOrValue(t, e.value);case ">": + return t && t.state > this._getTemplateOrValue(t, e.value);case "!=": + return t && t.state != this._getTemplateOrValue(t, e.value);case "regex": + return !(!t || !t.state.match(this._getTemplateOrValue(t, e.value)));case "template": + return this._getTemplateOrValue(t, e.value);case "default": + return n = e, !1;default: + return !1;} + });return !i && n ? n : i; + }_evalTemplate(t, e) { + return new Function("states", "entity", "user", "hass", "variables", `'use strict'; ${e}`).call(this, this.hass.states, t, this.hass.user, this.hass, this.config.variables); + }_objectEvalTemplate(t, e) { + const n = JSON.parse(JSON.stringify(e));return this._getTemplateOrValue(t, n); + }_getTemplateOrValue(t, e) { + if (["number", "boolean"].includes(typeof e)) return e;if (!e) return e;if (["object"].includes(typeof e)) return Object.keys(e).forEach(n => { + e[n] = this._getTemplateOrValue(t, e[n]); + }), e;const n = e.trim();return "[[[" === n.substring(0, 3) && "]]]" === n.slice(-3) ? this._evalTemplate(t, n.slice(3, -3)) : e; + }_getDefaultColorForState(t) { + switch (t.state) {case "on": + return this.config.color_on;case "off": + return this.config.color_off;default: + return this.config.default_color;} + }_getColorForLightEntity(t, e) { + let n = this.config.default_color;return t && (t.attributes.rgb_color ? (n = `rgb(${t.attributes.rgb_color.join(",")})`, t.attributes.brightness && (n = ee(n, (t.attributes.brightness + 245) / 5))) : e && t.attributes.color_temp && t.attributes.min_mireds && t.attributes.max_mireds ? (n = function (t, e, n) { + const i = new Xt("rgb(255, 160, 0)"), + s = new Xt("rgb(166, 209, 255)"), + r = new Xt("white"), + a = (t - e) / (n - e) * 100;return a < 50 ? Kt(s).mix(r, 2 * a).toRgbString() : Kt(r).mix(i, 2 * (a - 50)).toRgbString(); + }(t.attributes.color_temp, t.attributes.min_mireds, t.attributes.max_mireds), t.attributes.brightness && (n = ee(n, (t.attributes.brightness + 245) / 5))) : n = t.attributes.brightness ? ee(this._getDefaultColorForState(t), (t.attributes.brightness + 245) / 5) : this._getDefaultColorForState(t)), n; + }_buildCssColorAttribute(t, e) { + let n, + i = "";return e && e.color ? i = e.color : "auto" !== this.config.color && t && "off" === t.state ? i = this.config.color_off : this.config.color && (i = this.config.color), n = "auto" == i || "auto-no-temperature" == i ? this._getColorForLightEntity(t, "auto-no-temperature" !== i) : i || (t ? this._getDefaultColorForState(t) : this.config.default_color), n; + }_buildIcon(t, e) { + if (!this.config.show_icon) return;let n;return e && e.icon ? n = e.icon : this.config.icon ? n = this.config.icon : t && t.attributes && (n = t.attributes.icon ? t.attributes.icon : function (t, e) { + if (t in Ct) return Ct[t];switch (t) {case "alarm_control_panel": + switch (e) {case "armed_home": + return "hass:bell-plus";case "armed_night": + return "hass:bell-sleep";case "disarmed": + return "hass:bell-outline";case "triggered": + return "hass:bell-ring";default: + return "hass:bell";}case "binary_sensor": + return e && "off" === e ? "hass:radiobox-blank" : "hass:checkbox-marked-circle";case "cover": + return "closed" === e ? "hass:window-closed" : "hass:window-open";case "lock": + return e && "unlocked" === e ? "hass:lock-open" : "hass:lock";case "media_player": + return e && "off" !== e && "idle" !== e ? "hass:cast-connected" : "hass:cast";case "zwave": + switch (e) {case "dead": + return "hass:emoticon-dead";case "sleeping": + return "hass:sleep";case "initializing": + return "hass:timer-sand";default: + return "hass:z-wave";}default: + return console.warn("Unable to find icon for domain " + t + " (" + e + ")"), "hass:bookmark";} + }(Qt(t.entity_id), t.state)), this._getTemplateOrValue(t, n); + }_buildEntityPicture(t, e) { + if (!this.config.show_entity_picture || !t && !e && !this.config.entity_picture) return;let n;return e && e.entity_picture ? n = e.entity_picture : this.config.entity_picture ? n = this.config.entity_picture : t && (n = t.attributes && t.attributes.entity_picture ? t.attributes.entity_picture : void 0), this._getTemplateOrValue(t, n); + }_buildStyleGeneric(t, e, n) { + let i = {};if (this.config.styles && this.config.styles[n] && (i = Object.assign(i, ...this.config.styles[n])), e && e.styles && e.styles[n]) { + let t = {};t = Object.assign(t, ...e.styles[n]), i = Object.assign(Object.assign({}, i), t); + }return Object.keys(i).forEach(e => { + i[e] = this._getTemplateOrValue(t, i[e]); + }), i; + }_buildCustomStyleGeneric(t, e, n) { + let i = {};if (this.config.styles && this.config.styles.custom_fields && this.config.styles.custom_fields[n] && (i = Object.assign(i, ...this.config.styles.custom_fields[n])), e && e.styles && e.styles.custom_fields && e.styles.custom_fields[n]) { + let t = {};t = Object.assign(t, ...e.styles.custom_fields[n]), i = Object.assign(Object.assign({}, i), t); + }return Object.keys(i).forEach(e => { + i[e] = this._getTemplateOrValue(t, i[e]); + }), i; + }_buildName(t, e) { + if (!1 === this.config.show_name) return;let n;var i;return e && e.name ? n = e.name : this.config.name ? n = this.config.name : t && (n = t.attributes && t.attributes.friendly_name ? t.attributes.friendly_name : (i = t.entity_id).substr(i.indexOf(".") + 1)), this._getTemplateOrValue(t, n); + }_buildStateString(t) { + let e;if (this.config.show_state && t && t.state) { + const n = ((t, e) => { + let n;const i = Qt(e.entity_id);return "binary_sensor" === i ? (e.attributes.device_class && (n = t(`state.${i}.${e.attributes.device_class}.${e.state}`)), n || (n = t(`state.${i}.default.${e.state}`))) : n = e.attributes.unit_of_measurement && !["unknown", "unavailable"].includes(e.state) ? e.state : "zwave" === i ? ["initializing", "dead"].includes(e.state) ? t(`state.zwave.query_stage.${e.state}`, "query_stage", e.attributes.query_stage) : t(`state.zwave.default.${e.state}`) : t(`state.${i}.${e.state}`), n || (n = t(`state.default.${e.state}`) || t(`component.${i}.state.${e.state}`) || e.state), n; + })(this.hass.localize, t), + i = this._buildUnits(t);i ? e = `${t.state} ${i}` : "timer" === Qt(t.entity_id) ? "idle" === t.state || 0 === this._timeRemaining ? e = n : (e = this._computeTimeDisplay(t), "paused" === t.state && (e += ` (${n})`)) : e = n; + }return e; + }_buildUnits(t) { + let e;return t && this.config.show_units && (e = t.attributes && t.attributes.unit_of_measurement && !this.config.units ? t.attributes.unit_of_measurement : this.config.units ? this.config.units : void 0), e; + }_buildLastChanged(t, e) { + return this.config.show_last_changed && t ? j` + ` : void 0; + }_buildLabel(t, e) { + if (!this.config.show_label) return;let n;return n = e && e.label ? e.label : this.config.label, this._getTemplateOrValue(t, n); + }_buildCustomFields(t, e) { + let n = j``;const i = {}, + s = {};return this.config.custom_fields && Object.keys(this.config.custom_fields).forEach(e => { + const n = this.config.custom_fields[e];n.card ? s[e] = this._objectEvalTemplate(t, n.card) : i[e] = this._getTemplateOrValue(t, n); + }), e && e.custom_fields && Object.keys(e.custom_fields).forEach(n => { + const r = e.custom_fields[n];r.card ? s[n] = this._objectEvalTemplate(t, r.card) : i[n] = this._getTemplateOrValue(t, r); + }), Object.keys(i).forEach(s => { + if (null != i[s]) { + const r = Object.assign(Object.assign({}, this._buildCustomStyleGeneric(t, e, s)), { "grid-area": s });n = j`${n} +
${rt(i[s])}
`; + } + }), Object.keys(s).forEach(i => { + if (null != s[i]) { + const r = Object.assign(Object.assign({}, this._buildCustomStyleGeneric(t, e, i)), { "grid-area": i }), + a = function (t) { + var e = function (t, e) { + return n("hui-error-card", { type: "error", error: t, config: e }); + }, + n = function (t, n) { + var i = window.document.createElement(t);try { + i.setConfig(n); + } catch (i) { + return console.error(t, i), e(i.message, n); + }return i; + };if (!t || "object" != typeof t || !t.type) return e("No type defined", t);var i = t.type;if (i = i.startsWith("custom:") ? i.substr("custom:".length) : "hui-" + i + "-card", customElements.get(i)) return n(i, t);var s = e("Custom element doesn't exist: " + t.type + ".", t);s.style.display = "None";var r = setTimeout(function () { + s.style.display = ""; + }, 2e3);return customElements.whenDefined(t.type).then(function () { + clearTimeout(r), Tt(s, "ll-rebuild", {}, s); + }), s; + }(s[i]);a.hass = this.hass, n = j`${n} +
${a}
`; + } + }), n; + }_isClickable(t) { + let e = !0;if ("toggle" === this.config.tap_action.action && "none" === this.config.hold_action.action && "none" === this.config.double_tap_action.action || "toggle" === this.config.hold_action.action && "none" === this.config.tap_action.action && "none" === this.config.double_tap_action.action || "toggle" === this.config.double_tap_action.action && "none" === this.config.tap_action.action && "none" === this.config.hold_action.action) { + if (t) switch (Qt(t.entity_id)) {case "sensor":case "binary_sensor":case "device_tracker": + e = !1;break;default: + e = !0;} else e = !1; + } else e = "none" != this.config.tap_action.action || "none" != this.config.hold_action.action || "none" != this.config.double_tap_action.action;return e; + }_rotate(t) { + return !(!t || !t.spin); + }_blankCardColoredHtml(t) { + const e = Object.assign({ background: "none", "box-shadow": "none" }, t);return j` + +
+
+ `; + }_cardHtml() { + const t = this._getMatchingConfigState(this._stateObj), + e = this._buildCssColorAttribute(this._stateObj, t);let n = e, + i = {}, + s = {};const r = {}, + a = this._buildStyleGeneric(this._stateObj, t, "lock"), + o = this._buildStyleGeneric(this._stateObj, t, "card"), + l = { "button-card-main": !0, disabled: !this._isClickable(this._stateObj) };switch (o.width && (this.style.setProperty("flex", "0 0 auto"), this.style.setProperty("max-width", "fit-content")), this.config.color_type) {case "blank-card": + return this._blankCardColoredHtml(o);case "card":case "label-card": + { + const t = function (t) { + const e = new Xt(te(t));return e.isValid && e.getLuminance() > .5 ? "rgb(62, 62, 62)" : "rgb(234, 234, 234)"; + }(e);i.color = t, s.color = t, i["background-color"] = e, i = Object.assign(Object.assign({}, i), o), n = "inherit";break; + }default: + i = o;}return this.config.aspect_ratio ? (r["--aspect-ratio"] = this.config.aspect_ratio, i.position = "absolute") : r.display = "inline", this.style.setProperty("--button-card-light-color", this._getColorForLightEntity(this._stateObj, !0)), this.style.setProperty("--button-card-light-color-no-temperature", this._getColorForLightEntity(this._stateObj, !1)), s = Object.assign(Object.assign({}, s), a), j` +
+ + ${this._buttonContent(this._stateObj, t, n)} + ${this._getLock(s)} + +
+ `; + }_getLock(t) { + return this.config.lock && this._getTemplateOrValue(this._stateObj, this.config.lock.enabled) ? j` +
this._handleUnlockType(t, "tap")} + @ha-hold=${t => this._handleUnlockType(t, "hold")} + @ha-dblclick=${t => this._handleUnlockType(t, "double_tap")} + .hasDblClick=${"double_tap" === this.config.lock.unlock} + .longpress=${$t()} + .config="${this.config}" + > + +
+ ` : j``; + }_buttonContent(t, e, n) { + const i = this._buildName(t, e), + s = this._buildStateString(t), + r = function (t, e) { + if (!t && !e) return;let n;return n = e ? t ? `${t}: ${e}` : e : t, n; + }(i, s);switch (this.config.layout) {case "icon_name_state":case "name_state": + return this._gridHtml(t, e, this.config.layout, n, r, void 0);default: + return this._gridHtml(t, e, this.config.layout, n, i, s);} + }_gridHtml(t, e, n, i, s, r) { + const a = this._getIconHtml(t, e, i), + o = [n], + l = this._buildLabel(t, e), + c = this._buildStyleGeneric(t, e, "name"), + h = this._buildStyleGeneric(t, e, "state"), + u = this._buildStyleGeneric(t, e, "label"), + d = this._buildLastChanged(t, u), + f = this._buildStyleGeneric(t, e, "grid");return a || o.push("no-icon"), s || o.push("no-name"), r || o.push("no-state"), l || d || o.push("no-label"), j` +
+ ${a || ""} + ${s ? j`
${rt(s)}
` : ""} + ${r ? j`
${r}
` : ""} + ${l && !d ? j`
${rt(l)}
` : ""} + ${d || ""} + ${this._buildCustomFields(t, e)} +
+ `; + }_getIconHtml(t, e, n) { + const i = this._buildIcon(t, e), + s = this._buildEntityPicture(t, e), + r = this._buildStyleGeneric(t, e, "entity_picture"), + a = this._buildStyleGeneric(t, e, "icon"), + o = this._buildStyleGeneric(t, e, "img_cell"), + l = this._buildStyleGeneric(t, e, "card"), + c = Object.assign({ color: n, width: this.config.size, position: this.config.aspect_ratio || l.height ? "absolute" : "relative" }, a), + h = Object.assign(Object.assign({}, c), r);return i || s ? j` +
+ ${i && !s ? j`` : ""} + ${s ? j`` : ""} +
+ ` : void 0; + }setConfig(t) { + if (!t) throw new Error("Invalid configuration");const e = function () { + var t = document.querySelector("home-assistant");if (t = (t = (t = (t = (t = (t = (t = (t = t && t.shadowRoot) && t.querySelector("home-assistant-main")) && t.shadowRoot) && t.querySelector("app-drawer-layout partial-panel-resolver")) && t.shadowRoot || t) && t.querySelector("ha-panel-lovelace")) && t.shadowRoot) && t.querySelector("hui-root")) { + var e = t.lovelace;return e.current_view = t.___curView, e; + }return null; + }() || function () { + let t = document.querySelector("hc-main");if (t = t && t.shadowRoot, t = t && t.querySelector("hc-lovelace"), t = t && t.shadowRoot, t = t && t.querySelector("hui-view"), t) { + const e = t.lovelace;return e.current_view = t.___curView, e; + }return null; + }();let n = Object.assign({}, t), + i = n.template, + s = t.state;for (; i && e.config.button_card_templates && e.config.button_card_templates[i];) n = ne(e.config.button_card_templates[i], n), s = ie(e.config.button_card_templates[i].state, s), i = e.config.button_card_templates[i].template;n.state = s, this.config = Object.assign({ tap_action: { action: "toggle" }, hold_action: { action: "none" }, double_tap_action: { action: "none" }, layout: "vertical", size: "40%", color_type: "icon", show_name: !0, show_state: !1, show_icon: !0, show_units: !0, show_label: !1, show_entity_picture: !1 }, n), this.config.lock = Object.assign({ enabled: !1, duration: 5, unlock: "tap" }, this.config.lock), this.config.default_color = "var(--primary-text-color)", "icon" !== this.config.color_type ? this.config.color_off = "var(--paper-card-background-color)" : this.config.color_off = "var(--paper-item-icon-color)", this.config.color_on = "var(--paper-item-icon-active-color)";const r = JSON.stringify(this.config), + a = new RegExp("\\[\\[\\[.*\\]\\]\\]", "gm");this._hasTemplate = !!r.match(a); + }getCardSize() { + return 3; + }_evalActions(t, e) { + const n = this.config.entity ? this.hass.states[this.config.entity] : void 0, + i = JSON.parse(JSON.stringify(t)), + s = t => t ? (Object.keys(t).forEach(e => { + "object" == typeof t[e] ? t[e] = s(t[e]) : t[e] = this._getTemplateOrValue(n, t[e]); + }), t) : t;return i[e] = s(i[e]), !i[e].confirmation && i.confirmation && (i[e].confirmation = s(i.confirmation)), i; + }_handleTap(t) { + const e = t.target.config;Et(this, this.hass, this._evalActions(e, "tap_action"), !1, !1); + }_handleHold(t) { + const e = t.target.config;Et(this, this.hass, this._evalActions(e, "hold_action"), !0, !1); + }_handleDblTap(t) { + const e = t.target.config;Et(this, this.hass, this._evalActions(e, "double_tap_action"), !1, !0); + }_handleUnlockType(t, e) { + t.target.config.lock.unlock === e && this._handleLock(t); + }_handleLock(t) { + t.stopPropagation();const e = this.shadowRoot.getElementById("lock");if (!e) return;if (this.config.lock.exemptions) { + if (!this.hass.user.name || !this.hass.user.id) return;let t = !1;if (this.config.lock.exemptions.forEach(e => { + (!t && e.user === this.hass.user.id || e.username === this.hass.user.name) && (t = !0); + }), !t) return e.classList.add("invalid"), void window.setTimeout(() => { + e && e.classList.remove("invalid"); + }, 3e3); + }const n = this.shadowRoot.getElementById("overlay"), + i = this.shadowRoot.getElementById("card");n.style.setProperty("pointer-events", "none");const s = document.createElement("paper-ripple");if (e) { + i.appendChild(s);const t = document.createAttribute("icon");t.value = "mdi:lock-open-outline", e.attributes.setNamedItem(t), e.classList.add("hidden"); + }window.setTimeout(() => { + if (n.style.setProperty("pointer-events", ""), e) { + e.classList.remove("hidden");const t = document.createAttribute("icon");t.value = "mdi:lock-outline", e.attributes.setNamedItem(t), i.removeChild(s); + } + }, 1e3 * this.config.lock.duration); + }_stopPropagation(t) { + t.stopPropagation(); + } +};var ae;t([Z()], re.prototype, "hass", void 0), t([Z()], re.prototype, "config", void 0), t([Z()], re.prototype, "_timeRemaining", void 0), t([Z()], re.prototype, "_hasTemplate", void 0), t([Z()], re.prototype, "_stateObj", void 0), re = t([(ae = "button-card", t => "function" == typeof t ? ((t, e) => (window.customElements.define(t, e), e))(ae, t) : ((t, e) => { + const { kind: n, elements: i } = e;return { kind: n, elements: i, finisher(e) { + window.customElements.define(t, e); + } }; +})(ae, t))], re);