Unverified Commit 83c191f9 authored by Vassilis Panos's avatar Vassilis Panos Committed by GitHub
Browse files

Add support for self-update

parent 6ac8cae7
Loading
Loading
Loading
Loading
+97 −1
Original line number Diff line number Diff line
import asyncio
import binascii
from distutils.version import StrictVersion
import json
import logging
import os.path
import requests
import struct
import voluptuous as vol

from homeassistant.const import (
    ATTR_FRIENDLY_NAME, __version__ as current_ha_version)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'smartir'
VERSION = '1.1.0'
VERSION_URL = (
    "https://raw.githubusercontent.com/smartHomeHub/SmartIR/master/version.json")
REMOTE_BASE_DIR = (
    "https://raw.githubusercontent.com/smartHomeHub/SmartIR/master/smartir/"
)
CONF_CHECK_UPDATES = 'check_updates'

CONFIG_SCHEMA = vol.Schema({
    DOMAIN: vol.Schema({
        vol.Optional(CONF_CHECK_UPDATES, default=True): cv.boolean
    })
}, extra=vol.ALLOW_EXTRA)

async def async_setup(hass, config):
    """Set up the SmartIR component."""
    conf = config.get(DOMAIN)

    async def check_updates(service):
        await _update(hass)

    async def update_component(service):
        await _update(hass, True)

    hass.services.async_register(DOMAIN, 'check_updates', check_updates)
    hass.services.async_register(DOMAIN, 'update_component', update_component)

    if conf[CONF_CHECK_UPDATES]:
        await _update(hass)

    return True

async def _update(hass, do_update = False):
    has_errors = False

    request = requests.get(VERSION_URL, stream=True, timeout=10)
    
    if request.status_code == 200:
        data = request.json()
        last_version = data['version']
        min_ha_version = data['minHAVersion']
        release_notes = data['releaseNotes']
        
        if StrictVersion(last_version) <= StrictVersion(VERSION):
            return
            
        if StrictVersion(current_ha_version) >= StrictVersion(min_ha_version):
            if do_update:
                files = data['files']
                abspath = os.path.dirname(os.path.abspath(__file__))

                for file in files:
                    try:
                        source = REMOTE_BASE_DIR + file
                        dest = os.path.join(abspath, file)
                        os.makedirs(os.path.dirname(dest), exist_ok=True)
                        Helper.downloader(source, dest)
                    except:
                        _LOGGER.error("Error updating %s. Please update the file manually.", file)
                        has_errors = True
            else:
                hass.components.persistent_notification.async_create(
                    release_notes, title='SmartIR')

        else:
            hass.components.persistent_notification.async_create(
                "There is a new version of SmartIR, but it is **incompatible** "
                "with your HA version. Please first update Home Assistant.", title='SmartIR')

    else:
        _LOGGER.error("Invalid response from the server while checking for a new version")
        has_errors = True

    if do_update:
        if has_errors:
            hass.components.persistent_notification.async_create(
                "There was an error updating 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')

class Helper():
    @staticmethod
@@ -14,7 +111,6 @@ class Helper():
        else:
            raise Exception('File not found')


    @staticmethod
    def pronto2lirc(pronto):
        codes = [int(binascii.hexlify(pronto[i:i+2]), 16) for i in range(0, len(pronto), 2)]