{"action":"created","comment":{"url":"https://api.github.com/repos/balloob/home-assistant/pulls/comments/51352215","id":51352215,"diff_hunk":"@@ -0,0 +1,154 @@\n+\"\"\"\n+homeassistant.components.scsgate\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+Provides support for SCSGate components.\n+\n+For more details about this component, please refer to the documentation at\n+https://home-assistant.io/components/scsgate/\n+\"\"\"\n+import logging\n+from threading import Lock\n+from homeassistant.core import EVENT_HOMEASSISTANT_STOP\n+\n+from scsgate.connection import Connection\n+from scsgate.reactor import Reactor\n+from scsgate.tasks import GetStatusTask\n+from scsgate.messages import StateMessage, ScenarioTriggeredMessage\n+\n+DOMAIN = \"scsgate\"\n+\n+SCSGATE = None\n+_LOGGER = logging.getLogger(__name__)\n+\n+# pylint: disable=broad-except\n+\n+class SCSGate:\n+    \"\"\" Class dealing with the SCSGate device via scsgate.Reactor \"\"\"\n+\n+    def __init__(self, device, logger):\n+        self._logger = logger\n+        self._devices = {}\n+        self._devices_to_register = {}\n+        self._devices_to_register_lock = Lock()\n+        self._device_being_registered = None\n+        self._device_being_registered_lock = Lock()\n+\n+        connection = Connection(device=device, logger=self._logger)\n+        self._reactor = Reactor(\n+            connection=connection,\n+            logger=self._logger,\n+            handle_message=self.handle_message)\n+\n+    def handle_message(self, message):\n+        \"\"\" Method called whenever a message is seen on the bus \"\"\"\n+        self._logger.debug(\"Received message {}\".format(message))\n+        if not isinstance(message, StateMessage) and \\\n+           not isinstance(message, ScenarioTriggeredMessage):\n+            msg = \"Ignored message {} - not releavant type\".format(\n+                message)\n+            self._logger.debug(msg)\n+            return\n+\n+        if message.entity in self._devices:\n+            new_device_activated = False\n+            with self._devices_to_register_lock:\n+                if message.entity == self._device_being_registered:\n+                    self._device_being_registered = None\n+                    new_device_activated = True\n+            if new_device_activated:\n+                self._activate_next_device()\n+\n+            try:\n+                self._devices[message.entity].process_event(message)\n+            except Exception as exception:\n+                msg = \"Exception while processing event: {}\".format(\n+                    exception)\n+                self._logger.error(msg)\n+        else:\n+            self._logger.info(\n+                \"Ignoring state message for device {} because unknonw\".format(\n+                    message.entity))\n+\n+    @property\n+    def devices(self):\n+        \"\"\" Dictionary with known devices. Key is device ID,\n+        value is the device itself \"\"\"\n+        return self._devices\n+\n+    def add_device(self, device):\n+        \"\"\" Adds the specified device to the list of the already registered ones.\n+\n+        Beware: this is not what you usually want to do, take\n+        a look at `add_devices_to_register`\n+        \"\"\"\n+        self._devices[device.scs_id] = device\n+\n+    def add_devices_to_register(self, devices):\n+        \"\"\" List of devices to be registered.\n+\n+        Arguments:\n+        * devices: list of devices to register\n+        \"\"\"\n+        with self._devices_to_register_lock:\n+            for device in devices:\n+                self._devices_to_register[device.scs_id] = device\n+        self._activate_next_device()\n+\n+    def _activate_next_device(self):\n+        \"\"\" Starts the activation of the 1st device inside of self._devices \"\"\"\n+        with self._devices_to_register_lock:\n+            if len(self._devices_to_register) == 0:\n+                return\n+            _, device = self._devices_to_register.popitem()\n+            self._devices[device.scs_id] = device\n+            self._device_being_registered = device.scs_id\n+            self._reactor.append_task(GetStatusTask(target=device.scs_id))\n+\n+    def is_device_registered(self, device_id):\n+        \"\"\" Checks whether a device is already registered or not\n+\n+        Arguments:\n+        device_id: the ID of the device to look for\n+        \"\"\"\n+        with self._devices_to_register_lock:\n+            if device_id in self._devices_to_register.keys():\n+                return False\n+\n+        with self._device_being_registered_lock:\n+            if device_id == self._device_being_registered:\n+                return False\n+\n+        return True\n+\n+    def start(self):\n+        \"\"\" Start the scsgate.Reactor \"\"\"\n+        self._reactor.start()\n+\n+    def stop(self):\n+        \"\"\" Stop the scsgate.Reactor \"\"\"\n+        self._reactor.stop()\n+\n+    def append_task(self, task):\n+        \"\"\" Registers a new task to be executed \"\"\"\n+        self._reactor.append_task(task)\n+\n+\n+def setup(hass, config):\n+    \"\"\" Setup the SCSGate component. \"\"\"\n+\n+    _LOGGER.info(config)","path":"homeassistant/components/scsgate.py","position":139,"original_position":139,"commit_id":"89675688f25def8891210ed60ec6e803e1a9f775","original_commit_id":"89675688f25def8891210ed60ec6e803e1a9f775","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},"body":"This seems unnecessary. ","created_at":"2016-01-30T22:49:14Z","updated_at":"2016-01-30T22:49:14Z","html_url":"https://github.com/balloob/home-assistant/pull/1056#discussion_r51352215","pull_request_url":"https://api.github.com/repos/balloob/home-assistant/pulls/1056","_links":{"self":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/comments/51352215"},"html":{"href":"https://github.com/balloob/home-assistant/pull/1056#discussion_r51352215"},"pull_request":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/1056"}}},"pull_request":{"url":"https://api.github.com/repos/balloob/home-assistant/pulls/1056","id":57750854,"html_url":"https://github.com/balloob/home-assistant/pull/1056","diff_url":"https://github.com/balloob/home-assistant/pull/1056.diff","patch_url":"https://github.com/balloob/home-assistant/pull/1056.patch","issue_url":"https://api.github.com/repos/balloob/home-assistant/issues/1056","number":1056,"state":"open","locked":false,"title":"Add support for the SCSGate device","user":{"login":"flavio","id":22728,"avatar_url":"https://avatars.githubusercontent.com/u/22728?v=3","gravatar_id":"","url":"https://api.github.com/users/flavio","html_url":"https://github.com/flavio","followers_url":"https://api.github.com/users/flavio/followers","following_url":"https://api.github.com/users/flavio/following{/other_user}","gists_url":"https://api.github.com/users/flavio/gists{/gist_id}","starred_url":"https://api.github.com/users/flavio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flavio/subscriptions","organizations_url":"https://api.github.com/users/flavio/orgs","repos_url":"https://api.github.com/users/flavio/repos","events_url":"https://api.github.com/users/flavio/events{/privacy}","received_events_url":"https://api.github.com/users/flavio/received_events","type":"User","site_admin":false},"body":"This will allow home-assistant to interact with BTicino/Legrand MyHome system.\r\n\r\nSCSGate is a homebrew device that allows to interact with the MyHome bus. More information about the device can be found here (sorry, links have to be translated with Google because the pages are written in Italian):\r\n\r\n  * [device electronic schema](http://guidopic.altervista.org/eibscsgt/schema.pdf)\r\n  * [reverse engineering of the communication protocol](https://translate.google.com/translate?hl=en&sl=it&tl=en&u=http%3A%2F%2Fguidopic.altervista.org%2Falter%2Feibscsgt.html&sandbox=1)\r\n  * [device \"manual\"](https://translate.google.com/translate?hl=en&sl=it&tl=en&u=http%3A%2F%2Fguidopic.altervista.org%2Feibscsgt%2Finterface.html)\r\n\r\nTo interact with the device I create [this](https://github.com/flavio/scsgate) python module.\r\n\r\nSupported devices:\r\n  * lights\r\n  * switches\r\n  * scenario switches (this is a special type of switch which is part of the MyHome offering)\r\n  * roller shutters\r\n\r\nNot yet supported:\r\n  * heating (I've still to complete the reverse engineering of the protocol)\r\n\r\nI'll submit a PR to update the official documentation later on.","created_at":"2016-01-30T21:13:44Z","updated_at":"2016-01-30T22:49:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3330189493dd1c642a7efbab3225f41719198dae","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/balloob/home-assistant/pulls/1056/commits","review_comments_url":"https://api.github.com/repos/balloob/home-assistant/pulls/1056/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/1056/comments","statuses_url":"https://api.github.com/repos/balloob/home-assistant/statuses/89675688f25def8891210ed60ec6e803e1a9f775","head":{"label":"flavio:scsgate","ref":"scsgate","sha":"89675688f25def8891210ed60ec6e803e1a9f775","user":{"login":"flavio","id":22728,"avatar_url":"https://avatars.githubusercontent.com/u/22728?v=3","gravatar_id":"","url":"https://api.github.com/users/flavio","html_url":"https://github.com/flavio","followers_url":"https://api.github.com/users/flavio/followers","following_url":"https://api.github.com/users/flavio/following{/other_user}","gists_url":"https://api.github.com/users/flavio/gists{/gist_id}","starred_url":"https://api.github.com/users/flavio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flavio/subscriptions","organizations_url":"https://api.github.com/users/flavio/orgs","repos_url":"https://api.github.com/users/flavio/repos","events_url":"https://api.github.com/users/flavio/events{/privacy}","received_events_url":"https://api.github.com/users/flavio/received_events","type":"User","site_admin":false},"repo":{"id":50742298,"name":"home-assistant","full_name":"flavio/home-assistant","owner":{"login":"flavio","id":22728,"avatar_url":"https://avatars.githubusercontent.com/u/22728?v=3","gravatar_id":"","url":"https://api.github.com/users/flavio","html_url":"https://github.com/flavio","followers_url":"https://api.github.com/users/flavio/followers","following_url":"https://api.github.com/users/flavio/following{/other_user}","gists_url":"https://api.github.com/users/flavio/gists{/gist_id}","starred_url":"https://api.github.com/users/flavio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flavio/subscriptions","organizations_url":"https://api.github.com/users/flavio/orgs","repos_url":"https://api.github.com/users/flavio/repos","events_url":"https://api.github.com/users/flavio/events{/privacy}","received_events_url":"https://api.github.com/users/flavio/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/flavio/home-assistant","description":":house_with_garden: Open-source home automation platform running on Python 3","fork":true,"url":"https://api.github.com/repos/flavio/home-assistant","forks_url":"https://api.github.com/repos/flavio/home-assistant/forks","keys_url":"https://api.github.com/repos/flavio/home-assistant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/flavio/home-assistant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/flavio/home-assistant/teams","hooks_url":"https://api.github.com/repos/flavio/home-assistant/hooks","issue_events_url":"https://api.github.com/repos/flavio/home-assistant/issues/events{/number}","events_url":"https://api.github.com/repos/flavio/home-assistant/events","assignees_url":"https://api.github.com/repos/flavio/home-assistant/assignees{/user}","branches_url":"https://api.github.com/repos/flavio/home-assistant/branches{/branch}","tags_url":"https://api.github.com/repos/flavio/home-assistant/tags","blobs_url":"https://api.github.com/repos/flavio/home-assistant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/flavio/home-assistant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/flavio/home-assistant/git/refs{/sha}","trees_url":"https://api.github.com/repos/flavio/home-assistant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/flavio/home-assistant/statuses/{sha}","languages_url":"https://api.github.com/repos/flavio/home-assistant/languages","stargazers_url":"https://api.github.com/repos/flavio/home-assistant/stargazers","contributors_url":"https://api.github.com/repos/flavio/home-assistant/contributors","subscribers_url":"https://api.github.com/repos/flavio/home-assistant/subscribers","subscription_url":"https://api.github.com/repos/flavio/home-assistant/subscription","commits_url":"https://api.github.com/repos/flavio/home-assistant/commits{/sha}","git_commits_url":"https://api.github.com/repos/flavio/home-assistant/git/commits{/sha}","comments_url":"https://api.github.com/repos/flavio/home-assistant/comments{/number}","issue_comment_url":"https://api.github.com/repos/flavio/home-assistant/issues/comments{/number}","contents_url":"https://api.github.com/repos/flavio/home-assistant/contents/{+path}","compare_url":"https://api.github.com/repos/flavio/home-assistant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/flavio/home-assistant/merges","archive_url":"https://api.github.com/repos/flavio/home-assistant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/flavio/home-assistant/downloads","issues_url":"https://api.github.com/repos/flavio/home-assistant/issues{/number}","pulls_url":"https://api.github.com/repos/flavio/home-assistant/pulls{/number}","milestones_url":"https://api.github.com/repos/flavio/home-assistant/milestones{/number}","notifications_url":"https://api.github.com/repos/flavio/home-assistant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/flavio/home-assistant/labels{/name}","releases_url":"https://api.github.com/repos/flavio/home-assistant/releases{/id}","deployments_url":"https://api.github.com/repos/flavio/home-assistant/deployments","created_at":"2016-01-30T19:59:13Z","updated_at":"2016-01-30T19:59:15Z","pushed_at":"2016-01-30T21:05:54Z","git_url":"git://github.com/flavio/home-assistant.git","ssh_url":"git@github.com:flavio/home-assistant.git","clone_url":"https://github.com/flavio/home-assistant.git","svn_url":"https://github.com/flavio/home-assistant","homepage":"https://home-assistant.io","size":38631,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"dev"}},"base":{"label":"balloob:dev","ref":"dev","sha":"3e35bc06fc609a7d979afe4b25ed5a649fe72476","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-01-30T21:21:33Z","pushed_at":"2016-01-30T22:42:11Z","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":39633,"stargazers_count":2658,"watchers_count":2658,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":408,"mirror_url":null,"open_issues_count":130,"forks":408,"open_issues":130,"watchers":2658,"default_branch":"dev"}},"_links":{"self":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/1056"},"html":{"href":"https://github.com/balloob/home-assistant/pull/1056"},"issue":{"href":"https://api.github.com/repos/balloob/home-assistant/issues/1056"},"comments":{"href":"https://api.github.com/repos/balloob/home-assistant/issues/1056/comments"},"review_comments":{"href":"https://api.github.com/repos/balloob/home-assistant/pulls/1056/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/1056/commits"},"statuses":{"href":"https://api.github.com/repos/balloob/home-assistant/statuses/89675688f25def8891210ed60ec6e803e1a9f775"}}}}