core/homeassistant/components/weblink.py

73 lines
1.7 KiB
Python
Raw Normal View History

"""
2016-03-07 17:49:31 +00:00
Support for links to external web pages.
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/
"""
import logging
2016-09-02 17:16:42 +00:00
import voluptuous as vol
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__)
2016-09-02 17:16:42 +00:00
CONF_ENTITIES = 'entities'
2016-09-02 17:16:42 +00:00
DOMAIN = 'weblink'
ENTITIES_SCHEMA = vol.Schema({
2016-09-18 06:21:24 +00:00
# pylint: disable=no-value-for-parameter
vol.Required(CONF_URL): 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)
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):
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
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