Skip to content

Commit

Permalink
Merge pull request #19 from Minims/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Minims authored Nov 4, 2023
2 parents 71c684b + a0741b4 commit 79b9938
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
37 changes: 22 additions & 15 deletions myFox2Mqtt/business/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mqtt import MQTTClient

LOGGER = logging.getLogger(__name__)
HISTORY = {}


def ha_sites_config(
Expand Down Expand Up @@ -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}")
Expand Down
7 changes: 7 additions & 0 deletions myFox2Mqtt/business/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion myFox2Mqtt/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ version: "3"
services:
myfox2mqtt:
build: .
image: myfox2mqtt:v2023.10.2
image: myfox2mqtt:v2023.11.0
volumes:
- ./config:/config
2 changes: 1 addition & 1 deletion myFox2Mqtt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 79b9938

Please sign in to comment.