2016-01-31 08:50:03 +00:00
|
|
|
"""
|
2016-03-07 17:49:31 +00:00
|
|
|
Support for links to external web pages.
|
2016-01-31 08:50:03 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
2016-02-02 22:55:44 +00:00
|
|
|
https://home-assistant.io/components/weblink/
|
2016-01-31 08:50:03 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-09-02 17:16:42 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (CONF_NAME, CONF_ICON, CONF_URL)
|
2016-01-31 08:50:03 +00:00
|
|
|
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-01-31 08:50:03 +00:00
|
|
|
|
2016-09-02 17:16:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-01-31 08:50:03 +00:00
|
|
|
|
2016-09-02 17:16:42 +00:00
|
|
|
CONF_ENTITIES = 'entities'
|
2018-01-27 06:30:39 +00:00
|
|
|
CONF_RELATIVE_URL_ERROR_MSG = "Invalid relative URL. Absolute path required."
|
|
|
|
CONF_RELATIVE_URL_REGEX = r'\A/'
|
2016-01-31 08:50:03 +00:00
|
|
|
|
2016-09-02 17:16:42 +00:00
|
|
|
DOMAIN = 'weblink'
|
|
|
|
|
|
|
|
ENTITIES_SCHEMA = vol.Schema({
|
2018-02-11 17:19:00 +00:00
|
|
|
# pylint: disable=no-value-for-parameter
|
2018-01-27 06:30:39 +00:00
|
|
|
vol.Required(CONF_URL): vol.Any(
|
|
|
|
vol.Match(CONF_RELATIVE_URL_REGEX, msg=CONF_RELATIVE_URL_ERROR_MSG),
|
2018-02-11 17:19:00 +00:00
|
|
|
vol.Url()),
|
2016-09-02 17:16:42 +00:00
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_ICON): cv.icon,
|
|
|
|
})
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_ENTITIES): [ENTITIES_SCHEMA],
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
2016-01-31 08:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the weblink component."""
|
2016-01-31 08:50:03 +00:00
|
|
|
links = config.get(DOMAIN)
|
|
|
|
|
2016-09-02 17:16:42 +00:00
|
|
|
for link in links.get(CONF_ENTITIES):
|
|
|
|
Link(hass, link.get(CONF_NAME), link.get(CONF_URL),
|
2016-09-14 01:15:19 +00:00
|
|
|
link.get(CONF_ICON))
|
2016-01-31 08:50:03 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class Link(Entity):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Representation of a link."""
|
|
|
|
|
2016-01-31 08:50:03 +00:00
|
|
|
def __init__(self, hass, name, url, icon):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the link."""
|
2016-01-31 08:50:03 +00:00
|
|
|
self.hass = hass
|
|
|
|
self._name = name
|
|
|
|
self._url = url
|
|
|
|
self._icon = icon
|
|
|
|
self.entity_id = DOMAIN + '.%s' % slugify(name)
|
2017-03-04 23:10:36 +00:00
|
|
|
self.schedule_update_ha_state()
|
2016-01-31 08:50:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2016-01-31 08:50:03 +00:00
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Return the name of the URL."""
|
2016-01-31 08:50:03 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Return the URL."""
|
2016-01-31 08:50:03 +00:00
|
|
|
return self._url
|