Commit a699d99f authored by Vassilis Panos's avatar Vassilis Panos
Browse files

Fix doing I/O

parent 16434129
Loading
Loading
Loading
Loading
+66 −68
Original line number Diff line number Diff line
import aiofiles
import aiohttp
import asyncio
import binascii
from distutils.version import StrictVersion
@@ -8,6 +10,7 @@ import requests
import struct
import voluptuous as vol

from aiohttp import ClientSession
from homeassistant.const import (
    ATTR_FRIENDLY_NAME, __version__ as current_ha_version)
import homeassistant.helpers.config_validation as cv
@@ -16,7 +19,7 @@ from homeassistant.helpers.typing import ConfigType
_LOGGER = logging.getLogger(__name__)

DOMAIN = 'smartir'
VERSION = '1.7.4'
VERSION = '1.7.5'
MANIFEST_URL = (
    "https://raw.githubusercontent.com/"
    "smartHomeHub/SmartIR/{}/"
@@ -65,18 +68,11 @@ async def async_setup(hass, config):

async def _update(hass, branch, do_update=False, notify_if_latest=True):
    try:
        request = requests.get(MANIFEST_URL.format(branch), stream=True, timeout=10)
    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
        async with aiohttp.ClientSession() as session:
            async with session.get(MANIFEST_URL.format(branch)) as response:
                if response.status == 200:
                    
    data = request.json()
                    data = await response.json(content_type='text/plain')
                    min_ha_version = data['homeassistant']
                    last_version = data['updater']['version']
                    release_notes = data['updater']['releaseNotes']
@@ -84,7 +80,8 @@ async def _update(hass, branch, do_update=False, notify_if_latest=True):
                    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')
                                "You're already using the latest version!", 
                                title='SmartIR')
                        return

                    if StrictVersion(current_ha_version) < StrictVersion(min_ha_version):
@@ -110,7 +107,7 @@ async def _update(hass, branch, do_update=False, notify_if_latest=True):
                            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)
                            await Helper.downloader(source, dest)
                        except:
                            has_errors = True
                            _LOGGER.error("Error updating %s. Please update the file manually.", file)
@@ -123,16 +120,17 @@ async def _update(hass, branch, do_update=False, notify_if_latest=True):
                        hass.components.persistent_notification.async_create(
                            "Successfully updated to {}. Please restart Home Assistant."
                            .format(last_version), title='SmartIR')
    except:
       _LOGGER.error("An error occurred while checking for updates.")

class Helper():
    @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)
    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")

+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
                            "smartHomeHub/SmartIR/master/"
                            "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:
            _LOGGER.error("There was an error while downloading the device Json file. " \
                          "Please check your internet connection or if the device code " \
+1 −1
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
                            "smartHomeHub/SmartIR/master/"
                            "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:
            _LOGGER.error("There was an error while downloading the device Json file. " \
                          "Please check your internet connection or if the device code " \
+3 −3
Original line number Diff line number Diff line
@@ -4,11 +4,11 @@
  "documentation": "https://github.com/smartHomeHub/SmartIR",
  "dependencies": [],
  "codeowners": ["@smartHomeHub"],
  "requirements": [],
  "requirements": ["aiofiles==0.5.0"],
  "homeassistant": "0.96.0",
  "updater": {
    "version": "1.7.4",
    "releaseNotes": "-- Fix Alexa volume set",
    "version": "1.7.5",
    "releaseNotes": "-- Fix doing I/O",
    "files": [
      "__init__.py",
      "climate.py",
+1 −1
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
                            "smartHomeHub/SmartIR/master/"
                            "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:
            _LOGGER.error("There was an error while downloading the device Json file. " \
                          "Please check your internet connection or if the device code " \