Skip to content

Instantly share code, notes, and snippets.

@storm1er
Forked from HeedfulCrayon/README.md
Last active March 16, 2024 20:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save storm1er/09b8bee73cc6e31d1efd1f271e56d9bb to your computer and use it in GitHub Desktop.
Save storm1er/09b8bee73cc6e31d1efd1f271e56d9bb to your computer and use it in GitHub Desktop.
Script for OBS Studio to publish session stats to MQTT broker with autodiscovery set up for Home Assistant

OBS MQTT Status to Home Assistant

Setup:

  1. Download script
  2. Install mqtt-wrapper pip install mqtt-wrapper
  3. Open OBS Studio
  4. In OBS Studio add a script (Tools -> Scripts)
  5. Configure script parameters (my base channel is homeassistant and my sensor name is obs, creating the path homeassistant/sensor/obs)

script_parameters

  1. Click the script refresh button

  2. If mqtt autodiscovery is on in home assistant, you should see a sensor called sensor.[MQTT Sensor Name] with attributes containing the stats.

    If you have allowed the MQTT to control OBS, you will see two switches called switch.[MQTT Sensor Name]_record and switch.[MQTT Sensor Name]_stream

    You will also see a switch for each profile named switch.[MQTT Sensor Name]_[OBS Profile Name]_Profile (NOTE: Profiles in OBS will have to contain no spaces)

    If mqtt autodiscovery is not turned on, you will need to add this as your sensor config

    - platform: "mqtt"
      name: "OBS"
      state_topic: "[Your set topic here]/state"
      icon: "mdi:video-wireless"
      force_update: true
      qos: 1
      json_attributes_topic: "[Your set topic here]/attributes"
    

    and if you would like to control via MQTT without autodiscovery you will need to add two mqtt switches for recording and streaming

    - platform: "mqtt"
      name: OBS Record Switch
      state_topic: "[MQTT Base Channel]/switch/[MQTT Sensor Name]/record/state"
      command_topic: "[MQTT Base Channel]/switch/[MQTT Sensor Name]/record/set"
      available_topic: "[MQTT Base Channel]/switch/[MQTT Sensor Name]/record/available"
      payload_on: "ON"
      payload_off: "OFF"
      icon: mdi:record
    - platform: "mqtt"
      name: OBS Stream Switch
      state_topic: "[MQTT Base Channel]/switch/[MQTT Sensor Name]/stream/state"
      command_topic: "[MQTT Base Channel]/switch/[MQTT Sensor Name]/stream/set"
      available_topic: "[MQTT Base Channel]/switch/[MQTT Sensor Name]/stream/available"
      payload_on: "ON"
      payload_off: "OFF"
      icon: mdi:broadcast
    

    If you would like to control profiles without discovery turned on you will need to add switches FOR EACH profile you wish to turn on from Home Assistant

    - platform: "mqtt"
      name: OBS Test Profile
      state_topic: "[MQTT Base Channel]/switch/[OBS Profile Name]/state"
      command_topic: "[MQTT Base Channel]/switch/[OBS Profile Name]/profile/set"
      payload_on: "ON"
      payload_off: "OFF"
      icon: mdi:alpha-p-box
    

    The same goes for scene

Sensor States

  • Recording
  • Streaming
  • Streaming and Recording
  • Stopped (OBS Open)
  • Off (OBS Closed)

NOTE: If you have autodiscovery on when you update the script, make sure to remove the Stream, Record and OBS Sensor configs by doing an empty publish to their respective configs

[Your base channel]/sensor/[Sensor Name]/config for sensor

[Your base channel]/switch/[Sensor Name]_stream/config for stream switch

[Your base channel]/sensor/[Sensor Name]_record/config for record switch

⚠️ For scene to work

There's an OBS issue about crashes when trying to change scene through python, see:

Solution: first comment below

import json
import socket # Just so we can properly handle hostname exceptions
import obspython as obs
import paho.mqtt.client as mqtt
import ssl
import pathlib
import time
import enum
import uuid
# Meta
__version__ = '1.0.3'
__version_info__ = (1, 0, 3)
__license__ = "AGPLv3"
__license_info__ = {
"AGPLv3": {
"product": "update_mqtt_status_homeassistant",
"users": 0, # 0 being unlimited
"customer": "Unsupported",
"version": __version__,
"license_format": "1.0",
}
}
__author__ = 'HeedfulCrayon'
__doc__ = """\
Publishes real-time OBS status info to the given MQTT server/port/channel \
at the configured interval. Also opens up controllable aspects of OBS to \
be controlled through MQTT.
"""
# Default values for the configurable options:
INTERVAL = 5 # Update interval (in seconds)
MQTT_HOST = "localhost" # Hostname of your MQTT server
MQTT_USER = ""
MQTT_PW = ""
MQTT_PORT = 1883 # Default MQTT port is 1883
MQTT_BASE_CHANNEL = ""
MQTT_SENSOR_NAME = "obs"
PROFILES = []
SCENES = []
SCENE = None
STREAM_SWITCH = None
RECORD_SWITCH = None
SENSOR = None
CONTROL = False
DEBUG = False
LOCK = False
MAC = ':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
for ele in range(0,8*6,8)][::-1])
SCENES_TO_RELEASE = []
class SwitchType(str, enum.Enum):
profile = "profile"
scene = "scene"
record = "record"
stream = "stream"
class SwitchPayload(str, enum.Enum):
OFF = "OFF"
ON = "ON"
class Switch:
"""
Represents a controllable aspect of OBS (Profile, Record, Stream, etc.)
"""
def __init__(self):
self.publish_config()
self.subscribe()
def publish_config(self):
CLIENT.publish(self.config_topic, json.dumps(self.config))
if DEBUG: print(f"Published config {self.config['name']}")
def subscribe(self):
CLIENT.subscribe(self.command_topic)
if DEBUG: print(f"Subscribed to {self.config['name']}")
def publish_state(self, payload):
CLIENT.publish(self.state_topic, payload)
if DEBUG: print(f"{self.config['name']} state changed to {payload}")
def publish_command(self, payload):
CLIENT.publish(self.command_topic, payload)
if DEBUG: print(f"{self.config['name']} command published. Payload: {payload}")
class PersistentSwitch(Switch):
"""
Switch that is persisted (retained) in MQTT
"""
def __init__(self):
super().__init__()
self.publish_availability(SwitchPayload.ON)
def publish_config(self):
CLIENT.publish(self.config_topic, json.dumps(self.config), retain=True)
if DEBUG: print(f"Published config {self.config['name']}")
def publish_availability(self, payload):
CLIENT.publish(self.available_topic, payload)
# if DEBUG: print(f"{self.config['name']} availability set to {payload}")
class ProfileSwitch(Switch):
def __init__(self, profile_name, mqtt_base_channel, mqtt_sensor_name):
self.profile_name = profile_name
self.mqtt_base_channel = mqtt_base_channel
self.mqtt_sensor_name = mqtt_sensor_name
self.switch_type = SwitchType.profile
self.state_topic = f"{self.mqtt_base_channel}/switch/{self.profile_name}/state"
self.command_topic = f"{self.mqtt_base_channel}/switch/{self.profile_name}/profile/set"
self.config_topic = f"{self.mqtt_base_channel}/switch/{self.profile_name}/config"
self.config = {
"name": f"{self.profile_name} Profile",
"unique_id": f"{self.mqtt_sensor_name}_{self.profile_name}_profile",
"device": {
"name": f"{self.mqtt_sensor_name}",
"identifiers": f"[['mac',{MAC}]]",
"manufacturer": f"OBS Script v.{__version__}",
"sw_version": __version__
},
"state_topic": self.state_topic,
"command_topic": self.command_topic,
"icon": f"mdi:alpha-{self.profile_name[0].lower()}-box",
"payload_on": SwitchPayload.ON,
"payload_off": SwitchPayload.OFF
}
super().__init__()
def publish_remove_config(self):
CLIENT.publish(self.config_topic, "")
if DEBUG: print(f"Removed config {self.config['name']}")
class SceneSwitch(Switch):
def __init__(self, scene_name, mqtt_base_channel, mqtt_sensor_name):
self.scene_name = scene_name
self.mqtt_base_channel = mqtt_base_channel
self.mqtt_sensor_name = mqtt_sensor_name
self.switch_type = SwitchType.scene
self.state_topic = f"{self.mqtt_base_channel}/switch/{self.scene_name}/state"
self.command_topic = f"{self.mqtt_base_channel}/switch/{self.scene_name}/scene/set"
self.config_topic = f"{self.mqtt_base_channel}/switch/{self.scene_name}/config"
self.config = {
"name": f"{self.scene_name} Scene",
"unique_id": f"{self.mqtt_sensor_name}_{self.scene_name}_scene",
"device": {
"name": f"{self.mqtt_sensor_name}",
"identifiers": f"[['mac',{MAC}]]",
"manufacturer": f"OBS Script v.{__version__}",
"sw_version": __version__
},
"state_topic": self.state_topic,
"command_topic": self.command_topic,
"icon": f"mdi:alpha-{self.scene_name[0].lower()}-box",
"payload_on": SwitchPayload.ON,
"payload_off": SwitchPayload.OFF
}
super().__init__()
def publish_remove_config(self):
CLIENT.publish(self.config_topic, "")
if DEBUG: print(f"Removed config {self.config['name']}")
class StreamSwitch(PersistentSwitch):
def __init__(self, mqtt_base_channel, mqtt_sensor_name):
self.mqtt_base_channel = mqtt_base_channel
self.mqtt_sensor_name = mqtt_sensor_name
self.switch_type = SwitchType.stream
self.state_topic = f"{self.mqtt_base_channel}/switch/{self.mqtt_sensor_name}/stream/state"
self.command_topic = f"{self.mqtt_base_channel}/switch/{self.mqtt_sensor_name}/stream/set"
self.config_topic = f"{self.mqtt_base_channel}/switch/{self.mqtt_sensor_name}_stream/config"
self.available_topic = f"{self.mqtt_base_channel}/switch/{self.mqtt_sensor_name}/stream/available"
self.config = {
"name": f"Stream",
"unique_id": f"{self.mqtt_sensor_name}_stream",
"device": {
"name": f"{self.mqtt_sensor_name}",
"identifiers": f"[['mac',{MAC}]]",
"manufacturer": f"OBS Script v.{__version__}",
"sw_version": __version__
},
"state_topic": self.state_topic,
"command_topic": self.command_topic,
"payload_on": SwitchPayload.ON,
"payload_off": SwitchPayload.OFF,
"availability": {
"payload_available": SwitchPayload.ON,
"payload_not_available": SwitchPayload.OFF,
"topic": self.available_topic
},
"icon": "mdi:broadcast"
}
super().__init__()
class RecordSwitch(PersistentSwitch):
def __init__(self, mqtt_base_channel, mqtt_sensor_name):
self.mqtt_base_channel = mqtt_base_channel
self.mqtt_sensor_name = mqtt_sensor_name
self.switch_type = SwitchType.record
self.state_topic = f"{self.mqtt_base_channel}/switch/{self.mqtt_sensor_name}/record/state"
self.command_topic = f"{self.mqtt_base_channel}/switch/{self.mqtt_sensor_name}/record/set"
self.config_topic = f"{self.mqtt_base_channel}/switch/{self.mqtt_sensor_name}_record/config"
self.available_topic = f"{self.mqtt_base_channel}/switch/{self.mqtt_sensor_name}/record/available"
self.config = {
"name": f"Record",
"unique_id": f"{self.mqtt_sensor_name}_record",
"device": {
"name": f"{self.mqtt_sensor_name}",
"identifiers": f"[['mac',{MAC}]]",
"manufacturer": f"OBS Script v.{__version__}",
"sw_version": __version__
},
"state_topic": self.state_topic,
"command_topic": self.command_topic,
"payload_on": SwitchPayload.ON,
"payload_off": SwitchPayload.OFF,
"availability": {
"payload_available": SwitchPayload.ON,
"payload_not_available": SwitchPayload.OFF,
"topic": self.available_topic
},
"icon": "mdi:record"
}
super().__init__()
class SensorState(str, enum.Enum):
Off = "Off"
Stopped = "Stopped"
Recording = "Recording"
Streaming = "Streaming"
Recording_and_Streaming = "Recording and Streaming"
class Sensor:
def __init__(self, mqtt_base_channel, mqtt_sensor_name):
self.mqtt_base_channel = mqtt_base_channel
self.mqtt_sensor_name = mqtt_sensor_name
self.state_topic = f"{self.mqtt_base_channel}/sensor/{self.mqtt_sensor_name}/state"
self.config_topic = f"{self.mqtt_base_channel}/sensor/{self.mqtt_sensor_name}/config"
self.attributes_topic = f"{self.mqtt_base_channel}/sensor/{self.mqtt_sensor_name}/attributes"
self.config = {
"name": self.mqtt_sensor_name,
"unique_id": self.mqtt_sensor_name,
"device": {
"name": f"{self.mqtt_sensor_name}",
"identifiers": f"[['mac',{MAC}]]",
"manufacturer": f"OBS Script v.{__version__}",
"sw_version": __version__
},
"state_topic": self.state_topic,
"json_attributes_topic": self.attributes_topic
}
self.state = self.get_state
self.previous_state = SensorState.Off
self.recording = obs.obs_frontend_recording_active
self.streaming = obs.obs_frontend_streaming_active
self.paused = obs.obs_frontend_recording_paused
self.replay_buffer =obs.obs_frontend_replay_buffer_active
self.fps = obs.obs_get_active_fps
self.frame_time_ns = obs.obs_get_average_frame_time_ns
self.frames = obs.obs_get_total_frames
self.lagged_frames = obs.obs_get_lagged_frames
self.active = False
self.publish_config()
self.publish_state()
self.publish_attributes()
def publish_config(self):
CLIENT.publish(self.config_topic, json.dumps(self.config))
if DEBUG: print(f"Published config {self.config['name']}")
def publish_attributes(self):
stats = {
"recording": self.recording(),
"streaming": self.streaming(),
"paused": self.paused(),
"fps": self.fps(),
"frame_time_ns": self.frame_time_ns(),
"frames": self.frames(),
"lagged_frames": self.lagged_frames()
}
CLIENT.publish(self.attributes_topic, json.dumps(stats))
self.publish_state()
if DEBUG:
print(f"{self.config['name']} attributes updated")
print(json.dumps(stats))
def get_state(self):
recording = self.recording()
streaming = self.streaming()
if recording and streaming:
self.active = True
self.previous_state = SensorState.Recording_and_Streaming
return SensorState.Recording_and_Streaming
elif streaming:
self.active = True
self.previous_state = SensorState.Streaming
return SensorState.Streaming
elif recording:
self.active = True
self.previous_state = SensorState.Recording
return SensorState.Recording
else:
self.active = False
self.previous_state = SensorState.Stopped
return SensorState.Stopped
def publish_state(self):
state = self.state()
CLIENT.publish(self.state_topic, state)
if DEBUG: print(f"{self.config['name']} state changed to {state}")
def publish_off_state(self):
self.previous_state = SensorState.Off
CLIENT.publish(self.state_topic, SensorState.Off)
if DEBUG: print(f"{self.config['name']} state changed to {SensorState.Off}")
# MQTT Event Functions
def on_mqtt_connect(client, userdata, flags, rc):
"""
Called when the MQTT client is connected from the server. Just prints a
message indicating we connected successfully.
"""
if DEBUG: print("MQTT connection successful")
set_homeassistant_config()
def on_mqtt_disconnect(client, userdata, rc):
"""
Called when the MQTT client gets disconnected. Just logs a message about it
(we'll auto-reconnect inside of update_status()).
"""
if DEBUG: print("MQTT disconnected. Reason: {}".format(str(rc)))
def on_mqtt_message(client, userdata, message):
"""
Handles MQTT messages that have been subscribed to
"""
payload = str(message.payload.decode("utf-8"))
if DEBUG: print(f"{message.topic}: {payload}")
entity = message_to_switch_entity(message)
if entity != None:
execute_action(entity, payload)
else:
print(f"Unable to get entity {message.topic}")
# OBS Script Function Exports
def script_description():
return __doc__ # We wrote a nice docstring... Might as well use it!
def script_load(settings):
"""
Just prints a message indicating that the script was loaded successfully.
"""
global STATE
if DEBUG: print("MQTT script loaded.")
STATE = "Initializing"
def script_unload():
"""
Publishes a final status message indicating OBS is off
(so your MQTT sensor doesn't get stuck thinking you're
recording/streaming forever) and calls `CLIENT.disconnect()`.
"""
global STATE
global SCENES_TO_RELEASE
if DEBUG: print("Script unloading")
STATE = "Off"
for scene in SCENES_TO_RELEASE:
try:
obs.obs_scene_release(scene)
except:
try:
obs.obs_source_release(scene)
except:
if DEBUG: print("Unable to release some SCENES_TO_RELEASE")
SCENES_TO_RELEASE = []
if CLIENT.is_connected():
SENSOR.publish_off_state()
set_persistent_switch_availability()
remove_profiles_from_homeassistant()
remove_scenes_from_homeassistant()
CLIENT.disconnect()
CLIENT.loop_stop()
def script_defaults(settings):
"""
Sets up our default settings in the OBS Scripts interface.
"""
obs.obs_data_set_default_string(settings, "mqtt_host", MQTT_HOST)
obs.obs_data_set_default_string(settings, "mqtt_user", MQTT_USER)
obs.obs_data_set_default_string(settings, "mqtt_pw", MQTT_PW)
obs.obs_data_set_default_string(settings, "mqtt_base_channel", MQTT_BASE_CHANNEL)
obs.obs_data_set_default_string(settings, "mqtt_sensor_name", MQTT_SENSOR_NAME)
obs.obs_data_set_default_int(settings, "mqtt_port", MQTT_PORT)
obs.obs_data_set_default_int(settings, "interval", INTERVAL)
obs.obs_data_set_default_bool(settings, "controllable", CONTROL)
def script_properties():
"""
Makes this script's settings configurable via OBS's Scripts GUI.
"""
props = obs.obs_properties_create()
obs.obs_properties_add_text(props, "mqtt_host", "MQTT server hostname", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_text(props, "mqtt_user", "MQTT username", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_text(props, "mqtt_pw", "MQTT password", obs.OBS_TEXT_PASSWORD)
obs.obs_properties_add_text(props, "mqtt_base_channel", "MQTT Base channel",obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_text(props, "mqtt_sensor_name", "MQTT Sensor Name",obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_int(props, "mqtt_port", "MQTT TCP/IP port", MQTT_PORT, 65535, 1)
obs.obs_properties_add_int(props, "interval", "Update Interval (seconds)", 1, 3600, 1)
obs.obs_properties_add_bool(props, "controllable", "Control Streaming/Recording via MQTT")
obs.obs_properties_add_bool(props, "debug", "Debug")
return props
def script_update(settings):
"""
Applies any changes made to the MQTT settings in the OBS Scripts GUI then
reconnects the MQTT client.
"""
# Apply the new settings
global MQTT_HOST
global MQTT_USER
global MQTT_PW
global MQTT_PORT
global MQTT_BASE_CHANNEL
global MQTT_SENSOR_NAME
global INTERVAL
global CONTROL
global DEBUG
mqtt_host = obs.obs_data_get_string(settings, "mqtt_host")
if mqtt_host != MQTT_HOST:
MQTT_HOST = mqtt_host
mqtt_user = obs.obs_data_get_string(settings, "mqtt_user")
if mqtt_user != MQTT_USER:
MQTT_USER = mqtt_user
mqtt_pw = obs.obs_data_get_string(settings, "mqtt_pw")
if mqtt_pw != MQTT_PW:
MQTT_PW = mqtt_pw
mqtt_base_channel = obs.obs_data_get_string(settings, "mqtt_base_channel")
if mqtt_base_channel != MQTT_BASE_CHANNEL:
MQTT_BASE_CHANNEL = mqtt_base_channel
mqtt_sensor_name = obs.obs_data_get_string(settings, "mqtt_sensor_name")
if mqtt_sensor_name != MQTT_SENSOR_NAME:
MQTT_SENSOR_NAME = mqtt_sensor_name
mqtt_port = obs.obs_data_get_int(settings, "mqtt_port")
if mqtt_port != MQTT_PORT:
MQTT_PORT = mqtt_port
INTERVAL = obs.obs_data_get_int(settings, "interval")
CONTROL = obs.obs_data_get_bool(settings, "controllable")
DEBUG = obs.obs_data_get_bool(settings, "debug")
# Disconnect (if connected) and reconnect the MQTT client
CLIENT.disconnect()
try:
if MQTT_PW != "" and MQTT_USER != "":
CLIENT.username_pw_set(MQTT_USER, password=MQTT_PW)
CLIENT.connect_async(MQTT_HOST, MQTT_PORT, 60)
except (socket.gaierror, ConnectionRefusedError) as e:
print("NOTE: Got a socket issue: %s" % e)
pass # Ignore it for now
obs.obs_frontend_remove_event_callback(frontend_changed)
obs.obs_frontend_add_event_callback(frontend_changed)
# Remove and replace the timer that publishes our status information
obs.timer_remove(update_status)
obs.timer_add(update_status, INTERVAL * 1000)
CLIENT.loop_start()
def change_scene(scene_name):
global SCENES_TO_RELEASE
if DEBUG: print("Trying to change scene")
targetScene = obs.obs_get_source_by_name(scene_name)
SCENES_TO_RELEASE.append(targetScene)
obs.obs_frontend_set_current_scene(targetScene)
if DEBUG: print("Done")
# obs.obs_scene_release(targetScene)
def frontend_changed(event):
"""
Callback for frontend events
"""
switcher = {
obs.OBS_FRONTEND_EVENT_SCENE_CHANGED: scene_changed,
obs.OBS_FRONTEND_EVENT_SCENE_LIST_CHANGED: scene_list_changed,
obs.OBS_FRONTEND_EVENT_PROFILE_CHANGED: profile_changed,
obs.OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED: profile_list_changed,
obs.OBS_FRONTEND_EVENT_RECORDING_STARTED: recording_started,
obs.OBS_FRONTEND_EVENT_RECORDING_STOPPED: recording_stopped,
obs.OBS_FRONTEND_EVENT_STREAMING_STARTED: streaming_started,
obs.OBS_FRONTEND_EVENT_STREAMING_STOPPED: streaming_stopped
}
function = switcher.get(event, None)
if function != None:
if DEBUG: print(f"Event fired: {event}")
function()
else:
if DEBUG: print(f"Unknown event fired: {event}")
def profile_changed():
"""
Callback for OBS_FRONTEND_EVENT_PROFILE_CHANGED
"""
global PROFILE
while LOCK:
time.sleep(0.5)
PROFILE.publish_state(SwitchPayload.OFF)
new_profile = obs.obs_frontend_get_current_profile()
for profile in PROFILES:
if profile.profile_name == new_profile:
profile.publish_state(SwitchPayload.ON)
PROFILE = profile
if DEBUG: print("Profile Changed")
def scene_changed():
"""
Callback for OBS_FRONTEND_EVENT_SCENE_CHANGED
"""
global SCENE
global SCENES_TO_RELEASE
while LOCK:
time.sleep(0.5)
if (SCENE != None):
SCENE.publish_state(SwitchPayload.OFF)
targetSource = obs.obs_frontend_get_current_scene()
SCENES_TO_RELEASE.append(targetSource)
new_scene = obs.obs_source_get_name(targetSource)
# obs.obs_source_release(targetSource)
for scene in SCENES:
if scene.scene_name == new_scene:
scene.publish_state(SwitchPayload.ON)
SCENE = scene
if DEBUG: print("Scene Changed")
def profile_list_changed():
"""
Callback for OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED
"""
global LOCK
LOCK = True
for profile in PROFILES:
profile.publish_remove_config()
time.sleep(0.1)
setup_profiles_in_homeassistant()
LOCK = False
if DEBUG: print("Profile List Changed")
def scene_list_changed():
"""
Callback for OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED
"""
global LOCK
LOCK = True
for scene in SCENES:
scene.publish_remove_config()
time.sleep(0.1)
setup_scenes_in_homeassistant()
LOCK = False
if DEBUG: print("Profile List Changed")
def recording_started():
"""
Publishes state of sensor and record switch
"""
SENSOR.publish_state()
SENSOR.publish_attributes()
if CONTROL:
RECORD_SWITCH.publish_state(SwitchPayload.ON)
def recording_stopped():
"""
Publishes state of sensor and record switch
"""
SENSOR.publish_state()
if CONTROL:
RECORD_SWITCH.publish_state(SwitchPayload.OFF)
def streaming_started():
"""
Publishes state of sensor and stream switch
"""
SENSOR.publish_state()
SENSOR.publish_attributes()
if CONTROL:
STREAM_SWITCH.publish_state(SwitchPayload.ON)
def streaming_stopped():
"""
Publishes state of sensor and stream switch
"""
SENSOR.publish_state()
if CONTROL:
STREAM_SWITCH.publish_state(SwitchPayload.OFF)
# Event Helper Functions
def set_homeassistant_config():
"""
Sends initial configuration state and attributes topic
for autodiscovery in Home Assistant
"""
global SENSOR
SENSOR = Sensor(MQTT_BASE_CHANNEL, MQTT_SENSOR_NAME)
if CONTROL:
setup_homeassistant_control()
def setup_homeassistant_control():
"""
Sets up profile, recording and streaming controls
"""
global STREAM_SWITCH
global RECORD_SWITCH
setup_profiles_in_homeassistant()
setup_scenes_in_homeassistant()
# Set up switches for autodiscovery
STREAM_SWITCH = StreamSwitch(MQTT_BASE_CHANNEL, MQTT_SENSOR_NAME)
RECORD_SWITCH = RecordSwitch(MQTT_BASE_CHANNEL, MQTT_SENSOR_NAME)
def setup_profiles_in_homeassistant():
"""
Publishes config, and subscribes to the command topic for each profile.
Also sets the current profile's state
"""
global PROFILE
global PROFILES
current_profile = obs.obs_frontend_get_current_profile()
profiles = obs.obs_frontend_get_profiles()
PROFILES = []
for profile in profiles:
profile_switch = ProfileSwitch(
profile_name=profile,
mqtt_base_channel=MQTT_BASE_CHANNEL,
mqtt_sensor_name=MQTT_SENSOR_NAME
)
PROFILES.append(profile_switch)
if DEBUG: print(f"Profile {profile_switch.profile_name} added to PROFILES")
if profile_switch.profile_name == current_profile:
PROFILE = profile_switch
time.sleep(0.5)
for profile_switch in PROFILES:
if profile_switch.profile_name == current_profile:
profile_switch.publish_state(SwitchPayload.ON)
else:
profile_switch.publish_state(SwitchPayload.OFF)
def setup_scenes_in_homeassistant():
"""
Publishes config, and subscribes to the command topic for each scene.
Also sets the current scene's state
"""
global SCENE
global SCENES
global SCENES_TO_RELEASE
current_scene_obj = obs.obs_frontend_get_current_scene()
SCENES_TO_RELEASE.append(current_scene_obj)
current_scene = obs.obs_source_get_name(current_scene_obj)
scenes = obs.obs_frontend_get_scene_names()
SCENES = []
for scene in scenes:
scene_switch = SceneSwitch(
scene_name=scene,
mqtt_base_channel=MQTT_BASE_CHANNEL,
mqtt_sensor_name=MQTT_SENSOR_NAME
)
SCENES.append(scene_switch)
if DEBUG: print(f"Scene {scene_switch.scene_name} added to SCENES")
if scene_switch.scene_name == current_scene:
SCENE = scene_switch
time.sleep(0.5)
for scene_switch in SCENES:
if scene_switch.scene_name == current_scene:
scene_switch.publish_state(SwitchPayload.ON)
else:
scene_switch.publish_state(SwitchPayload.OFF)
def set_persistent_switch_availability():
"""
Reports the availability of the persistent switches
"""
RECORD_SWITCH.publish_availability(SwitchPayload.OFF)
STREAM_SWITCH.publish_availability(SwitchPayload.OFF)
def remove_profiles_from_homeassistant():
"""
Profiles are removed when obs is not open
"""
global PROFILES
for profile in PROFILES:
profile.publish_remove_config()
PROFILES = []
def remove_scenes_from_homeassistant():
"""
Profiles are removed when obs is not open
"""
global SCENES
for scene in SCENES:
scene.publish_remove_config()
SCENES = []
def execute_action(switch, payload):
"""
Executes frontend actions (Profile change, recording, streaming)
"""
if switch.switch_type == SwitchType.scene:
if payload == SwitchPayload.ON and SCENE.scene_name != switch.scene_name:
change_scene(switch.scene_name)
else:
if DEBUG: print(f"Already on scene {switch.scene_name}")
if switch.switch_type == SwitchType.profile:
if SENSOR.active: # Not sure if this NEEDS to be here as it won't actually change the profile while streaming/recording
return
if PROFILE.profile_name != switch.profile_name:
obs.obs_frontend_set_current_profile(switch.profile_name)
else:
if DEBUG: print(f"Already on profile {switch.profile_name}")
elif switch.switch_type == SwitchType.stream:
if payload == SwitchPayload.ON:
obs.obs_frontend_streaming_start()
else:
obs.obs_frontend_streaming_stop()
elif switch.switch_type == SwitchType.record:
if payload == SwitchPayload.ON:
obs.obs_frontend_recording_start()
else:
obs.obs_frontend_recording_stop()
# Helper Functions
def update_status():
"""
Updates the STATE and the STATUS global with the stats of the current session.
This info if published (JSON-encoded) to the configured MQTT_HOST/MQTT_PORT/MQTT_BASE_CHANNEL.
Meant to be called at the configured INTERVAL.
"""
global SENSOR
if CONTROL:
STREAM_SWITCH.publish_availability(SwitchPayload.ON)
RECORD_SWITCH.publish_availability(SwitchPayload.ON)
previous_state = SENSOR.previous_state
if previous_state != SensorState.Stopped and SENSOR.state() == SensorState.Stopped:
if DEBUG: print("Publishing Final Stopped Message")
SENSOR.publish_attributes()
if previous_state != SensorState.Off and SENSOR.state() == SensorState.Off:
if DEBUG: print("Publishing Final Off Message")
SENSOR.publish_attributes()
if SENSOR.active:
SENSOR.publish_attributes()
def message_to_switch_entity(message):
"""
Converts MQTT Message to the corresponding switch entity
"""
topic = pathlib.PurePosixPath(message.topic)
message_type = SwitchType[topic.parent.stem] # Stream, Record or Profile
if message_type == SwitchType.stream:
return STREAM_SWITCH
if message_type == SwitchType.record:
return RECORD_SWITCH
if message_type == SwitchType.profile:
for profile in PROFILES:
if profile.command_topic == message.topic:
return profile
if message_type == SwitchType.scene:
for scene in SCENES:
if scene.command_topic == message.topic:
return scene
return None
# Using a global MQTT client variable to keep things simple:
CLIENT = mqtt.Client()
CLIENT.on_connect = on_mqtt_connect
CLIENT.on_disconnect = on_mqtt_disconnect
CLIENT.on_message = on_mqtt_message
@storm1er
Copy link
Author

You need a custom obs version for this to work ONLY IF YOUR CURRENT VERSION OF OBS CRASHES WHEN YOU TRY TO CHANGE SCENE

Here's the custom version: https://drive.google.com/file/d/1EFbeg1sTJ4v5OtqqnJc8ds4yytL05Rnj/view?usp=sharing

it's version 29.1.3 but with the fix from this pull request by @cg2121

⚠️ this means that if you use another version than 29.1.3 DON'T USE THIS

  • if that's the case, please check if the issue is still there
  • if it's still there, either rebuild it yourself or ask for another build on the given pull request

Why a weird downloadable zip?

Just so you can install this moded version easily, I initially tried to download it from the automated github action created by the pull request mentioned, but the artifacts expired. So I rebuild it locally and made a zip so I can share it.

Also, by this way you're sure to keep all existing settings, profiles, etc

But it's not installable

I didn't package because if you're here you probably already installed/configured obs, so

  • make sure obs is closed
  • unzip the folder directly in %ProgramFiles%/obs-studio
  • override all existing files
  • restart obs and refresh the script =)

@BenJamesAndo
Copy link

Works great, thank you!
My scenes only show up if there are no spaces. I wonder if there's any way around this to also allow scenes with spaces?

Some ideas for future development.
Controls:
Studio mode on/off
Mute/unmute active audio sources
Scene transition e.g. fade or cut

Attributes:
CPU usage
Stream & recording bitrate
Disk space available

@storm1er
Copy link
Author

@BenJamesAndo

My scenes only show up if there are no spaces.

Exact, same goes for profiles, I didn't bother make a sanitizer to avoid any issue with duplicates, but yes, no spaces are a requirements for now.

Mute/unmute active audio sources

For now I did this using https://github.com/LAB02-Research/HASS.Agent and hotkeys but that feels like a workaround, I'll do it better soon as I need to see audio sources details on my stream deck =)

Everything else is on low priority for now

Attributes:
CPU usage
Stream & recording bitrate
Disk space available

All this is already available using HASS.Agent ;)

@storm1er
Copy link
Author

storm1er commented Sep 26, 2023

Update: Fix mqtt sensor name should not be prefixed with device name.

(see this comment)

@DeFlanko
Copy link

Any idea what this Error means?

[update_mqtt_status_homeassistant.py] Exception ignored in: <function Client.__del__ at 0x000002A24C401B20>
[update_mqtt_status_homeassistant.py] Traceback (most recent call last):
[update_mqtt_status_homeassistant.py]   File "C:\Users\DeFlanko\AppData\Local\Programs\Python\Python312\Lib\site-packages\paho\mqtt\client.py", line 874, in __del__
[update_mqtt_status_homeassistant.py]     self._reset_sockets()
[update_mqtt_status_homeassistant.py]   File "C:\Users\DeFlanko\AppData\Local\Programs\Python\Python312\Lib\site-packages\paho\mqtt\client.py", line 1133, in _reset_sockets
[update_mqtt_status_homeassistant.py]     self._sock_close()
[update_mqtt_status_homeassistant.py]   File "C:\Users\DeFlanko\AppData\Local\Programs\Python\Python312\Lib\site-packages\paho\mqtt\client.py", line 1119, in _sock_close
[update_mqtt_status_homeassistant.py]     if not self._sock:
[update_mqtt_status_homeassistant.py]            ^^^^^^^^^^
[update_mqtt_status_homeassistant.py] AttributeError: 'Client' object has no attribute '_sock'
[update_mqtt_status_homeassistant.py] Traceback (most recent call last):
[update_mqtt_status_homeassistant.py]   File "D:\Google Drive/!Stream/OBS_Integrations\update_mqtt_status_homeassistant.py", line 771, in <module>
[update_mqtt_status_homeassistant.py]     CLIENT = mqtt.Client()
[update_mqtt_status_homeassistant.py]              ^^^^^^^^^^^^^
[update_mqtt_status_homeassistant.py] TypeError: Client.__init__() missing 1 required positional argument: 'callback_api_version'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment