From a699d99f587731e2bf302aa0c63eca09dbea7c1b Mon Sep 17 00:00:00 2001 From: Vassilis Panos <4130346+vassilis-panos@users.noreply.github.com> Date: Fri, 1 May 2020 01:27:05 +0300 Subject: [PATCH] Fix doing I/O --- custom_components/smartir/__init__.py | 134 +++++++++++----------- custom_components/smartir/climate.py | 2 +- custom_components/smartir/fan.py | 2 +- custom_components/smartir/manifest.json | 6 +- custom_components/smartir/media_player.py | 2 +- 5 files changed, 72 insertions(+), 74 deletions(-) diff --git a/custom_components/smartir/__init__.py b/custom_components/smartir/__init__.py index 465b97b1..a0df8207 100644 --- a/custom_components/smartir/__init__.py +++ b/custom_components/smartir/__init__.py @@ -1,3 +1,5 @@ +import aiofiles +import aiohttp import asyncio import binascii from distutils.version import StrictVersion @@ -8,6 +10,7 @@ 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 @@ _LOGGER = logging.getLogger(__name__) DOMAIN = 'smartir' -VERSION = '1.7.4' +VERSION = '1.7.5' MANIFEST_URL = ( "https://raw.githubusercontent.com/" "smartHomeHub/SmartIR/{}/" @@ -65,76 +68,71 @@ async def _update_component(service): async def _update(hass, branch, do_update=False, notify_if_latest=True): 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: - _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(): @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 def pronto2lirc(pronto): @@ -170,4 +168,4 @@ def lirc2broadlink(pulses): remainder = (len(packet) + 4) % 16 if remainder: packet += bytearray(16 - remainder) - return packet + return packet \ No newline at end of file diff --git a/custom_components/smartir/climate.py b/custom_components/smartir/climate.py index bd5b9fd9..8dd4f6c1 100644 --- a/custom_components/smartir/climate.py +++ b/custom_components/smartir/climate.py @@ -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 " \ diff --git a/custom_components/smartir/fan.py b/custom_components/smartir/fan.py index a551b4af..f595862a 100644 --- a/custom_components/smartir/fan.py +++ b/custom_components/smartir/fan.py @@ -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 " \ diff --git a/custom_components/smartir/manifest.json b/custom_components/smartir/manifest.json index 5526273a..ca0b2597 100644 --- a/custom_components/smartir/manifest.json +++ b/custom_components/smartir/manifest.json @@ -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", diff --git a/custom_components/smartir/media_player.py b/custom_components/smartir/media_player.py index 21892288..170afbce 100644 --- a/custom_components/smartir/media_player.py +++ b/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/" "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 " \