Skip to content

Commit

Permalink
Initial Release, v3.0.0 beta 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gcobb321 committed Dec 17, 2022
1 parent ce3c419 commit 0f6222d
Show file tree
Hide file tree
Showing 215 changed files with 14,386 additions and 22,128 deletions.
206 changes: 206 additions & 0 deletions custom_components/icloud3/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
"""GitHub Custom Component."""
import asyncio

from homeassistant.components import history
from homeassistant.const import CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, CONF_INCLUDE


from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CoreState, HomeAssistant
from homeassistant.helpers.typing import ConfigType
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
from homeassistant.helpers.aiohttp_client import async_get_clientsession
#from homeassistant.setup import async_setup_component
from homeassistant.helpers import collection, storage
import homeassistant.util.location as ha_location_info
import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
import voluptuous as vol
import os

from .const import (DOMAIN, PLATFORMS, MODE_PLATFORM, MODE_INTEGRATION,CONF_VERSION,
EVLOG_IC3_STARTING, )

from .global_variables import GlobalVariables as Gb
from .helpers.common import (instr, )
from .helpers.messaging import (_traceha, log_info_msg, log_warning_msg, log_error_msg, log_exception)
from .support.v2v3_config_migration import iCloud3_v2v3ConfigMigration
from .support import start_ic3
from .support import config_file
from .support import restore_state
from .support import service_handler
from .support import pyicloud_ic3_interface
from .support import event_log
from .icloud3_main import iCloud3

import logging
# _LOGGER = logging.getLogger(__name__)
Gb.HALogger = logging.getLogger(f"icloud3")
successful_startup = True

#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><><><><><><><><><>
#
# PLATFORM MODE - STARTED FROM CONFIGURATION.YAML 'DEVICE_TRACKER/PLATFORM: ICLOUD3 STATEMENT
#
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><><><><><><><><><>

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
if 'device_tracker' not in config:
return True

hass.data.setdefault(DOMAIN, {})

Gb.hass = hass
Gb.config = config
Gb.ha_config_platform_stmt = True
Gb.operating_mode = MODE_PLATFORM
log_info_msg(f"Initializing iCloud3 {Gb.version} - Using Platform method")

start_ic3.initialize_directory_filenames()
config_file.load_storage_icloud3_configuration_file()

# Convert the .storage/icloud3.configuration file if it is at a default
# state or has never been updated via config_flow using 'HA Integrations > iCloud3'
if Gb.conf_profile[CONF_VERSION] == -1:
config_file.load_icloud3_ha_config_yaml(config)
v2v3_config_migration = iCloud3_v2v3ConfigMigration()
v2v3_config_migration.convert_v2_config_files_to_v3()
Gb.v2v3_config_migrated = True

return True


#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><><><><><><><><><>
#
# INTEGRATION MODE - STARTED FROM CONFIGURATION > INTEGRATIONS ENTRY
#
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><><><><><><><><><>

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
""" Set up the iCloud3 integration from a ConfigEntry integration """

try:

log_info_msg(f"Setting up iCloud3 {Gb.version} - Using Integration method")

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.unique_id] = DOMAIN
if entry.unique_id is None:
hass.config_entries.async_update_entry(entry, unique_id=DOMAIN)
hass_data = dict(entry.data)

# Get location information from ha
if location_info := await ha_location_info.async_detect_location_info(
async_get_clientsession(hass)):
Gb.location_info = {
"country_code": location_info.country_code,
"region_code": location_info.region_code,
"zip_code": location_info.zip_code,
"region_name": location_info.region_name,
"city": location_info.city,
"time_zone": location_info.time_zone,
"latitude": location_info.latitude,
"longitude": location_info.longitude,
"use_metric": location_info.use_metric,
}

Gb.hass = hass
Gb.config_entry = entry
Gb.entry_id = entry.entry_id
Gb.operating_mode = MODE_INTEGRATION
Gb.start_icloud3_inprocess_flag = True

start_ic3.initialize_directory_filenames()
config_file.load_storage_icloud3_configuration_file()
start_ic3.set_icloud_username_password()
restore_state.load_storage_icloud3_restore_state_file()
hass.config_entries.async_setup_platforms(entry, PLATFORMS)

#----- hass test - start


#----- hass test - end

# conf_version goes from:
# -1 --> 0 (default version installed) --> (v2 migrated to v3)
# 0 --> 1 (configurator/config_flow opened and configuration file was accessed/updated).
if (Gb.conf_profile[CONF_VERSION] == -1
and Gb.v2v3_config_migrated is False):
config_file.load_icloud3_ha_config_yaml('')
v2v3_config_migration = iCloud3_v2v3ConfigMigration()
v2v3_config_migration.convert_v2_config_files_to_v3()
Gb.v2v3_config_migrated = True

# Do not start if loading/initialization failed
if successful_startup is False:
log_error_msg(f"iCloud3 Initialization Failed, configuration file "
f"{Gb.icloud3_config_filename} failed to load.")
log_error_msg("Verify the configuration file and delete it manually if necessary")
return False

# Store a reference to the unsubscribe function to cleanup if an entry is unloaded.
unsub_options_update_listener = entry.add_update_listener(options_update_listener)
hass_data["unsub_options_update_listener"] = unsub_options_update_listener
hass.data[DOMAIN][entry.entry_id] = hass_data

except Exception as err:
log_exception(err)
return False

#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#
# SETUP PROCESS TO START ICLOUD3
#
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
async def start_icloud3(event=None):
Gb.initial_icloud3_loading_flag = True
icloud3_started = await Gb.hass.async_add_executor_job(Gb.iCloud3.start_icloud3)

if icloud3_started:
log_info_msg(f"iCloud3 {Gb.version} started")
else:
log_error_msg(f"iCloud3 {Gb.version} Initialization Failed")

Gb.PyiCloud = None
Gb.EvLog = event_log.EventLog(Gb.hass)
Gb.EvLog.display_user_message('iCloud3 is Starting')
Gb.EvLog.post_event(f"{EVLOG_IC3_STARTING}Initializing iCloud3 v{Gb.version} > "
f"{dt_util.now().strftime('%A, %b %d')}")
Gb.iCloud3 = iCloud3()

# These will run concurrently while HA is starting everything else
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, start_ic3.ha_startup_completed)
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, start_ic3.ha_stopping)

hass.async_add_executor_job(service_handler.register_icloud3_services)
if Gb.data_source_use_icloud:
hass.async_add_executor_job(pyicloud_ic3_interface.create_PyiCloud_service_executor_job)

if hass.state == CoreState.running:
start_icloud3()
else:
# This is fired from sensor.py and device_tracker.py when all the sensors
# and device_trackers have been set up
hass.bus.async_listen_once('start_icloud3', start_icloud3)

return True

#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><><><><><><><><><>
async def options_update_listener(hass: HomeAssistant, config_entry: ConfigEntry):
"""Handle options update."""
await hass.config_entries.async_reload(config_entry.entry_id)

#-------------------------------------------------------------------------------------------
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(await asyncio.gather(
*[hass.config_entries.async_forward_entry_unload(entry, "sensor")]))

# Remove options_update_listener.
hass.data[DOMAIN][entry.entry_id]["unsub_options_update_listener"]()

# Remove config entry from domain.
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok
Loading

0 comments on commit 0f6222d

Please sign in to comment.