Browse Source

Fix doing I/O

Vassilis Panos 6 years ago
parent
commit
a699d99f58

+ 66 - 68
custom_components/smartir/__init__.py

@@ -1,3 +1,5 @@
+import aiofiles
+import aiohttp
 import asyncio
 import asyncio
 import binascii
 import binascii
 from distutils.version import StrictVersion
 from distutils.version import StrictVersion
@@ -8,6 +10,7 @@ import requests
 import struct
 import struct
 import voluptuous as vol
 import voluptuous as vol
 
 
+from aiohttp import ClientSession
 from homeassistant.const import (
 from homeassistant.const import (
     ATTR_FRIENDLY_NAME, __version__ as current_ha_version)
     ATTR_FRIENDLY_NAME, __version__ as current_ha_version)
 import homeassistant.helpers.config_validation as cv
 import homeassistant.helpers.config_validation as cv
@@ -16,7 +19,7 @@ from homeassistant.helpers.typing import ConfigType
 _LOGGER = logging.getLogger(__name__)
 _LOGGER = logging.getLogger(__name__)
 
 
 DOMAIN = 'smartir'
 DOMAIN = 'smartir'
-VERSION = '1.7.4'
+VERSION = '1.7.5'
 MANIFEST_URL = (
 MANIFEST_URL = (
     "https://raw.githubusercontent.com/"
     "https://raw.githubusercontent.com/"
     "smartHomeHub/SmartIR/{}/"
     "smartHomeHub/SmartIR/{}/"
@@ -65,76 +68,71 @@ async def async_setup(hass, config):
 
 
 async def _update(hass, branch, do_update=False, notify_if_latest=True):
 async def _update(hass, branch, do_update=False, notify_if_latest=True):
     try:
     try:
-        request = requests.get(MANIFEST_URL.format(branch), stream=True, timeout=10)
+        async with aiohttp.ClientSession() as session:
+            async with session.get(MANIFEST_URL.format(branch)) as response:
+                if response.status == 200:
+                    
+                    data = await response.json(content_type='text/plain')
+                    min_ha_version = data['homeassistant']
+                    last_version = data['updater']['version']
+                    release_notes = data['updater']['releaseNotes']
+
+                    if StrictVersion(last_version) <= StrictVersion(VERSION):
+                        if notify_if_latest:
+                            hass.components.persistent_notification.async_create(
+                                "You're already using the latest version!", 
+                                title='SmartIR')
+                        return
+
+                    if StrictVersion(current_ha_version) < StrictVersion(min_ha_version):
+                        hass.components.persistent_notification.async_create(
+                            "There is a new version of SmartIR integration, but it is **incompatible** "
+                            "with your system. Please first update Home Assistant.", title='SmartIR')
+                        return
+
+                    if do_update is False:
+                        hass.components.persistent_notification.async_create(
+                            "A new version of SmartIR integration is available ({}). "
+                            "Call the ``smartir.update_component`` service to update "
+                            "the integration. \n\n **Release notes:** \n{}"
+                            .format(last_version, release_notes), title='SmartIR')
+                        return
+
+                    # Begin update
+                    files = data['updater']['files']
+                    has_errors = False
+
+                    for file in files:
+                        try:
+                            source = REMOTE_BASE_URL.format(branch) + file
+                            dest = os.path.join(COMPONENT_ABS_DIR, file)
+                            os.makedirs(os.path.dirname(dest), exist_ok=True)
+                            await Helper.downloader(source, dest)
+                        except:
+                            has_errors = True
+                            _LOGGER.error("Error updating %s. Please update the file manually.", file)
+
+                    if has_errors:
+                        hass.components.persistent_notification.async_create(
+                            "There was an error updating one or more files of SmartIR. "
+                            "Please check the logs for more information.", title='SmartIR')
+                    else:
+                        hass.components.persistent_notification.async_create(
+                            "Successfully updated to {}. Please restart Home Assistant."
+                            .format(last_version), title='SmartIR')
     except:
     except:
-        _LOGGER.error("An error occurred while checking for updates. "
-                      "Please check your internet connection.")
-        return
-
-    if request.status_code != 200:
-        _LOGGER.error("Invalid response from the server while "
-                      "checking for a new version")
-        return
-
-    data = request.json()
-    min_ha_version = data['homeassistant']
-    last_version = data['updater']['version']
-    release_notes = data['updater']['releaseNotes']
-
-    if StrictVersion(last_version) <= StrictVersion(VERSION):
-        if notify_if_latest:
-            hass.components.persistent_notification.async_create(
-                "You're already using the latest version!", title='SmartIR')
-        return
-
-    if StrictVersion(current_ha_version) < StrictVersion(min_ha_version):
-        hass.components.persistent_notification.async_create(
-            "There is a new version of SmartIR integration, but it is **incompatible** "
-            "with your system. Please first update Home Assistant.", title='SmartIR')
-        return
-
-    if do_update is False:
-        hass.components.persistent_notification.async_create(
-            "A new version of SmartIR integration is available ({}). "
-            "Call the ``smartir.update_component`` service to update "
-            "the integration. \n\n **Release notes:** \n{}"
-            .format(last_version, release_notes), title='SmartIR')
-        return
-
-    # Begin update
-    files = data['updater']['files']
-    has_errors = False
-
-    for file in files:
-        try:
-            source = REMOTE_BASE_URL.format(branch) + file
-            dest = os.path.join(COMPONENT_ABS_DIR, file)
-            os.makedirs(os.path.dirname(dest), exist_ok=True)
-            Helper.downloader(source, dest)
-        except:
-            has_errors = True
-            _LOGGER.error("Error updating %s. Please update the file manually.", file)
-
-    if has_errors:
-        hass.components.persistent_notification.async_create(
-            "There was an error updating one or more files of SmartIR. "
-            "Please check the logs for more information.", title='SmartIR')
-    else:
-        hass.components.persistent_notification.async_create(
-            "Successfully updated to {}. Please restart Home Assistant."
-            .format(last_version), title='SmartIR')
+       _LOGGER.error("An error occurred while checking for updates.")
 
 
 class Helper():
 class Helper():
     @staticmethod
     @staticmethod
-    def downloader(source, dest):
-        req = requests.get(source, stream=True, timeout=10)
-
-        if req.status_code == 200:
-            with open(dest, 'wb') as fil:
-                for chunk in req.iter_content(1024):
-                    fil.write(chunk)
-        else:
-            raise Exception("File not found")
+    async def downloader(source, dest):
+        async with aiohttp.ClientSession() as session:
+            async with session.get(source) as response:
+                if response.status == 200:
+                    async with aiofiles.open(dest, mode='wb') as f:
+                        await f.write(await response.read())
+                else:
+                    raise Exception("File not found")
 
 
     @staticmethod
     @staticmethod
     def pronto2lirc(pronto):
     def pronto2lirc(pronto):
@@ -170,4 +168,4 @@ class Helper():
         remainder = (len(packet) + 4) % 16
         remainder = (len(packet) + 4) % 16
         if remainder:
         if remainder:
             packet += bytearray(16 - remainder)
             packet += bytearray(16 - remainder)
-        return packet
+        return packet

+ 1 - 1
custom_components/smartir/climate.py

@@ -68,7 +68,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
                             "smartHomeHub/SmartIR/master/"
                             "smartHomeHub/SmartIR/master/"
                             "codes/climate/{}.json")
                             "codes/climate/{}.json")
 
 
-            Helper.downloader(codes_source.format(device_code), device_json_path)
+            await Helper.downloader(codes_source.format(device_code), device_json_path)
         except:
         except:
             _LOGGER.error("There was an error while downloading the device Json file. " \
             _LOGGER.error("There was an error while downloading the device Json file. " \
                           "Please check your internet connection or if the device code " \
                           "Please check your internet connection or if the device code " \

+ 1 - 1
custom_components/smartir/fan.py

@@ -57,7 +57,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
                             "smartHomeHub/SmartIR/master/"
                             "smartHomeHub/SmartIR/master/"
                             "codes/fan/{}.json")
                             "codes/fan/{}.json")
 
 
-            Helper.downloader(codes_source.format(device_code), device_json_path)
+            await Helper.downloader(codes_source.format(device_code), device_json_path)
         except:
         except:
             _LOGGER.error("There was an error while downloading the device Json file. " \
             _LOGGER.error("There was an error while downloading the device Json file. " \
                           "Please check your internet connection or if the device code " \
                           "Please check your internet connection or if the device code " \

+ 3 - 3
custom_components/smartir/manifest.json

@@ -4,11 +4,11 @@
   "documentation": "https://github.com/smartHomeHub/SmartIR",
   "documentation": "https://github.com/smartHomeHub/SmartIR",
   "dependencies": [],
   "dependencies": [],
   "codeowners": ["@smartHomeHub"],
   "codeowners": ["@smartHomeHub"],
-  "requirements": [],
+  "requirements": ["aiofiles==0.5.0"],
   "homeassistant": "0.96.0",
   "homeassistant": "0.96.0",
   "updater": {
   "updater": {
-    "version": "1.7.4",
-    "releaseNotes": "-- Fix Alexa volume set",
+    "version": "1.7.5",
+    "releaseNotes": "-- Fix doing I/O",
     "files": [
     "files": [
       "__init__.py",
       "__init__.py",
       "climate.py",
       "climate.py",

+ 1 - 1
custom_components/smartir/media_player.py

@@ -62,7 +62,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
                             "smartHomeHub/SmartIR/master/"
                             "smartHomeHub/SmartIR/master/"
                             "codes/media_player/{}.json")
                             "codes/media_player/{}.json")
 
 
-            Helper.downloader(codes_source.format(device_code), device_json_path)
+            await Helper.downloader(codes_source.format(device_code), device_json_path)
         except:
         except:
             _LOGGER.error("There was an error while downloading the device Json file. " \
             _LOGGER.error("There was an error while downloading the device Json file. " \
                           "Please check your internet connection or if the device code " \
                           "Please check your internet connection or if the device code " \