{"action":"created","comment":{"url":"https://api.github.com/repos/balloob/home-assistant/pulls/comments/51906651","id":51906651,"diff_hunk":"@@ -0,0 +1,170 @@\n+\"\"\"\n+homeassistant.components.switch.template\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+Allows the creation of a switch that integrates other components together\n+\n+For more details about this platform, please refer to the documentation at\n+https://home-assistant.io/components/switch.template/\n+\"\"\"\n+import logging\n+\n+from homeassistant.helpers.entity import generate_entity_id\n+\n+from homeassistant.components.switch import SwitchDevice\n+\n+from homeassistant.core import EVENT_STATE_CHANGED\n+from homeassistant.const import (\n+    STATE_ON,\n+    STATE_OFF,\n+    ATTR_FRIENDLY_NAME,\n+    CONF_VALUE_TEMPLATE)\n+\n+from homeassistant.helpers.service import call_from_config\n+\n+from homeassistant.util import template, slugify\n+\n+from homeassistant.exceptions import TemplateError\n+\n+from homeassistant.components.switch import DOMAIN\n+\n+ENTITY_ID_FORMAT = DOMAIN + '.{}'\n+\n+_LOGGER = logging.getLogger(__name__)\n+\n+CONF_SWITCHES = 'switches'\n+\n+STATE_ERROR = 'error'\n+\n+ON_ACTION = 'turn_on'\n+OFF_ACTION = 'turn_off'\n+\n+STATE_TRUE = 'True'\n+STATE_FALSE = 'False'\n+\n+\n+# pylint: disable=unused-argument\n+def setup_platform(hass, config, add_devices, discovery_info=None):\n+    \"\"\" Sets up the switches. \"\"\"\n+\n+    switches = []\n+    if config.get(CONF_SWITCHES) is None:\n+        _LOGGER.error(\"Missing configuration data for switch platform\")\n+        return False\n+\n+    for device, device_config in config[CONF_SWITCHES].items():\n+\n+        if device != slugify(device):\n+            _LOGGER.error(\"Found invalid key for switch.template: %s. \"\n+                          \"Use %s instead\", device, slugify(device))\n+            continue\n+\n+        if not isinstance(device_config, dict):\n+            _LOGGER.error(\"Missing configuration data for switch %s\", device)\n+            continue\n+\n+        friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)\n+        state_template = device_config.get(CONF_VALUE_TEMPLATE)\n+        on_action = device_config.get(ON_ACTION)\n+        off_action = device_config.get(OFF_ACTION)\n+        if state_template is None:\n+            _LOGGER.error(\n+                \"Missing %s for switch %s\", CONF_VALUE_TEMPLATE, device)\n+            continue\n+\n+        if on_action is None or off_action is None:\n+            _LOGGER.error(\n+                \"Missing action for switch %s\", device)\n+            continue\n+\n+        switches.append(\n+            SwitchTemplate(\n+                hass,\n+                device,\n+                friendly_name,\n+                state_template,\n+                on_action,\n+                off_action)\n+            )\n+    if not switches:\n+        _LOGGER.error(\"No switches added\")\n+        return False\n+    add_devices(switches)\n+    return True\n+\n+\n+class SwitchTemplate(SwitchDevice):\n+    \"\"\" Represents a Template Switch. \"\"\"\n+\n+    # pylint: disable=too-many-arguments\n+    def __init__(self,\n+                 hass,\n+                 device_id,\n+                 friendly_name,\n+                 state_template,\n+                 on_action,\n+                 off_action):\n+\n+        self.entity_id = generate_entity_id(\n+            ENTITY_ID_FORMAT, device_id,\n+            hass=hass)\n+\n+        self.hass = hass\n+        self._name = friendly_name\n+        self._template = state_template\n+        self._on_action = on_action\n+        self._off_action = off_action\n+        self.update()\n+\n+        def _update_callback(_event):\n+            \"\"\" Called when the target device changes state. \"\"\"\n+            # This can be called before the entity is properly\n+            # initialised, so check before updating state,\n+            if self.entity_id:\n+                self.update_ha_state(True)\n+\n+        self.hass.bus.listen(EVENT_STATE_CHANGED, _update_callback)\n+\n+    @property\n+    def name(self):\n+        \"\"\" Returns the name of the device. \"\"\"\n+        return self._name\n+\n+    @property\n+    def should_poll(self):\n+        \"\"\" Tells Home Assistant not to poll this entity. \"\"\"\n+        return False\n+\n+    def turn_on(self, **kwargs):\n+        \"\"\" Fires the on action. \"\"\"\n+        call_from_config(self.hass, self._on_action, True)\n+\n+    def turn_off(self, **kwargs):\n+        \"\"\" Fires the off action. \"\"\"\n+        call_from_config(self.hass, self._off_action, True)\n+\n+    @property\n+    def is_on(self):\n+        \"\"\" True if device is on. \"\"\"\n+        return self._state == STATE_TRUE or self._state == STATE_ON","path":"homeassistant/components/switch/template.py","position":142,"original_position":148,"commit_id":"ced380f0cda0566110c6e89eeeba84dcff71dbea","original_commit_id":"b20d3f8b3af41a233262175fbdc593be0fb61d9c","user":{"login":"pavoni","id":4487313,"avatar_url":"https://avatars.githubusercontent.com/u/4487313?v=3","gravatar_id":"","url":"https://api.github.com/users/pavoni","html_url":"https://github.com/pavoni","followers_url":"https://api.github.com/users/pavoni/followers","following_url":"https://api.github.com/users/pavoni/following{/other_user}","gists_url":"https://api.github.com/users/pavoni/gists{/gist_id}","starred_url":"https://api.github.com/users/pavoni/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pavoni/subscriptions","organizations_url":"https://api.github.com/users/pavoni/orgs","repos_url":"https://api.github.com/users/pavoni/repos","events_url":"https://api.github.com/users/pavoni/events{/privacy}","received_events_url":"https://api.github.com/users/pavoni/received_events","type":"User","site_admin":false},"body":"Perhaps it should be TEMPLATE_TRUE.","created_at":"2016-02-04T17:29:31Z","updated_at":"2016-02-04T17:29:31Z","html_url":"https://github.com/balloob/home-assistant/pull/1112#discussion_r51906651","pull_request_url":"https://api.github.com/repos/balloob/home-assistant/pulls/1112","_links":{"self":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/comments/51906651"},"html":{"href":"https://github.com/balloob/home-assistant/pull/1112#discussion_r51906651"},"pull_request":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/1112"}}},"pull_request":{"url":"https://api.github.com/repos/balloob/home-assistant/pulls/1112","id":58174922,"html_url":"https://github.com/balloob/home-assistant/pull/1112","diff_url":"https://github.com/balloob/home-assistant/pull/1112.diff","patch_url":"https://github.com/balloob/home-assistant/pull/1112.patch","issue_url":"https://api.github.com/repos/balloob/home-assistant/issues/1112","number":1112,"state":"open","locked":false,"title":"First cut of switch.template","user":{"login":"pavoni","id":4487313,"avatar_url":"https://avatars.githubusercontent.com/u/4487313?v=3","gravatar_id":"","url":"https://api.github.com/users/pavoni","html_url":"https://github.com/pavoni","followers_url":"https://api.github.com/users/pavoni/followers","following_url":"https://api.github.com/users/pavoni/following{/other_user}","gists_url":"https://api.github.com/users/pavoni/gists{/gist_id}","starred_url":"https://api.github.com/users/pavoni/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pavoni/subscriptions","organizations_url":"https://api.github.com/users/pavoni/orgs","repos_url":"https://api.github.com/users/pavoni/repos","events_url":"https://api.github.com/users/pavoni/events{/privacy}","received_events_url":"https://api.github.com/users/pavoni/received_events","type":"User","site_admin":false},"body":"A first implementation of the meta component described here https://github.com/balloob/home-assistant/issues/949. This is in the same family as `sensor.template`.\r\n\r\nThe main purpose is to allow you to integrate several devices into a single switch, but I expect it will have other uses.\r\n\r\nThe state of the switch is set from a template. Either a logical value, or `on` / `off` will set the value of the switch in the gui. Turning on or off the switch will fire an action in the same format as automation.\r\n\r\nBest to make sure firing the on or off actions will update the template, otherwise it doesn't make much of a switch!\r\n\r\nHere is an example that turns on and off a script (just as a demo)\r\n````\r\nswitch:\r\n    - platform: template\r\n      switches:\r\n          test_switch:\r\n              friendly_name: 'Slow script'\r\n              value_template: \"{{ states.script.slow.state }}\"\r\n              turn_on:\r\n                service: script.turn_on\r\n                entity_id: script.slow\r\n              turn_off:\r\n                service: script.turn_off\r\n                entity_id: script.slow\r\n````\r\n\r\nHere is something I expect to use. My skylight has a sensor to show if it's open or closed - and two momentary switches to operate the open and close. I can replace my custom component with something like this:- \r\n````\r\nswitch:\r\n    - platform: template\r\n      switches:\r\n          skylight:\r\n              value_template: \"{{ states.sensor.skylight == 'tripped' }}\"\r\n              turn_on:\r\n                service: switch.turn_on\r\n                entity_id: switch.skylight_open\r\n              turn_off:\r\n                service: switch.turn_on\r\n                entity_id: switch.skylight_close\r\n````","created_at":"2016-02-03T17:53:07Z","updated_at":"2016-02-04T17:29:31Z","closed_at":null,"merged_at":null,"merge_commit_sha":"365a390fef04a9776f8732f45a948a2d242fc4dd","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/balloob/home-assistant/pulls/1112/commits","review_comments_url":"https://api.github.com/repos/balloob/home-assistant/pulls/1112/comments","review_comment_url":"https://api.github.com/repos/balloob/home-assistant/pulls/comments{/number}","comments_url":"https://api.github.com/repos/balloob/home-assistant/issues/1112/comments","statuses_url":"https://api.github.com/repos/balloob/home-assistant/statuses/ced380f0cda0566110c6e89eeeba84dcff71dbea","head":{"label":"balloob:switch-template","ref":"switch-template","sha":"ced380f0cda0566110c6e89eeeba84dcff71dbea","user":{"login":"balloob","id":1444314,"avatar_url":"https://avatars.githubusercontent.com/u/1444314?v=3","gravatar_id":"","url":"https://api.github.com/users/balloob","html_url":"https://github.com/balloob","followers_url":"https://api.github.com/users/balloob/followers","following_url":"https://api.github.com/users/balloob/following{/other_user}","gists_url":"https://api.github.com/users/balloob/gists{/gist_id}","starred_url":"https://api.github.com/users/balloob/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/balloob/subscriptions","organizations_url":"https://api.github.com/users/balloob/orgs","repos_url":"https://api.github.com/users/balloob/repos","events_url":"https://api.github.com/users/balloob/events{/privacy}","received_events_url":"https://api.github.com/users/balloob/received_events","type":"User","site_admin":false},"repo":{"id":12888993,"name":"home-assistant","full_name":"balloob/home-assistant","owner":{"login":"balloob","id":1444314,"avatar_url":"https://avatars.githubusercontent.com/u/1444314?v=3","gravatar_id":"","url":"https://api.github.com/users/balloob","html_url":"https://github.com/balloob","followers_url":"https://api.github.com/users/balloob/followers","following_url":"https://api.github.com/users/balloob/following{/other_user}","gists_url":"https://api.github.com/users/balloob/gists{/gist_id}","starred_url":"https://api.github.com/users/balloob/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/balloob/subscriptions","organizations_url":"https://api.github.com/users/balloob/orgs","repos_url":"https://api.github.com/users/balloob/repos","events_url":"https://api.github.com/users/balloob/events{/privacy}","received_events_url":"https://api.github.com/users/balloob/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/balloob/home-assistant","description":":house_with_garden: Open-source home automation platform running on Python 3","fork":false,"url":"https://api.github.com/repos/balloob/home-assistant","forks_url":"https://api.github.com/repos/balloob/home-assistant/forks","keys_url":"https://api.github.com/repos/balloob/home-assistant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/balloob/home-assistant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/balloob/home-assistant/teams","hooks_url":"https://api.github.com/repos/balloob/home-assistant/hooks","issue_events_url":"https://api.github.com/repos/balloob/home-assistant/issues/events{/number}","events_url":"https://api.github.com/repos/balloob/home-assistant/events","assignees_url":"https://api.github.com/repos/balloob/home-assistant/assignees{/user}","branches_url":"https://api.github.com/repos/balloob/home-assistant/branches{/branch}","tags_url":"https://api.github.com/repos/balloob/home-assistant/tags","blobs_url":"https://api.github.com/repos/balloob/home-assistant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/balloob/home-assistant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/balloob/home-assistant/git/refs{/sha}","trees_url":"https://api.github.com/repos/balloob/home-assistant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/balloob/home-assistant/statuses/{sha}","languages_url":"https://api.github.com/repos/balloob/home-assistant/languages","stargazers_url":"https://api.github.com/repos/balloob/home-assistant/stargazers","contributors_url":"https://api.github.com/repos/balloob/home-assistant/contributors","subscribers_url":"https://api.github.com/repos/balloob/home-assistant/subscribers","subscription_url":"https://api.github.com/repos/balloob/home-assistant/subscription","commits_url":"https://api.github.com/repos/balloob/home-assistant/commits{/sha}","git_commits_url":"https://api.github.com/repos/balloob/home-assistant/git/commits{/sha}","comments_url":"https://api.github.com/repos/balloob/home-assistant/comments{/number}","issue_comment_url":"https://api.github.com/repos/balloob/home-assistant/issues/comments{/number}","contents_url":"https://api.github.com/repos/balloob/home-assistant/contents/{+path}","compare_url":"https://api.github.com/repos/balloob/home-assistant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/balloob/home-assistant/merges","archive_url":"https://api.github.com/repos/balloob/home-assistant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/balloob/home-assistant/downloads","issues_url":"https://api.github.com/repos/balloob/home-assistant/issues{/number}","pulls_url":"https://api.github.com/repos/balloob/home-assistant/pulls{/number}","milestones_url":"https://api.github.com/repos/balloob/home-assistant/milestones{/number}","notifications_url":"https://api.github.com/repos/balloob/home-assistant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/balloob/home-assistant/labels{/name}","releases_url":"https://api.github.com/repos/balloob/home-assistant/releases{/id}","deployments_url":"https://api.github.com/repos/balloob/home-assistant/deployments","created_at":"2013-09-17T07:29:48Z","updated_at":"2016-02-03T18:32:56Z","pushed_at":"2016-02-04T17:25:19Z","git_url":"git://github.com/balloob/home-assistant.git","ssh_url":"git@github.com:balloob/home-assistant.git","clone_url":"https://github.com/balloob/home-assistant.git","svn_url":"https://github.com/balloob/home-assistant","homepage":"https://home-assistant.io","size":40353,"stargazers_count":2678,"watchers_count":2678,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":417,"mirror_url":null,"open_issues_count":139,"forks":417,"open_issues":139,"watchers":2678,"default_branch":"dev"}},"base":{"label":"balloob:dev","ref":"dev","sha":"ce75c590b152c1751c3f3fcb12a45ca785bf724d","user":{"login":"balloob","id":1444314,"avatar_url":"https://avatars.githubusercontent.com/u/1444314?v=3","gravatar_id":"","url":"https://api.github.com/users/balloob","html_url":"https://github.com/balloob","followers_url":"https://api.github.com/users/balloob/followers","following_url":"https://api.github.com/users/balloob/following{/other_user}","gists_url":"https://api.github.com/users/balloob/gists{/gist_id}","starred_url":"https://api.github.com/users/balloob/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/balloob/subscriptions","organizations_url":"https://api.github.com/users/balloob/orgs","repos_url":"https://api.github.com/users/balloob/repos","events_url":"https://api.github.com/users/balloob/events{/privacy}","received_events_url":"https://api.github.com/users/balloob/received_events","type":"User","site_admin":false},"repo":{"id":12888993,"name":"home-assistant","full_name":"balloob/home-assistant","owner":{"login":"balloob","id":1444314,"avatar_url":"https://avatars.githubusercontent.com/u/1444314?v=3","gravatar_id":"","url":"https://api.github.com/users/balloob","html_url":"https://github.com/balloob","followers_url":"https://api.github.com/users/balloob/followers","following_url":"https://api.github.com/users/balloob/following{/other_user}","gists_url":"https://api.github.com/users/balloob/gists{/gist_id}","starred_url":"https://api.github.com/users/balloob/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/balloob/subscriptions","organizations_url":"https://api.github.com/users/balloob/orgs","repos_url":"https://api.github.com/users/balloob/repos","events_url":"https://api.github.com/users/balloob/events{/privacy}","received_events_url":"https://api.github.com/users/balloob/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/balloob/home-assistant","description":":house_with_garden: Open-source home automation platform running on Python 3","fork":false,"url":"https://api.github.com/repos/balloob/home-assistant","forks_url":"https://api.github.com/repos/balloob/home-assistant/forks","keys_url":"https://api.github.com/repos/balloob/home-assistant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/balloob/home-assistant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/balloob/home-assistant/teams","hooks_url":"https://api.github.com/repos/balloob/home-assistant/hooks","issue_events_url":"https://api.github.com/repos/balloob/home-assistant/issues/events{/number}","events_url":"https://api.github.com/repos/balloob/home-assistant/events","assignees_url":"https://api.github.com/repos/balloob/home-assistant/assignees{/user}","branches_url":"https://api.github.com/repos/balloob/home-assistant/branches{/branch}","tags_url":"https://api.github.com/repos/balloob/home-assistant/tags","blobs_url":"https://api.github.com/repos/balloob/home-assistant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/balloob/home-assistant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/balloob/home-assistant/git/refs{/sha}","trees_url":"https://api.github.com/repos/balloob/home-assistant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/balloob/home-assistant/statuses/{sha}","languages_url":"https://api.github.com/repos/balloob/home-assistant/languages","stargazers_url":"https://api.github.com/repos/balloob/home-assistant/stargazers","contributors_url":"https://api.github.com/repos/balloob/home-assistant/contributors","subscribers_url":"https://api.github.com/repos/balloob/home-assistant/subscribers","subscription_url":"https://api.github.com/repos/balloob/home-assistant/subscription","commits_url":"https://api.github.com/repos/balloob/home-assistant/commits{/sha}","git_commits_url":"https://api.github.com/repos/balloob/home-assistant/git/commits{/sha}","comments_url":"https://api.github.com/repos/balloob/home-assistant/comments{/number}","issue_comment_url":"https://api.github.com/repos/balloob/home-assistant/issues/comments{/number}","contents_url":"https://api.github.com/repos/balloob/home-assistant/contents/{+path}","compare_url":"https://api.github.com/repos/balloob/home-assistant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/balloob/home-assistant/merges","archive_url":"https://api.github.com/repos/balloob/home-assistant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/balloob/home-assistant/downloads","issues_url":"https://api.github.com/repos/balloob/home-assistant/issues{/number}","pulls_url":"https://api.github.com/repos/balloob/home-assistant/pulls{/number}","milestones_url":"https://api.github.com/repos/balloob/home-assistant/milestones{/number}","notifications_url":"https://api.github.com/repos/balloob/home-assistant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/balloob/home-assistant/labels{/name}","releases_url":"https://api.github.com/repos/balloob/home-assistant/releases{/id}","deployments_url":"https://api.github.com/repos/balloob/home-assistant/deployments","created_at":"2013-09-17T07:29:48Z","updated_at":"2016-02-03T18:32:56Z","pushed_at":"2016-02-04T17:25:19Z","git_url":"git://github.com/balloob/home-assistant.git","ssh_url":"git@github.com:balloob/home-assistant.git","clone_url":"https://github.com/balloob/home-assistant.git","svn_url":"https://github.com/balloob/home-assistant","homepage":"https://home-assistant.io","size":40353,"stargazers_count":2678,"watchers_count":2678,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":417,"mirror_url":null,"open_issues_count":139,"forks":417,"open_issues":139,"watchers":2678,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/1112"},"html":{"href":"https://github.com/balloob/home-assistant/pull/1112"},"issue":{"href":"https://api.github.com/repos/balloob/home-assistant/issues/1112"},"comments":{"href":"https://api.github.com/repos/balloob/home-assistant/issues/1112/comments"},"review_comments":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/1112/comments"},"review_comment":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/1112/commits"},"statuses":{"href":"https://api.github.com/repos/balloob/home-assistant/statuses/ced380f0cda0566110c6e89eeeba84dcff71dbea"}}}}