From e02067e0ee0e5283604e9c4d6807ed8f5d94b86d Mon Sep 17 00:00:00 2001 From: Minims Date: Mon, 2 Sep 2024 21:40:37 +0200 Subject: [PATCH 1/3] 2024.9.0: disable log file --- myFox2Mqtt/Dockerfile | 2 +- myFox2Mqtt/docker-compose.yml | 2 +- myFox2Mqtt/main.py | 6 ++++-- myFox2Mqtt/utils/__init__.py | 14 +++++++++----- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/myFox2Mqtt/Dockerfile b/myFox2Mqtt/Dockerfile index d196207..f9bbe12 100644 --- a/myFox2Mqtt/Dockerfile +++ b/myFox2Mqtt/Dockerfile @@ -20,4 +20,4 @@ RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser / USER appuser # During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug -ENTRYPOINT ["python", "main.py", "-c", "/config/config.yaml"] +ENTRYPOINT ["python", "main.py", "-c", "/config/config.yaml", "-l", "''"] diff --git a/myFox2Mqtt/docker-compose.yml b/myFox2Mqtt/docker-compose.yml index 142b85a..6c6f968 100644 --- a/myFox2Mqtt/docker-compose.yml +++ b/myFox2Mqtt/docker-compose.yml @@ -2,6 +2,6 @@ version: "3" services: myfox2mqtt: build: . - image: myfox2mqtt:v2023.11.0 + image: myfox2mqtt:v2024.9.0 volumes: - ./config:/config diff --git a/myFox2Mqtt/main.py b/myFox2Mqtt/main.py index 290b46c..bdc472e 100755 --- a/myFox2Mqtt/main.py +++ b/myFox2Mqtt/main.py @@ -12,7 +12,7 @@ from myfox.sso import init_sso from myfox.api import MyFoxApi -VERSION = "2023.11.0" +VERSION = "2024.9.0" def myfox_loop(config, mqtt_client, api): @@ -31,12 +31,14 @@ def myfox_loop(config, mqtt_client, api): PARSER = argparse.ArgumentParser() PARSER.add_argument("--verbose", "-v", action="store_true", help="verbose mode") PARSER.add_argument("--configuration", "-c", type=str, help="config file path") + PARSER.add_argument("--logfile", "-l", type=str, help="logfile", default="myFox2Mqtt.log") ARGS = PARSER.parse_args() DEBUG = ARGS.verbose CONFIG_FILE = ARGS.configuration + LOG_FILE = ARGS.logfile # Setup Logger - setup_logger(debug=DEBUG, filename="myFox2Mqtt.log") + setup_logger(debug=DEBUG, filename=LOG_FILE) LOGGER = logging.getLogger(__name__) LOGGER.info(f"Starting MyFox2Mqtt {VERSION}") diff --git a/myFox2Mqtt/utils/__init__.py b/myFox2Mqtt/utils/__init__.py index 11c40e8..eae68ef 100644 --- a/myFox2Mqtt/utils/__init__.py +++ b/myFox2Mqtt/utils/__init__.py @@ -12,23 +12,27 @@ LOGGER = logging.getLogger(__name__) -def setup_logger(debug: bool = False, filename: str = "/var/log/myFox.log") -> None: +def setup_logger(filename: str, debug: bool = False) -> None: """Setup Logging Args: debug (bool, optional): True if debug enabled. Defaults to False. filename (str, optional): log filename. Defaults to "/var/log/myFox.log". """ log_level = logging.DEBUG if debug else logging.INFO + handlers = [ + logging.StreamHandler(), + ] + if filename: + handlers.append(logging.FileHandler(filename=filename)) + logging.basicConfig( level=log_level, format="%(asctime)s [%(levelname)s] [%(name)s:%(lineno)d] %(message)s", - handlers=[ - logging.StreamHandler(), - logging.FileHandler(filename=filename), - ], + handlers=handlers, ) + def read_config_file(config_file: str) -> Dict[str, Any]: """Read config file From da2272c29661a840cd8656992d2991d07c50646b Mon Sep 17 00:00:00 2001 From: Minims Date: Sun, 8 Sep 2024 10:14:38 +0200 Subject: [PATCH 2/3] 2024.9.1: convert date event to local time --- myFox2Mqtt/business/__init__.py | 15 ++++++++++++++- myFox2Mqtt/docker-compose.yml | 2 +- myFox2Mqtt/main.py | 2 +- myFox2Mqtt/requirements.txt | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/myFox2Mqtt/business/__init__.py b/myFox2Mqtt/business/__init__.py index b8dedb5..9361886 100644 --- a/myFox2Mqtt/business/__init__.py +++ b/myFox2Mqtt/business/__init__.py @@ -1,6 +1,8 @@ """Business Functions""" + import logging from datetime import datetime, timedelta +import pytz from time import sleep from exceptions import MyFoxInitError @@ -372,6 +374,15 @@ def ha_devices_config( ) +def convert_utc_to_paris(date: datetime) -> datetime: + + utc_zone = pytz.utc + date = utc_zone.localize(date) + paris_zone = pytz.timezone("Europe/Paris") + paris_date = date.astimezone(paris_zone) + return paris_date + + def update_sites_status( api: MyFoxApi, mqtt_client: MQTTClient, @@ -389,7 +400,9 @@ def update_sites_status( created_at = event.get("createdAt") date_format = "%Y-%m-%dT%H:%M:%SZ" created_at_date = datetime.strptime(created_at, date_format) - now = datetime.now() + created_at_date = convert_utc_to_paris(date=created_at_date) + paris_tz = pytz.timezone("Europe/Paris") + now = datetime.now(paris_tz) if now - created_at_date < timedelta(seconds=90): if created_at in HISTORY: LOGGER.info(f"History still published: {HISTORY[created_at]}") diff --git a/myFox2Mqtt/docker-compose.yml b/myFox2Mqtt/docker-compose.yml index 6c6f968..b914509 100644 --- a/myFox2Mqtt/docker-compose.yml +++ b/myFox2Mqtt/docker-compose.yml @@ -2,6 +2,6 @@ version: "3" services: myfox2mqtt: build: . - image: myfox2mqtt:v2024.9.0 + image: myfox2mqtt:v2024.9.1 volumes: - ./config:/config diff --git a/myFox2Mqtt/main.py b/myFox2Mqtt/main.py index bdc472e..0221fb7 100755 --- a/myFox2Mqtt/main.py +++ b/myFox2Mqtt/main.py @@ -12,7 +12,7 @@ from myfox.sso import init_sso from myfox.api import MyFoxApi -VERSION = "2024.9.0" +VERSION = "2024.9.1" def myfox_loop(config, mqtt_client, api): diff --git a/myFox2Mqtt/requirements.txt b/myFox2Mqtt/requirements.txt index 34ded4e..2b98c64 100644 --- a/myFox2Mqtt/requirements.txt +++ b/myFox2Mqtt/requirements.txt @@ -4,3 +4,4 @@ paho-mqtt==1.6.1 pyyaml==6.0.1 requests-oauthlib==1.3.1 schedule==1.2.0 +pytz==2024.1 \ No newline at end of file From da9a0da51a18af86f09e744d40d2c5cfbb93ec15 Mon Sep 17 00:00:00 2001 From: Minims Date: Sun, 8 Sep 2024 14:44:07 +0200 Subject: [PATCH 3/3] 2024.9.1: set delay_site to 60s minimum --- myFox2Mqtt/config/config.yaml.example | 2 +- myFox2Mqtt/myfox_2_mqtt.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/myFox2Mqtt/config/config.yaml.example b/myFox2Mqtt/config/config.yaml.example index 3c815cb..e34e6b6 100644 --- a/myFox2Mqtt/config/config.yaml.example +++ b/myFox2Mqtt/config/config.yaml.example @@ -29,6 +29,6 @@ mqtt: ha_discover_prefix: "homeassistant" # MyFox2MQTT -delay_site: 10 # seconds +delay_site: 60 # seconds delay_device: 60 # seconds manual_snapshot: false diff --git a/myFox2Mqtt/myfox_2_mqtt.py b/myFox2Mqtt/myfox_2_mqtt.py index 8926d8b..59d8538 100644 --- a/myFox2Mqtt/myfox_2_mqtt.py +++ b/myFox2Mqtt/myfox_2_mqtt.py @@ -1,4 +1,5 @@ """MyFox 2 Mqtt""" + import logging from time import sleep @@ -41,10 +42,10 @@ def __init__(self, api: MyFoxApi, mqtt_client: MQTTClient, config: dict) -> None self.my_sites_id = [] self.delay_site = config.get("delay_site", 60) - self.delay_site = max(self.delay_site, 10) + self.delay_site = max(self.delay_site, 60) self.delay_device = config.get("delay_device", 60) - self.delay_device = max(self.delay_device, 10) + self.delay_device = max(self.delay_device, 60) self.manual_snapshot = config.get("manual_snapshot", False)