{"id":701,"date":"2021-03-21T20:44:00","date_gmt":"2021-03-22T01:44:00","guid":{"rendered":"https:\/\/lab.rapternet.us\/?p=701"},"modified":"2021-02-06T20:46:49","modified_gmt":"2021-02-07T02:46:49","slug":"calling-hass-api-from-various-shells","status":"publish","type":"post","link":"https:\/\/lab.rapternet.us\/?p=701","title":{"rendered":"Calling HASS API from Various Shells"},"content":{"rendered":"\n<p>While working on making my macro pad trigger Home Assistant automations, I found the API would be the easiest way to integrate the systems. Here&#8217;s some of my experimenting with the API and how to call it from Linux curl and PowerShell Invoke-WebRequest<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"linux-curl\">Linux Curl<\/h1>\n\n\n\n<p>Using the Linux curl command is pretty straight forward.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>curl -X POST http:\/\/hass.local:8123\/api\/services\/switch\/toggle -H 'Authorization: Bearer ABCDEF' -d '{\"entity_id\": \"switch.testsubject_test_subject\"}'<\/code><\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"powershell-invoke-webrequest\">PowerShell Invoke-WebRequest<\/h1>\n\n\n\n<p>PowerShell is a bit more annoying to get everything right, as Invoke-WebRequest is a bit pickier on its input arguments.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Invoke-WebRequest -Method POST -Uri \"http:<em>\/\/hass.local:8123\/api\/services\/switch\/toggle\" -H @{\"Authorization\"=\"Bearer ABCDEF\"} -Body '{\"entity_id\": \"switch.testsubject_test_subject\"}'<\/em><\/code><\/pre>\n\n\n\n<p>Apparently windows has a build of the official Unix curl and tar commands, but my PowerShell prompts all had curl as an alias to Invoke-WebRequest. I did find that if i call curl.exe, it would run the official curl command.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h1>\n\n\n\n<p>Do be careful when copying and pasting between shells, I found that copying out of the PowerShell prompt and back in would yield an invalid token. This was also encountered in the WSL terminal as well.<\/p>\n\n\n\n<p>The API is pretty straight forward after a little bit of trying things out. The documentation covers the capability but doesn&#8217;t have as many examples as I would have liked otherwise.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"resources\">Resources<\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/developers.home-assistant.io\/docs\/api\/rest\/\">Home Assistant API Documentation<\/a><\/li><li><a href=\"https:\/\/www.home-assistant.io\/integrations\/api\">Home Assistant API Integration<\/a><\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/remote-rendering\/how-tos\/tokens\">Microsoft Docs on Tokens<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>While working on making my macro pad trigger Home Assistant automations, I found the API would be the easiest way to integrate the systems. Here&#8217;s some of my experimenting with the API and how to call it from Linux curl and PowerShell Invoke-WebRequest Linux Curl Using the Linux curl command is pretty straight forward. PowerShell &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/lab.rapternet.us\/?p=701\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Calling HASS API from Various Shells&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[39,14,49],"class_list":["post-701","post","type-post","status-publish","format-standard","hentry","category-how-to","tag-home-assistant","tag-quick-bites","tag-smart-home"],"_links":{"self":[{"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=\/wp\/v2\/posts\/701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=701"}],"version-history":[{"count":3,"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=\/wp\/v2\/posts\/701\/revisions"}],"predecessor-version":[{"id":705,"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=\/wp\/v2\/posts\/701\/revisions\/705"}],"wp:attachment":[{"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lab.rapternet.us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}