Merge pull request #582 from balloob/mdi-icons

Icons for everyone!
This commit is contained in:
Paulus Schoutsen 2015-11-04 09:08:34 -08:00
commit bd798b8c55
13 changed files with 915 additions and 802 deletions

View file

@ -10,14 +10,15 @@ import homeassistant.core as ha
import homeassistant.bootstrap as bootstrap
import homeassistant.loader as loader
from homeassistant.const import (
CONF_PLATFORM, ATTR_ENTITY_PICTURE, ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME)
CONF_PLATFORM, ATTR_ENTITY_ID)
DOMAIN = "demo"
DEPENDENCIES = ['introduction', 'conversation']
DEPENDENCIES = ['conversation', 'introduction', 'zone']
COMPONENTS_WITH_DEMO_PLATFORM = [
'switch', 'light', 'sensor', 'thermostat', 'media_player', 'notify']
'device_tracker', 'light', 'media_player', 'notify', 'switch', 'sensor',
'thermostat']
def setup(hass, config):
@ -110,25 +111,6 @@ def setup(hass, config):
}},
]})
# Setup fake device tracker
hass.states.set("device_tracker.paulus", "home",
{ATTR_ENTITY_PICTURE:
"http://graph.facebook.com/297400035/picture",
ATTR_FRIENDLY_NAME: 'Paulus'})
hass.states.set("device_tracker.anne_therese", "not_home",
{ATTR_FRIENDLY_NAME: 'Anne Therese',
'latitude': hass.config.latitude + 0.002,
'longitude': hass.config.longitude + 0.002})
hass.states.set("group.all_devices", "home",
{
"auto": True,
ATTR_ENTITY_ID: [
"device_tracker.paulus",
"device_tracker.anne_therese"
]
})
# Setup configurator
configurator_ids = []

View file

@ -8,7 +8,7 @@ import re
import os
import logging
from . import version
from . import version, mdi_version
import homeassistant.util as util
from homeassistant.const import URL_ROOT, HTTP_OK
from homeassistant.config import get_default_config_dir
@ -74,6 +74,7 @@ def _handle_get_root(handler, path_match, data):
template_html = template_html.replace('{{ app_url }}', app_url)
template_html = template_html.replace('{{ auth }}', auth)
template_html = template_html.replace('{{ icons }}', mdi_version.VERSION)
handler.wfile.write(template_html.encode("UTF-8"))

View file

@ -46,6 +46,6 @@
</div>
<script src='/static/webcomponents-lite.min.js'></script>
<link rel='import' href='/static/{{ app_url }}' />
<home-assistant auth='{{ auth }}'></home-assistant>
<home-assistant auth='{{ auth }}' icons='{{ icons }}'></home-assistant>
</body>
</html>

View file

@ -0,0 +1,2 @@
""" DO NOT MODIFY. Auto-generated by update_mdi script """
VERSION = "38EF63D0474411E4B3CF842B2B6CFE1B"

View file

@ -1,2 +1,2 @@
""" DO NOT MODIFY. Auto-generated by build_frontend script """
VERSION = "beb922c55bb26ea576581b453f6d7c04"
VERSION = "5e61b80689feebb3a7043de07fc01971"

File diff suppressed because one or more lines are too long

@ -1 +1 @@
Subproject commit 24623ff26ab8cbf7b39f0a25c26d9d991063b61a
Subproject commit 9a5079c15b303ca124a91406825a4979525c4cd5

File diff suppressed because one or more lines are too long

View file

@ -12,16 +12,17 @@ from homeassistant.const import DEVICE_DEFAULT_NAME
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return demo switches. """
add_devices_callback([
DemoSwitch('Decorative Lights', True),
DemoSwitch('AC', False)
DemoSwitch('Decorative Lights', True, None),
DemoSwitch('AC', False, 'mdi:air-conditioner')
])
class DemoSwitch(SwitchDevice):
""" Provides a demo switch. """
def __init__(self, name, state):
def __init__(self, name, state, icon):
self._name = name or DEVICE_DEFAULT_NAME
self._state = state
self._icon = icon
@property
def should_poll(self):
@ -33,6 +34,11 @@ class DemoSwitch(SwitchDevice):
""" Returns the name of the device if any. """
return self._name
@property
def icon(self):
""" Returns the icon to use for device if any. """
return self._icon
@property
def current_power_mwh(self):
""" Current power usage in mwh. """

View file

@ -9,7 +9,7 @@ https://home-assistant.io/components/zone.html
import logging
from homeassistant.const import (
ATTR_HIDDEN, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME)
ATTR_HIDDEN, ATTR_ICON, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME)
from homeassistant.helpers import extract_domain_configs, generate_entity_id
from homeassistant.helpers.entity import Entity
from homeassistant.util.location import distance
@ -25,8 +25,7 @@ DEFAULT_NAME = 'Unnamed zone'
ATTR_RADIUS = 'radius'
DEFAULT_RADIUS = 100
ATTR_ICON = 'icon'
ICON_HOME = 'home'
ICON_HOME = 'mdi:home'
def active_zone(hass, latitude, longitude, radius=0):
@ -110,7 +109,7 @@ class Zone(Entity):
self.latitude = latitude
self.longitude = longitude
self.radius = radius
self.icon = icon
self._icon = icon
def should_poll(self):
return False
@ -124,14 +123,15 @@ class Zone(Entity):
""" The state property really does nothing for a zone. """
return STATE
@property
def icon(self):
return self._icon
@property
def state_attributes(self):
attr = {
return {
ATTR_HIDDEN: True,
ATTR_LATITUDE: self.latitude,
ATTR_LONGITUDE: self.longitude,
ATTR_RADIUS: self.radius,
}
if self.icon:
attr[ATTR_ICON] = self.icon
return attr

View file

@ -74,6 +74,9 @@ ATTR_FRIENDLY_NAME = "friendly_name"
# A picture to represent entity
ATTR_ENTITY_PICTURE = "entity_picture"
# Icon to use in the frontend
ATTR_ICON = "icon"
# The unit of measurement if applicable
ATTR_UNIT_OF_MEASUREMENT = "unit_of_measurement"

View file

@ -10,7 +10,7 @@ from collections import defaultdict
from homeassistant.exceptions import NoEntitySpecifiedError
from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_UNIT_OF_MEASUREMENT,
ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_UNIT_OF_MEASUREMENT, ATTR_ICON,
DEVICE_DEFAULT_NAME, STATE_ON, STATE_OFF, STATE_UNKNOWN, TEMP_CELCIUS,
TEMP_FAHRENHEIT)
@ -61,6 +61,11 @@ class Entity(object):
""" Unit of measurement of this entity, if any. """
return None
@property
def icon(self):
""" Icon to use in the frontend, if any. """
return None
@property
def hidden(self):
""" Suggestion if the entity should be hidden from UIs. """
@ -102,6 +107,9 @@ class Entity(object):
if ATTR_UNIT_OF_MEASUREMENT not in attr and self.unit_of_measurement:
attr[ATTR_UNIT_OF_MEASUREMENT] = self.unit_of_measurement
if ATTR_ICON not in attr and self.icon:
attr[ATTR_ICON] = self.icon
if self.hidden:
attr[ATTR_HIDDEN] = self.hidden

92
script/update_mdi.py Executable file
View file

@ -0,0 +1,92 @@
#!/usr/bin/env python3
"""
Downloads the latest Polymer v1 iconset version for materialdesignicons.com
"""
import os
import re
import requests
import sys
GETTING_STARTED_URL = ('https://raw.githubusercontent.com/Templarian/'
'MaterialDesign/master/site/getting-started.savvy')
DOWNLOAD_LINK = re.compile(r'(/api/download/polymer/v1/([A-Z0-9-]{36}))')
START_ICONSET = '<iron-iconset-svg'
CUR_VERSION = re.compile(r'VERSION = "([A-Za-z0-9]{32})"')
OUTPUT_BASE = os.path.join('homeassistant', 'components', 'frontend')
VERSION_OUTPUT = os.path.join(OUTPUT_BASE, 'mdi_version.py')
ICONSET_OUTPUT = os.path.join(OUTPUT_BASE, 'www_static', 'mdi.html')
def get_local_version():
""" Parse local version. """
try:
with open(VERSION_OUTPUT) as inp:
for line in inp:
match = CUR_VERSION.search(line)
if match:
return match.group(1)
except FileNotFoundError:
return False
return False
def get_remote_version():
""" Get current version and download link. """
gs_page = requests.get(GETTING_STARTED_URL).text
mdi_download = re.search(DOWNLOAD_LINK, gs_page)
if not mdi_download:
print("Unable to find download link")
sys.exit()
url = 'https://materialdesignicons.com' + mdi_download.group(1)
version = mdi_download.group(2).replace('-', '')
return version, url
def clean_component(source):
""" Clean component. """
return source[source.index(START_ICONSET):]
def write_component(version, source):
""" Write component. """
with open(ICONSET_OUTPUT, 'w') as outp:
print('Writing icons to', ICONSET_OUTPUT)
outp.write(source)
with open(VERSION_OUTPUT, 'w') as outp:
print('Generating version file', VERSION_OUTPUT)
outp.write(
'""" DO NOT MODIFY. Auto-generated by update_mdi script """\n')
outp.write('VERSION = "{}"\n'.format(version))
def main():
# All scripts should have their current work dir set to project root
if os.path.basename(os.getcwd()) == 'script':
os.chdir('..')
print("materialdesignicons.com icon updater")
local_version = get_local_version()
remote_version, remote_url = get_remote_version()
print('Local version:', local_version)
print('Remote version:', remote_version)
if local_version == remote_version:
print('Already on the latest version.')
sys.exit()
write_component(remote_version,
clean_component(requests.get(remote_url).text))
print('Updated to latest version')
if __name__ == '__main__':
main()