Skip to content

Commit

Permalink
iCloud3 v3.0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gcobb321 committed Mar 24, 2024
1 parent 8525360 commit 040c626
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 86 deletions.
2 changes: 1 addition & 1 deletion custom_components/icloud3/ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v3.0.1 - 3/15/2024
v3.0.1 - 3/20/2024
.......................
1. UPDATE SENSOR & DEVICE TRACKER ENTITIES (Improvement) - Changed the method of updating these items at the request of @balloob (Paulus Schousten, HA Founder) to prevent a potential Home Assistant lockup when doing an update.
2. LOG FILE (Improvement) - Changed the method of updating the iCloud3 log files. Also changed some of the record formatting to improve readability.
Expand Down
27 changes: 15 additions & 12 deletions custom_components/icloud3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
# from .const_sensor import (HA_EXCLUDE_SENSORS, )

from .global_variables import GlobalVariables as Gb
from .helpers.messaging import (_trace, _traceha, open_ic3_log_file_init,
from .helpers.messaging import (_trace, _traceha, open_ic3log_file_init,
post_alert, post_startup_alert,
log_info_msg, log_debug_msg, log_error_msg, log_exception)
log_info_msg, log_debug_msg, log_error_msg,
log_exception_HA, log_exception)
from .helpers.time_util import (time_now_secs, )
from .support.v2v3_config_migration import iCloud3_v2v3ConfigMigration
from .support import start_ic3
Expand All @@ -43,7 +44,8 @@
from .icloud3_main import iCloud3
from . import config_flow

Gb.HALogger = logging.getLogger(__name__)
Gb.HARootLogger = logging.getLogger("")
Gb.HALogger = logging.getLogger(__name__)

successful_startup = True

Expand Down Expand Up @@ -76,7 +78,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
Gb.HALogger.info(f"Initializing iCloud3 v{VERSION} - Remove Platform: iCloud3 statement")

except Exception as err:
log_exception(err)
# log_exception(err)
# log_exception_HA(err)
pass


return True
Expand Down Expand Up @@ -128,15 +132,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
Gb.entry_id = entry.entry_id
Gb.operating_mode = MODE_INTEGRATION
Gb.PyiCloud = None
Gb.start_icloud3_inprocess_flag = True

start_ic3.initialize_directory_filenames()

await Gb.hass.async_add_executor_job(
config_file.load_storage_icloud3_configuration_file)

start_ic3.set_log_level(Gb.log_level)

Gb.evlog_btnconfig_url = Gb.conf_profile[CONF_EVLOG_BTNCONFIG_URL].strip()
Gb.evlog_version = Gb.conf_profile['event_log_version']
Gb.EvLog = event_log.EventLog(Gb.hass)

await Gb.hass.async_add_executor_job(
open_ic3_log_file_init)
open_ic3log_file_init)

Gb.HALogger.info(f"Setting up iCloud3 v{Gb.version}")
log_info_msg(f"Setting up iCloud3 v{VERSION}")
Expand All @@ -150,13 +160,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

recorder_prefilter.add_filter(hass, [SENSOR_EVENT_LOG_NAME])

Gb.start_icloud3_inprocess_flag = True
Gb.evlog_btnconfig_url = Gb.conf_profile[CONF_EVLOG_BTNCONFIG_URL].strip()
Gb.evlog_version = Gb.conf_profile['event_log_version']
Gb.EvLog = event_log.EventLog(Gb.hass)
# log_info_msg(f"Setting up iCloud3 v{VERSION} - Using Integration method")
# Gb.HALogger.info(f"Setting up iCloud3 v{Gb.version}")

start_ic3.initialize_icloud_data_source()
await Gb.hass.async_add_executor_job(
restore_state.load_storage_icloud3_restore_state_file)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/icloud3/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

VERSION = '3.0.1'
VERSION = '3.0.1.1'
#-----------------------------------------
DOMAIN = 'icloud3'
ICLOUD3 = 'iCloud3'
Expand All @@ -27,7 +27,7 @@
HA_CONFIG_IC3_URL = '/config/integrations/integration/icloud3'
WAZE_LOCATION_HISTORY_DATABASE = 'icloud3.waze_location_history.db'
SENSOR_WAZEHIST_TRACK_NAME = 'icloud3_wazehist_track'
IC3_LOG_FILENAME = 'icloud3-0.log'
IC3LOG_FILENAME = 'icloud3-0.log'
PICTURE_WWW_STANDARD_DIRS = 'www/icloud3, www/community, www/images, www/custom_cards'

DEVICE_TRACKER = 'device_tracker'
Expand Down
28 changes: 14 additions & 14 deletions custom_components/icloud3/global_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,20 @@ class GlobalVariables(object):
MobileApp_devices = {} # mobile_app Integration devices dict from hass.data['mobile_app']['devices]


EvLog = None
EvLogSensor = None
HALogger = None
iC3Logger = None
EvLog = None
EvLogSensor = None
HARootLogger = None
HALogger = None
iC3Logger = None
iC3Logger_last_check_exist_secs = 0
# iC3Logger_last_recd = None
#iC3_LogFile = None
Sensors = None
iC3EntityPlatform = None # iCloud3 Entity Platform (homeassistant.helpers.entity_component)
PyiCloud = None # iCloud Account service
PyiCloudInit = None # iCloud Account service when started from __init__ via executive job
PyiCloudConfigFlow = None # iCloud Account service when started from config_flow
Waze = None
WazeHist = None

iC3EntityPlatform = None # iCloud3 Entity Platform (homeassistant.helpers.entity_component)
PyiCloud = None # iCloud Account service
PyiCloudInit = None # iCloud Account service when started from __init__ via executive job
PyiCloudConfigFlow = None # iCloud Account service when started from config_flow

Waze = None
WazeHist = None
WazeHistTrackSensor = None # Sensor for updating the lat/long values for the WazeHist Map display

operating_mode = 0 # Platform (Legacy using configuration.yaml) or Integration
Expand Down Expand Up @@ -197,7 +197,7 @@ class GlobalVariables(object):
evlog_trk_monitors_flag = False
info_notification = ''
ha_notification = {}
trace_prefix = ''
trace_prefix = '_INIT_'
trace_group = False
trace_text_change_1 = ''
trace_text_change_2 = ''
Expand Down
109 changes: 63 additions & 46 deletions custom_components/icloud3/helpers/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..const import (DOT, ICLOUD3_ERROR_MSG, EVLOG_DEBUG, EVLOG_ERROR, EVLOG_INIT_HDR, EVLOG_MONITOR,
EVLOG_TIME_RECD, EVLOG_UPDATE_HDR, EVLOG_UPDATE_START, EVLOG_UPDATE_END,
EVLOG_ALERT, EVLOG_WARNING, EVLOG_HIGHLIGHT, EVLOG_IC3_STARTING,EVLOG_IC3_STAGE_HDR,
IC3_LOG_FILENAME, EVLOG_TIME_RECD,
IC3LOG_FILENAME, EVLOG_TIME_RECD,
CRLF, CRLF_DOT, NBSP, NBSP2, NBSP3, NBSP4, NBSP5, NBSP6, CRLF_INDENT,
DASH_50, DASH_DOTTED_50, TAB_11, RED_ALERT, RED_STOP, RED_CIRCLE, YELLOW_ALERT,
DATETIME_FORMAT, DATETIME_ZERO,
Expand Down Expand Up @@ -246,16 +246,16 @@ def more_info(key):
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


def open_ic3_log_file_init():
def open_ic3log_file_init():
'''
Entry point for async_add_executor_job in __init__.py
'''
open_ic3_log_file(new_log_file=Gb.log_debug_flag)
open_ic3log_file(new_log_file=Gb.log_debug_flag)

#------------------------------------------------------------------------------
def open_ic3_log_file(new_log_file=False):
def open_ic3log_file(new_log_file=False):

ic3logger_file = Gb.hass.config.path(IC3_LOG_FILENAME)
ic3logger_file = Gb.hass.config.path(IC3LOG_FILENAME)
filemode = 'w' if new_log_file else 'a'

if Gb.iC3Logger is None or new_log_file:
Expand All @@ -265,34 +265,46 @@ def open_ic3_log_file(new_log_file=False):
fileHandler.setFormatter(formatter)
Gb.iC3Logger.addHandler(fileHandler)
Gb.iC3Logger.propagate = (Gb.conf_general[CONF_LOG_LEVEL] == 'debug-ha')
Gb.iC3Logger.setLevel(logging.INFO)

write_config_file_to_ic3_log()
write_config_file_to_ic3log()

#--------------------------------------------------------------------
def write_ic3_log_recd(log_msg):
def write_ic3log_recd(log_msg):
'''
Check to make sure the icloud3-0.log file exists if the last write to it
was moe than 2-secs ago. Recreate it if it does not. This catches deletes
and renames while iCloud3 is running
'''
time_now_msecs = time.time()
if time_now_msecs - Gb.iC3Logger_last_check_exist_secs > 2:
Gb.iC3Logger_last_check_exist_secs = time_now_msecs
try:
time_now_msecs = time.time()
if time_now_msecs - Gb.iC3Logger_last_check_exist_secs > 2:
Gb.iC3Logger_last_check_exist_secs = time_now_msecs

check_log_file_exists(Gb.hass.config.path(IC3_LOG_FILENAME))
check_ic3log_file_exists(Gb.hass.config.path(IC3LOG_FILENAME))

Gb.iC3Logger.info(log_msg)
if Gb.iC3Logger:
Gb.iC3Logger.info(log_msg)
else:
open_ic3log_file(new_log_file=True)
Gb.iC3Logger.info(log_msg)

except Exception as err:
return False

#--------------------------------------------------------------------
def check_log_file_exists(ic3logger_file):
def check_ic3log_file_exists(ic3logger_file):
'''
See if the icloud3-0.log file exists. Recreate it if it does not. This
catches deletes and renames while iCloud3 is running
'''
try:
if Gb.iC3Logger and os.path.isfile(ic3logger_file) is False:
if Gb.iC3Logger is None:
open_ic3log_file(new_log_file=True)

elif Gb.iC3Logger and os.path.isfile(ic3logger_file) is False:
Gb.iC3Logger.removeHandler(Gb.iC3Logger.handlers[0])
open_ic3_log_file(new_log_file=True)
open_ic3log_file(new_log_file=True)

log_msg = f"{EVLOG_IC3_STARTING}Recreated iCloud3 Log File: {ic3logger_file}"
log_msg = f" {format_startup_header_box(log_msg)}"
Expand All @@ -310,17 +322,17 @@ def check_log_file_exists(ic3logger_file):


#--------------------------------------------------------------------
def archive_log_file():
def archive_ic3log_file():
'''
At midnight, archive the log files and create a new one for the current day
Remove icloud-2.log,
rename icloud3-1.log to icloud3_2.log,
rename icloud3-0.log to icloud3-1.log
'''
try:
log_file_0 = Gb.hass.config.path(IC3_LOG_FILENAME)
log_file_1 = Gb.hass.config.path(IC3_LOG_FILENAME).replace('-0.', '-1.')
log_file_2 = Gb.hass.config.path(IC3_LOG_FILENAME).replace('-0.', '-2.')
log_file_0 = Gb.hass.config.path(IC3LOG_FILENAME)
log_file_1 = Gb.hass.config.path(IC3LOG_FILENAME).replace('-0.', '-1.')
log_file_2 = Gb.hass.config.path(IC3LOG_FILENAME).replace('-0.', '-2.')

post_event(f"iCloud3 Log File Archived")

Expand All @@ -331,40 +343,44 @@ def archive_log_file():
Gb.iC3Logger.removeHandler(Gb.iC3Logger.handlers[0])
os.rename(log_file_0, log_file_1)

open_ic3_log_file(new_log_file=True)
open_ic3log_file(new_log_file=True)

except Exception as err:
post_event(f"iCloud3 Log File Archive encountered an error > {err}")

#------------------------------------------------------------------------------
def write_config_file_to_ic3_log():
def write_config_file_to_ic3log():

conf_tracking_recd = Gb.conf_tracking.copy()
conf_tracking_recd[CONF_USERNAME] = obscure_field(conf_tracking_recd[CONF_USERNAME])
conf_tracking_recd[CONF_PASSWORD] = obscure_field(conf_tracking_recd[CONF_PASSWORD])
conf_tracking_recd[CONF_DEVICES] = f"{len(Gb.conf_devices)}"

recd = (f"iCloud3 v{Gb.version}, "
f"{dt_util.now().strftime('%A')}, "
f"{dt_util.now().strftime(DATETIME_FORMAT)[:19]}")
tabs = f"{'\t'*4}"
recd = (f"\t{DASH_50}\n"
f"{tabs}{recd}\n"
f"{tabs}{DASH_50}")
write_ic3_log_recd(recd)
Gb.trace_prefix = '_INIT_'
tabs = TABS_BOX_DEBUG if Gb.log_debug_flag else \
TABS_BOX_INFO
log_msg = ( f"iCloud3 v{Gb.version}, "
f"{dt_util.now().strftime('%A')}, "
f"{dt_util.now().strftime(DATETIME_FORMAT)[:19]}")
log_msg = ( f" \n"
f"{tabs}\t\t{DASH_50}\n"
f"{tabs}\t\t{log_msg}\n"
f"{tabs}\t\t{DASH_50}")

Gb.iC3Logger.info(log_msg)

# Write the ic3 configuration (general & devices) to the Log file
write_ic3_log_recd(f"Profile:\n{tabs}{Gb.conf_profile}")
write_ic3_log_recd(f"Tracking:\n{tabs}{conf_tracking_recd}")
log_info_msg(f"Profile:\n{tabs}\t\t{Gb.conf_profile}")
log_info_msg(f"Tracking:\n{tabs}\t\t{conf_tracking_recd}")

write_ic3_log_recd(f"General Configuration:\n{tabs}{Gb.conf_general}")
write_ic3_log_recd(f"{tabs}{Gb.ha_location_info}")
write_ic3_log_recd("")
log_info_msg(f"General Configuration:\n{tabs}\t\t{Gb.conf_general}")
log_info_msg(f"{tabs}\t\t{Gb.ha_location_info}")
log_info_msg("")

for conf_device in Gb.conf_devices:
write_ic3_log_recd( f"{conf_device[CONF_FNAME]}, {conf_device[CONF_IC3_DEVICENAME]}:\n"
f"{tabs}{conf_device}")
write_ic3_log_recd("")
log_info_msg( f"{conf_device[CONF_FNAME]}, {conf_device[CONF_IC3_DEVICENAME]}:\n"
f"{tabs}\t\t{conf_device}")
log_info_msg("")

#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#
Expand All @@ -391,7 +407,7 @@ def log_info_msg(module_name, log_msg='+'):
log_msg = _resolve_module_name_log_msg(module_name, log_msg)

log_msg = format_msg_line(log_msg)
write_ic3_log_recd(log_msg)
write_ic3log_recd(log_msg)

log_msg = log_msg.replace(' > +', ' > ...\n\t\t\t\t\t\t\t\t\t\t\t+')
Gb.HALogger.debug(log_msg)
Expand All @@ -404,7 +420,7 @@ def log_warning_msg(module_name, log_msg='+'):
Gb.HALogger.warning(log_msg)

log_msg = format_msg_line(log_msg)
write_ic3_log_recd(log_msg)
write_ic3log_recd(log_msg)

#--------------------------------------------------------------------
def log_error_msg(module_name, log_msg='+'):
Expand All @@ -414,12 +430,12 @@ def log_error_msg(module_name, log_msg='+'):
Gb.HALogger.error(log_msg)

log_msg = format_msg_line(log_msg)
write_ic3_log_recd(log_msg)
write_ic3log_recd(log_msg)

#--------------------------------------------------------------------
def log_exception(err):

write_ic3_log_recd(traceback.format_exc())
write_ic3log_recd(traceback.format_exc())
Gb.HALogger.exception(err)

#--------------------------------------------------------------------
Expand All @@ -434,7 +450,7 @@ def log_debug_msg(devicename_or_Device, log_msg='+', msg_prefix=None):
log_msg = format_msg_line(log_msg)

if Gb.log_debug_flag:
write_ic3_log_recd(log_msg)
write_ic3log_recd(log_msg)


log_msg = log_msg.replace(' > +', ' > ...\n\t\t\t\t\t\t\t\t\t\t\t+')
Expand Down Expand Up @@ -854,8 +870,9 @@ def _trace(devicename_or_Device, items='+'):
called_from = _called_from(trace=True)

#rc9 Reworked post_event and write_config_file to call modules directly
Gb.EvLog.post_event(devicename, f"^3^{called_from} {items}")
Gb.iC3Logger.info(f"{called_from} ❗❗❗> {items}")
if Gb.EvLog:
Gb.EvLog.post_event(devicename, f"^3^{called_from} {items}")
write_ic3log_recd(f"{called_from} ⛔━⛔ {items}")

#--------------------------------------------------------------------
def _traceha(items, v1='+++', v2='', v3='', v4='', v5=''):
Expand All @@ -869,9 +886,9 @@ def _traceha(items, v1='+++', v2='', v3='', v4='', v5=''):
else:
trace_msg = (f"|{v1}|-|{v2}|-|{v3}|-|{v4}|-|{v5}|")

trace_msg = (f"{called_from} ❗❗❗> {items} {trace_msg}")
trace_msg = (f"{called_from} ⛔━⛔ {items} {trace_msg}")

Gb.iC3Logger.info(trace_msg)
write_ic3log_recd(trace_msg)


except Exception as err:
Expand Down
4 changes: 2 additions & 2 deletions custom_components/icloud3/icloud3_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
post_event, post_error_msg, post_monitor_msg, post_internal_error,
post_alert, clear_alert,
log_info_msg, log_exception, log_start_finish_update_banner,
log_debug_msg, archive_log_file,
log_debug_msg, archive_ic3log_file,
_trace, _traceha, )
from .helpers.time_util import (time_now_secs, secs_to_time, secs_to, secs_since, time_now,
secs_to_time, format_timer, format_age, secs_to_hhmm,
Expand Down Expand Up @@ -1227,7 +1227,7 @@ def _timer_tasks_midnight(self):
Gb.WazeHist.wazehist_recalculate_time_dist_all_zones()

# Close log file, rename to '-1', open a new log file
archive_log_file()
archive_ic3log_file()
post_event(f"{EVLOG_IC3_STAGE_HDR}End of Day File Maintenance Started")

#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Expand Down
Loading

0 comments on commit 040c626

Please sign in to comment.