Skip to content

Commit

Permalink
Fix doing I/O
Browse files Browse the repository at this point in the history
  • Loading branch information
vassilis-panos committed Apr 30, 2020
1 parent 1643412 commit a699d99
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 74 deletions.
134 changes: 66 additions & 68 deletions custom_components/smartir/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import aiofiles
import aiohttp
import asyncio
import binascii
from distutils.version import StrictVersion
Expand All @@ -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
Expand All @@ -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/{}/"
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -170,4 +168,4 @@ def lirc2broadlink(pulses):
remainder = (len(packet) + 4) % 16
if remainder:
packet += bytearray(16 - remainder)
return packet
return packet
2 changes: 1 addition & 1 deletion custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 " \
Expand Down
2 changes: 1 addition & 1 deletion custom_components/smartir/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 " \
Expand Down
6 changes: 3 additions & 3 deletions custom_components/smartir/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion custom_components/smartir/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 " \
Expand Down

0 comments on commit a699d99

Please sign in to comment.