core/homeassistant/components/weblink/__init__.py

73 lines
1.8 KiB
Python
Raw Normal View History

"""Support for links to external web pages."""
import logging
2016-09-02 17:16:42 +00:00
import voluptuous as vol
2019-07-31 19:25:30 +00:00
from homeassistant.const import CONF_NAME, CONF_ICON, CONF_URL
from homeassistant.helpers.entity import Entity
from homeassistant.util import slugify
2016-09-02 17:16:42 +00:00
import homeassistant.helpers.config_validation as cv
2016-09-02 17:16:42 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_ENTITIES = "entities"
CONF_RELATIVE_URL_ERROR_MSG = "Invalid relative URL. Absolute path required."
2019-07-31 19:25:30 +00:00
CONF_RELATIVE_URL_REGEX = r"\A/"
2019-07-31 19:25:30 +00:00
DOMAIN = "weblink"
2016-09-02 17:16:42 +00:00
2019-07-31 19:25:30 +00:00
ENTITIES_SCHEMA = vol.Schema(
{
# pylint: disable=no-value-for-parameter
vol.Required(CONF_URL): vol.Any(
vol.Match(CONF_RELATIVE_URL_REGEX, msg=CONF_RELATIVE_URL_ERROR_MSG),
vol.Url(),
),
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_ICON): cv.icon,
}
)
2016-09-02 17:16:42 +00:00
2019-07-31 19:25:30 +00:00
CONFIG_SCHEMA = vol.Schema(
{DOMAIN: vol.Schema({vol.Required(CONF_ENTITIES): [ENTITIES_SCHEMA]})},
extra=vol.ALLOW_EXTRA,
)
def setup(hass, config):
"""Set up the weblink component."""
links = config.get(DOMAIN)
2016-09-02 17:16:42 +00:00
for link in links.get(CONF_ENTITIES):
2019-07-31 19:25:30 +00:00
Link(hass, link.get(CONF_NAME), link.get(CONF_URL), link.get(CONF_ICON))
return True
class Link(Entity):
2016-03-08 16:55:57 +00:00
"""Representation of a link."""
def __init__(self, hass, name, url, icon):
2016-03-08 16:55:57 +00:00
"""Initialize the link."""
self.hass = hass
self._name = name
self._url = url
self._icon = icon
2019-07-31 19:25:30 +00:00
self.entity_id = DOMAIN + ".%s" % slugify(name)
self.schedule_update_ha_state()
@property
def icon(self):
2016-03-07 17:49:31 +00:00
"""Return the icon to use in the frontend, if any."""
return self._icon
@property
def name(self):
2016-03-07 17:49:31 +00:00
"""Return the name of the URL."""
return self._name
@property
def state(self):
2016-03-07 17:49:31 +00:00
"""Return the URL."""
return self._url