diff --git a/myFox2Mqtt/business/__init__.py b/myFox2Mqtt/business/__init__.py index e60eb90..b8dedb5 100644 --- a/myFox2Mqtt/business/__init__.py +++ b/myFox2Mqtt/business/__init__.py @@ -21,6 +21,7 @@ from mqtt import MQTTClient LOGGER = logging.getLogger(__name__) +HISTORY = {} def ha_sites_config( @@ -384,22 +385,28 @@ def update_sites_status( payload = {} events = api.get_site_history(site_id=site_id) for event in events: - # Only Push last Event if event: - # 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() - # if now - created_at_date < timedelta(seconds=70): - payload = f"{event.get('type')} {event.get('createdAt')} {event.get('label')}" - # Push status to MQTT - mqtt_publish( - mqtt_client=mqtt_client, - topic=f"{mqtt_config.get('topic_prefix', 'myFox2mqtt')}/{site_id}/history", - payload=payload, - retain=True, - ) - break + 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() + if now - created_at_date < timedelta(seconds=90): + if created_at in HISTORY: + LOGGER.info(f"History still published: {HISTORY[created_at]}") + continue + HISTORY[created_at] = {event.get("type"): event.get("label")} + payload = f"{event.get('type')} {event.get('createdAt')} {event.get('label')}" + # Push status to MQTT + mqtt_publish( + mqtt_client=mqtt_client, + topic=f"{mqtt_config.get('topic_prefix', 'myFox2mqtt')}/{site_id}/history", + payload=payload, + retain=True, + ) + else: + LOGGER.info( + f"Event is too old {event.get('type')} {event.get('createdAt')} {event.get('label')}" + ) except Exception as exp: LOGGER.warning(f"Error while getting site history: {exp}") diff --git a/myFox2Mqtt/business/mqtt.py b/myFox2Mqtt/business/mqtt.py index d55f70c..7631057 100644 --- a/myFox2Mqtt/business/mqtt.py +++ b/myFox2Mqtt/business/mqtt.py @@ -93,6 +93,13 @@ def consume_mqtt_message(msg, mqtt_config: dict, api: MyFoxApi, mqtt_client: cli LOGGER.info(f"{text_payload} Shutter on {site_id} / {device_id}") api.shutter_action_device(site_id=site_id, device_id=device_id, action=text_payload) + # Manage Gate + elif text_payload in ["one", "two"]: + site_id = msg.topic.split("/")[1] + device_id = msg.topic.split("/")[2] + LOGGER.info(f"{text_payload} Gate on {site_id} / {device_id}") + api.gate_action_device(site_id=site_id, device_id=device_id, action=text_payload) + # Manage Socket elif text_payload in ["on_socket", "off_socket"]: site_id = msg.topic.split("/")[1] diff --git a/myFox2Mqtt/docker-compose.yml b/myFox2Mqtt/docker-compose.yml index e6ec08c..142b85a 100644 --- a/myFox2Mqtt/docker-compose.yml +++ b/myFox2Mqtt/docker-compose.yml @@ -2,6 +2,6 @@ version: "3" services: myfox2mqtt: build: . - image: myfox2mqtt:v2023.10.2 + image: myfox2mqtt:v2023.11.0 volumes: - ./config:/config diff --git a/myFox2Mqtt/main.py b/myFox2Mqtt/main.py index 8a3bb88..290b46c 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.10.2" +VERSION = "2023.11.0" def myfox_loop(config, mqtt_client, api):