Skip to content

Commit

Permalink
Resend discovery message on HA online #907 #920
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlt8 committed Jul 17, 2023
1 parent 4f311a0 commit ff60f03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
39 changes: 24 additions & 15 deletions app/wyzebridge/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def wrapper(*args, **kwargs):


@mqtt_enabled
def wyze_discovery(cam: WyzeCamera, cam_uri: str) -> None:
"""Add Wyze camera to MQTT if enabled."""
base = f"{MQTT_TOPIC}/{cam_uri or cam.name_uri}/"
msgs = [(f"{base}state", "stopped")]
def publish_discovery(cam_uri: str, cam: WyzeCamera, stopped: bool = True) -> None:
"""Publish MQTT discovery message for camera."""
base = f"{MQTT_TOPIC}/{cam_uri}/"
msgs = [(f"{base}state", "stopped")] if stopped else []
if MQTT_DISCOVERY:
base_payload = {
"device": {
Expand Down Expand Up @@ -82,15 +82,8 @@ def mqtt_sub_topic(m_topics: list, callback) -> Optional[paho.mqtt.client.Client
)
client.will_set(f"{MQTT_TOPIC}/state", payload="offline", qos=1, retain=True)
client.connect(MQTT_HOST, int(MQTT_PORT or 1883), 30)
if MQTT_DISCOVERY:
client.subscribe(f"{MQTT_DISCOVERY}/status")
client.message_callback_add(
f"{MQTT_DISCOVERY}/status",
lambda mq_client, _, msg: bridge_status(mq_client, [])
if msg.payload.decode().lower() == "online"
else None,
)
client.loop_start()

return client


Expand Down Expand Up @@ -147,17 +140,33 @@ def update_preview(cam_name: str):


@mqtt_enabled
def cam_control(cam_names: dict, callback):
def cam_control(cams: dict, callback):
topics = []
for uri in cam_names:
for uri in cams:
topics += [f"{uri.lower()}/{t}/set" for t in SET_CMDS]
topics += [f"{uri.lower()}/{t}/get" for t in GET_CMDS | PARAMS]

if client := mqtt_sub_topic(topics, callback):
if MQTT_DISCOVERY:
uri_cams = {uri: cam.camera for uri, cam in cams.items()}
client.subscribe(f"{MQTT_DISCOVERY}/status")
client.message_callback_add(
f"{MQTT_DISCOVERY}/status",
lambda cc, _, msg: _mqtt_discovery(cc, uri_cams, msg),
)
client.on_message = _on_message

return client


def _mqtt_discovery(client, cams, msg):
if msg.payload.decode().lower() != "online" or not cams:
return

bridge_status(client, [])
for uri, cam in cams.items():
publish_discovery(uri, cam, False)


def _on_message(client, callback, msg):
msg_topic = msg.topic.split("/")
if len(msg_topic) < 3:
Expand Down
6 changes: 3 additions & 3 deletions app/wyzebridge/wyze_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from wyzebridge.config import BRIDGE_IP, COOLDOWN, MQTT_TOPIC
from wyzebridge.ffmpeg import get_ffmpeg_cmd
from wyzebridge.logging import logger
from wyzebridge.mqtt import send_mqtt, update_mqtt_state, wyze_discovery
from wyzebridge.mqtt import publish_discovery, send_mqtt, update_mqtt_state
from wyzebridge.webhooks import ifttt_webhook
from wyzebridge.wyze_api import WyzeApi
from wyzebridge.wyze_commands import GET_CMDS, PARAMS, SET_CMDS
Expand Down Expand Up @@ -102,7 +102,7 @@ def setup(self):
logger.error(f"{self.camera.nickname} may not support multiple streams!!")
# self.state = StreamStatus.DISABLED
self.options.update_quality(self.camera.is_2k)
wyze_discovery(self.camera, self.uri)
publish_discovery(self.uri, self.camera)

@property
def state(self):
Expand Down Expand Up @@ -236,7 +236,7 @@ def get_info(self, item: Optional[str] = None) -> dict:
self.update_cam_info()
if self.camera.camera_info and "boa_info" in self.camera.camera_info:
data["boa_url"] = f"http://{self.camera.ip}/cgi-bin/hello.cgi?name=/"
return data | self.camera.dict(exclude={"p2p_id", "enr", "parent_enr"})
return data | self.camera.model_dump(exclude={"p2p_id", "enr", "parent_enr"})

def update_cam_info(self) -> None:
if not self.connected:
Expand Down

0 comments on commit ff60f03

Please sign in to comment.