From 71803658f519678fd849572a3c7daff95f863581 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 16 Mar 2015 22:20:31 -0700 Subject: [PATCH] Updates to demo component and platforms --- homeassistant/components/demo.py | 44 +++++++++++++------ homeassistant/components/light/demo.py | 5 +++ homeassistant/components/media_player/demo.py | 5 +++ homeassistant/components/notify/demo.py | 31 +++++++++++++ homeassistant/components/sensor/demo.py | 5 +++ homeassistant/components/switch/demo.py | 5 +++ homeassistant/components/thermostat/demo.py | 5 +++ tests/test_component_demo.py | 16 ++++--- 8 files changed, 96 insertions(+), 20 deletions(-) create mode 100644 homeassistant/components/notify/demo.py diff --git a/homeassistant/components/demo.py b/homeassistant/components/demo.py index 9c3e699b113..d4538f6815d 100644 --- a/homeassistant/components/demo.py +++ b/homeassistant/components/demo.py @@ -10,7 +10,7 @@ import homeassistant as ha import homeassistant.bootstrap as bootstrap import homeassistant.loader as loader from homeassistant.const import ( - CONF_PLATFORM, ATTR_ENTITY_PICTURE, + CONF_PLATFORM, ATTR_ENTITY_PICTURE, ATTR_ENTITY_ID, CONF_LATITUDE, CONF_LONGITUDE) DOMAIN = "demo" @@ -18,7 +18,7 @@ DOMAIN = "demo" DEPENDENCIES = [] COMPONENTS_WITH_DEMO_PLATFORM = [ - 'switch', 'light', 'thermostat', 'sensor', 'media_player'] + 'switch', 'light', 'thermostat', 'sensor', 'media_player', 'notify'] def setup(hass, config): @@ -29,16 +29,12 @@ def setup(hass, config): config.setdefault(ha.DOMAIN, {}) config.setdefault(DOMAIN, {}) - if config[DOMAIN].get('hide_demo_state') != '1': + if config[DOMAIN].get('hide_demo_state') != 1: hass.states.set('a.Demo_Mode', 'Enabled') # Setup sun - if CONF_LATITUDE not in config[ha.DOMAIN]: - config[ha.DOMAIN][CONF_LATITUDE] = '32.87336' - - if CONF_LONGITUDE not in config[ha.DOMAIN]: - config[ha.DOMAIN][CONF_LONGITUDE] = '-117.22743' - + config[ha.DOMAIN].setdefault(CONF_LATITUDE, '32.87336') + config[ha.DOMAIN].setdefault(CONF_LONGITUDE, '-117.22743') loader.get_component('sun').setup(hass, config) # Setup demo platforms @@ -52,18 +48,40 @@ def setup(hass, config): group.setup_group(hass, 'living room', [lights[0], lights[1], switches[0]]) group.setup_group(hass, 'bedroom', [lights[2], switches[1]]) - # Setup device tracker - hass.states.set("device_tracker.Paulus", "home", + # Setup scripts + bootstrap.setup_component( + hass, 'script', + {'script': + {'demo': { + 'alias': 'Demo {}'.format(lights[0]), + 'sequence': [{ + 'execute_service': 'light.turn_off', + 'service_data': {ATTR_ENTITY_ID: lights[0]} + }, { + 'delay': {'seconds': 5} + }, { + 'execute_service': 'light.turn_on', + 'service_data': {ATTR_ENTITY_ID: lights[0]} + }, { + 'delay': {'seconds': 5} + }, { + 'execute_service': 'light.turn_off', + 'service_data': {ATTR_ENTITY_ID: lights[0]} + }] + }}}) + + # Setup fake device tracker + hass.states.set("device_tracker.paulus", "home", {ATTR_ENTITY_PICTURE: "http://graph.facebook.com/schoutsen/picture"}) - hass.states.set("device_tracker.Anne_Therese", "not_home", + hass.states.set("device_tracker.anne_therese", "not_home", {ATTR_ENTITY_PICTURE: "http://graph.facebook.com/anne.t.frederiksen/picture"}) hass.states.set("group.all_devices", "home", { "auto": True, - "entity_id": [ + ATTR_ENTITY_ID: [ "device_tracker.Paulus", "device_tracker.Anne_Therese" ] diff --git a/homeassistant/components/light/demo.py b/homeassistant/components/light/demo.py index 88bc20e183b..4f25fef999d 100644 --- a/homeassistant/components/light/demo.py +++ b/homeassistant/components/light/demo.py @@ -30,6 +30,11 @@ class DemoLight(ToggleDevice): self._xy = xy or random.choice(LIGHT_COLORS) self._brightness = brightness + @property + def should_poll(self): + """ No polling needed for a demo light. """ + return False + @property def name(self): """ Returns the name of the device if any. """ diff --git a/homeassistant/components/media_player/demo.py b/homeassistant/components/media_player/demo.py index c093a8688c9..9ebdb85a92b 100644 --- a/homeassistant/components/media_player/demo.py +++ b/homeassistant/components/media_player/demo.py @@ -34,6 +34,11 @@ class DemoMediaPlayer(MediaPlayerDevice): self.media_title = media_title self.volume = 1.0 + @property + def should_poll(self): + """ No polling needed for a demo componentn. """ + return False + @property def name(self): """ Returns the name of the device. """ diff --git a/homeassistant/components/notify/demo.py b/homeassistant/components/notify/demo.py new file mode 100644 index 00000000000..c9afabd0a6b --- /dev/null +++ b/homeassistant/components/notify/demo.py @@ -0,0 +1,31 @@ +""" +homeassistant.components.notify.demo +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Demo notification service. +""" +from homeassistant.components.notify import ATTR_TITLE, BaseNotificationService + + +EVENT_NOTIFY = "notify" + + +def get_service(hass, config): + """ Get the demo notification service. """ + + return DemoNotificationService(hass) + + +# pylint: disable=too-few-public-methods +class DemoNotificationService(BaseNotificationService): + """ Implements demo notification service. """ + + def __init__(self, hass): + self.hass = hass + + def send_message(self, message="", **kwargs): + """ Send a message to a user. """ + + title = kwargs.get(ATTR_TITLE) + + self.hass.bus.fire(EVENT_NOTIFY, {"title": title, "message": message}) diff --git a/homeassistant/components/sensor/demo.py b/homeassistant/components/sensor/demo.py index b4547395240..3dc5dbd1998 100644 --- a/homeassistant/components/sensor/demo.py +++ b/homeassistant/components/sensor/demo.py @@ -21,6 +21,11 @@ class DemoSensor(Device): self._state = state self._unit_of_measurement = unit_of_measurement + @property + def should_poll(self): + """ No polling needed for a demo sensor. """ + return False + @property def name(self): """ Returns the name of the device. """ diff --git a/homeassistant/components/switch/demo.py b/homeassistant/components/switch/demo.py index b952504677e..a2009b0ec6e 100644 --- a/homeassistant/components/switch/demo.py +++ b/homeassistant/components/switch/demo.py @@ -18,6 +18,11 @@ class DemoSwitch(ToggleDevice): self._name = name or DEVICE_DEFAULT_NAME self._state = state + @property + def should_poll(self): + """ No polling needed for a demo switch. """ + return False + @property def name(self): """ Returns the name of the device if any. """ diff --git a/homeassistant/components/thermostat/demo.py b/homeassistant/components/thermostat/demo.py index 2a558ed442a..5f0898c3086 100644 --- a/homeassistant/components/thermostat/demo.py +++ b/homeassistant/components/thermostat/demo.py @@ -26,6 +26,11 @@ class DemoThermostat(ThermostatDevice): self._away = away self._current_temperature = current_temperature + @property + def should_poll(self): + """ No polling needed for a demo thermostat. """ + return False + @property def name(self): """ Returns the name. """ diff --git a/tests/test_component_demo.py b/tests/test_component_demo.py index d92b292f312..3107ba40833 100644 --- a/tests/test_component_demo.py +++ b/tests/test_component_demo.py @@ -63,12 +63,14 @@ class TestDemo(unittest.TestCase): self.assertEqual( STATE_OFF, self.hass.states.get(entity_id).state) - def test_hiding_demo_state(self): - """ Test if you can hide the demo card. """ - demo.setup(self.hass, {demo.DOMAIN: {'hide_demo_state': '1'}}) - - self.assertIsNone(self.hass.states.get('a.Demo_Mode')) - - demo.setup(self.hass, {demo.DOMAIN: {'hide_demo_state': '0'}}) + def test_if_demo_state_shows_by_default(self): + """ Test if demo state shows if we give no configuration. """ + demo.setup(self.hass, {demo.DOMAIN: {}}) self.assertIsNotNone(self.hass.states.get('a.Demo_Mode')) + + def test_hiding_demo_state(self): + """ Test if you can hide the demo card. """ + demo.setup(self.hass, {demo.DOMAIN: {'hide_demo_state': 1}}) + + self.assertIsNone(self.hass.states.get('a.Demo_Mode'))