Skip to content

Instantly share code, notes, and snippets.

@NotAFood
Created June 4, 2023 00:01
Show Gist options
  • Save NotAFood/6d864166c7a239ca0f35fb0cd4a2a3bc to your computer and use it in GitHub Desktop.
Save NotAFood/6d864166c7a239ca0f35fb0cd4a2a3bc to your computer and use it in GitHub Desktop.
[power printer] # Recommend assigning a static or manual IP to Wemo plug before adding to configuration
type: http
locked_while_printing: True
restart_klipper_when_powered: True
restart_delay: 2
bound_services: klipper
on_url: http://192.168.0.43:49154/upnp/control/basicevent1 # Some Wemo devices might use 49153
off_url: http://192.168.0.43:49154/upnp/control/basicevent1
status_url: http://192.168.0.43:49154/upnp/control/basicevent1
request_template:
# Keep in mind semicolons (U+003B) and hash signs (U+0023) must be escaped unicode literals
# If \# is used instead to bypass the configuration comment interpreter jinja2 will still see \# in the string instead of #
{% do http_request.set_method("POST") %}
{% set content_type_bearer = 'text/xml\u003b charset=\"utf-8\"' %}
{% if command in ["status"] %}
{% do log_debug("Attempting to send Wemo status request") %}
{% set soap_action_bearer = '\"urn:Belkin:service:basicevent:1\u0023GetBinaryState\"' %}
{% set xml_body = '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"></u:GetBinaryState></s:Body></s:Envelope>' %}
{% do http_request.add_header("CONTENT-TYPE", content_type_bearer) %}
{% do http_request.add_header("SOAPACTION", soap_action_bearer) %}
{% do http_request.set_body(xml_body) %}
{% do http_request.send() %}
{% endif %}
{% if command in ["on", "off"] %}
{% set soap_action_bearer = '\"urn:Belkin:service:basicevent:1\u0023SetBinaryState\"' %}
{% do http_request.add_header("CONTENT-TYPE", content_type_bearer) %}
{% do http_request.add_header("SOAPACTION", soap_action_bearer) %}
{% if command == "on" %}
{% do log_debug("Attempting to send Wemo power-on request") %}
{% set xml_body = '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:SetBinaryState></s:Body></s:Envelope>' %}
{% elif command == "off" %}
{% do log_debug("Attempting to send Wemo power-off request") %}
{% set xml_body = '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>0</BinaryState></u:SetBinaryState></s:Body></s:Envelope>' %}
{% endif %}
{% do http_request.set_body(xml_body) %}
{% do http_request.send() %}
{% endif %}
response_template:
{% do log_debug("Got status code " ~ http_request.last_response().status_code) %}
{% if http_request.last_response().status_code == 200 %}
{% set start_tag = '<BinaryState>' %}
{% set end_tag = '</BinaryState>' %}
{% set start_index = http_request.last_response().text.find(start_tag) + start_tag|length %}
{% set end_index = http_request.last_response().text.find(end_tag) %}
{% set binary_state = http_request.last_response().text[start_index:end_index] %}
{% do log_debug("Plug state binary is " ~ binary_state) %}
{% if binary_state == "1" %}
{% do log_debug("Plug marked as on") %}
{"on"}
{% else %}
{% do log_debug("Plug marked as off") %}
{"off"}
{% endif %}
{% endif %}
@awdev1
Copy link

awdev1 commented Apr 13, 2024

Seems to work well! Although my wemo smart plugs WSP080 seem to use port 49152 instead of 49153 or 49154. Found this out with pywemo!

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