mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-04-20 11:17:16 +00:00

* Update API for hass api v2 * fix lint * Refactory the old version of host_control * cleanup * Cleanup name inside addons/data * Cleanup name inside addons/data p2 * Rename api list * Fix path bug * Fix wrong config set
176 lines
4.8 KiB
Python
176 lines
4.8 KiB
Python
"""Bootstrap HassIO."""
|
|
import logging
|
|
import os
|
|
|
|
from .const import FILE_HASSIO_CONFIG, HASSIO_SHARE
|
|
from .tools import (
|
|
fetch_current_versions, write_json_file, read_json_file)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
HOMEASSISTANT_CONFIG = "{}/homeassistant"
|
|
HOMEASSISTANT_IMAGE = 'homeassistant_image'
|
|
HOMEASSISTANT_LAST = 'homeassistant_last'
|
|
|
|
HASSIO_SSL = "{}/ssl"
|
|
HASSIO_LAST = 'hassio_last'
|
|
HASSIO_CLEANUP = 'hassio_cleanup'
|
|
|
|
ADDONS_REPO = "{}/addons"
|
|
ADDONS_DATA = "{}/addons_data"
|
|
ADDONS_CUSTOM = "{}/addons_custom"
|
|
|
|
UPSTREAM_BETA = 'upstream_beta'
|
|
|
|
API_ENDPOINT = 'api_endpoint'
|
|
|
|
|
|
class Config(object):
|
|
"""Hold all config data."""
|
|
|
|
def __init__(self, config_file):
|
|
"""Initialize config object."""
|
|
self._filename = config_file
|
|
self._data = {}
|
|
|
|
# init or load data
|
|
if os.path.isfile(self._filename):
|
|
try:
|
|
self._data = read_json_file(self._filename)
|
|
except OSError:
|
|
_LOGGER.warning("Can't read %s", self._filename)
|
|
|
|
def save(self):
|
|
"""Store data to config file."""
|
|
if not write_json_file(self._filename, self._data):
|
|
_LOGGER.exception("Can't store config in %s", self._filename)
|
|
return False
|
|
return True
|
|
|
|
|
|
class CoreConfig(Config):
|
|
"""Hold all core config data."""
|
|
|
|
def __init__(self, websession):
|
|
"""Initialize config object."""
|
|
self.websession = websession
|
|
|
|
super().__init__(FILE_HASSIO_CONFIG)
|
|
|
|
# init data
|
|
if not self._data:
|
|
self._data.update({
|
|
HOMEASSISTANT_IMAGE: os.environ['HOMEASSISTANT_REPOSITORY'],
|
|
UPSTREAM_BETA: False,
|
|
})
|
|
self.save()
|
|
|
|
async def fetch_update_infos(self):
|
|
"""Read current versions from web."""
|
|
last = await fetch_current_versions(
|
|
self.websession, beta=self.upstream_beta)
|
|
|
|
if last:
|
|
self._data.update({
|
|
HOMEASSISTANT_LAST: last.get('homeassistant'),
|
|
HASSIO_LAST: last.get('hassio'),
|
|
})
|
|
self.save()
|
|
return True
|
|
|
|
return False
|
|
|
|
@property
|
|
def api_endpoint(self):
|
|
"""Return IP address of api endpoint."""
|
|
return self._data[API_ENDPOINT]
|
|
|
|
@api_endpoint.setter
|
|
def api_endpoint(self, value):
|
|
"""Store IP address of api endpoint."""
|
|
self._data[API_ENDPOINT] = value
|
|
|
|
@property
|
|
def upstream_beta(self):
|
|
"""Return True if we run in beta upstream."""
|
|
return self._data.get(UPSTREAM_BETA, False)
|
|
|
|
@upstream_beta.setter
|
|
def upstream_beta(self, value):
|
|
"""Set beta upstream mode."""
|
|
self._data[UPSTREAM_BETA] = bool(value)
|
|
|
|
@property
|
|
def hassio_cleanup(self):
|
|
"""Return Version they need to cleanup."""
|
|
return self._data.get(HASSIO_CLEANUP)
|
|
|
|
@hassio_cleanup.setter
|
|
def hassio_cleanup(self, version):
|
|
"""Set or remove cleanup flag."""
|
|
if version is None:
|
|
self._data.pop(HASSIO_CLEANUP, None)
|
|
else:
|
|
self._data[HASSIO_CLEANUP] = version
|
|
self.save()
|
|
|
|
@property
|
|
def homeassistant_image(self):
|
|
"""Return docker homeassistant repository."""
|
|
return self._data.get(HOMEASSISTANT_IMAGE)
|
|
|
|
@property
|
|
def last_homeassistant(self):
|
|
"""Actual version of homeassistant."""
|
|
return self._data.get(HOMEASSISTANT_LAST)
|
|
|
|
@property
|
|
def last_hassio(self):
|
|
"""Actual version of hassio."""
|
|
return self._data.get(HASSIO_LAST)
|
|
|
|
@property
|
|
def path_hassio_docker(self):
|
|
"""Return hassio data path extern for docker."""
|
|
return os.environ['SUPERVISOR_SHARE']
|
|
|
|
@property
|
|
def path_config_docker(self):
|
|
"""Return config path extern for docker."""
|
|
return HOMEASSISTANT_CONFIG.format(self.path_hassio_docker)
|
|
|
|
@property
|
|
def path_config(self):
|
|
"""Return config path inside supervisor."""
|
|
return HOMEASSISTANT_CONFIG.format(HASSIO_SHARE)
|
|
|
|
@property
|
|
def path_ssl_docker(self):
|
|
"""Return SSL path extern for docker."""
|
|
return HASSIO_SSL.format(self.path_hassio_docker)
|
|
|
|
@property
|
|
def path_ssl(self):
|
|
"""Return SSL path inside supervisor."""
|
|
return HASSIO_SSL.format(HASSIO_SHARE)
|
|
|
|
@property
|
|
def path_addons_repo(self):
|
|
"""Return git repo path for addons."""
|
|
return ADDONS_REPO.format(HASSIO_SHARE)
|
|
|
|
@property
|
|
def path_addons_custom(self):
|
|
"""Return path for customs addons."""
|
|
return ADDONS_CUSTOM.format(HASSIO_SHARE)
|
|
|
|
@property
|
|
def path_addons_data(self):
|
|
"""Return root addon data folder."""
|
|
return ADDONS_DATA.format(HASSIO_SHARE)
|
|
|
|
@property
|
|
def path_addons_data_docker(self):
|
|
"""Return root addon data folder extern for docker."""
|
|
return ADDONS_DATA.format(self.path_hassio_docker)
|