|
|
@@ -1,3 +1,5 @@
|
|
|
+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,76 +68,71 @@ 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)
|
|
|
+ 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 @@ class Helper():
|
|
|
remainder = (len(packet) + 4) % 16
|
|
|
if remainder:
|
|
|
packet += bytearray(16 - remainder)
|
|
|
- return packet
|
|
|
+ return packet
|