2016-01-31 08:50:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
homeassistant.components.weblink
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2016-02-02 22:55:44 +00:00
|
|
|
Adds links to external webpages.
|
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
|
|
|
|
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
|
|
|
DOMAIN = "weblink"
|
|
|
|
DEPENDENCIES = []
|
|
|
|
|
|
|
|
ATTR_NAME = 'name'
|
|
|
|
ATTR_URL = 'url'
|
|
|
|
ATTR_ICON = 'icon'
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
""" Setup weblink component. """
|
|
|
|
|
|
|
|
links = config.get(DOMAIN)
|
|
|
|
|
|
|
|
for link in links.get('entities'):
|
|
|
|
if ATTR_NAME not in link or ATTR_URL not in link:
|
|
|
|
_LOGGER.error("You need to set both %s and %s to add a %s",
|
|
|
|
ATTR_NAME, ATTR_URL, DOMAIN)
|
|
|
|
continue
|
|
|
|
Link(hass, link.get(ATTR_NAME), link.get(ATTR_URL),
|
|
|
|
link.get(ATTR_ICON))
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class Link(Entity):
|
2016-02-02 22:55:44 +00:00
|
|
|
""" Represent a link. """
|
2016-01-31 08:50:03 +00:00
|
|
|
|
|
|
|
def __init__(self, hass, name, url, icon):
|
|
|
|
self.hass = hass
|
|
|
|
self._name = name
|
|
|
|
self._url = url
|
|
|
|
self._icon = icon
|
|
|
|
self.entity_id = DOMAIN + '.%s' % slugify(name)
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-02-02 22:55:44 +00:00
|
|
|
""" Icon to use in the frontend, if any. """
|
2016-01-31 08:50:03 +00:00
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-02-02 22:55:44 +00:00
|
|
|
""" Returns the name of the URL. """
|
2016-01-31 08:50:03 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-02-02 22:55:44 +00:00
|
|
|
""" Returns the URL. """
|
2016-01-31 08:50:03 +00:00
|
|
|
return self._url
|