{"id":1161,"date":"2024-12-24T18:42:12","date_gmt":"2024-12-25T00:42:12","guid":{"rendered":"https:\/\/thecweb.com\/?p=1161"},"modified":"2024-12-24T18:42:12","modified_gmt":"2024-12-25T00:42:12","slug":"python-interface-to-kasa-devices","status":"publish","type":"post","link":"https:\/\/thecweb.com\/index.php\/2024\/12\/24\/python-interface-to-kasa-devices\/","title":{"rendered":"Python interface to Kasa Devices"},"content":{"rendered":"\n<p>The starting point for this project is the Kasa powerstrip I posted about, like  a month ago.  I&#8217;m trying o code up something to log power levels directly to a mysql db.<\/p>\n\n\n\n<p><a href=\"https:\/\/python-kasa.readthedocs.io\/\" data-type=\"link\" data-id=\"https:\/\/python-kasa.readthedocs.io\/\">Python Kasa library<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/dev.mysql.com\/doc\/connector-python\/en\/connector-python-introduction.html\" data-type=\"link\" data-id=\"https:\/\/dev.mysql.com\/doc\/connector-python\/en\/connector-python-introduction.html\">Python MySQL library<\/a><\/p>\n\n\n\n<p>Both seem to have good documentation on how to use them.<\/p>\n\n\n\n<p>The kasa library requires asyncio, which I haven&#8217;t really messed with async programing before, so I get to learn some new concepts.  Though what little I know tells me it&#8217;s just a way to keep a connection open while waiting on the other end to respond.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>asyncio is a library to write&nbsp;<strong>concurrent<\/strong>&nbsp;code using the&nbsp;<strong>async\/await<\/strong>&nbsp;syntax.<\/p>\n\n\n\n<p>asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web-servers, database connection libraries, distributed task queues, etc.<\/p>\n<cite><a href=\"https:\/\/docs.python.org\/3\/library\/asyncio.html\">https:\/\/docs.python.org\/3\/library\/asyncio.html<\/a><\/cite><\/blockquote>\n\n\n\n<p>This is really all I need to know, I think.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"1024\" src=\"https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-13-834x1024.png\" alt=\"\" class=\"wp-image-1163\" srcset=\"https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-13-834x1024.png 834w, https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-13-244x300.png 244w, https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-13-768x943.png 768w, https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-13-1251x1536.png 1251w, https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-13.png 1263w\" sizes=\"auto, (max-width: 834px) 100vw, 834px\" \/><\/figure>\n\n\n\n<p>I&#8217;ve slapped together this to print the data I want with python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import asyncio\nfrom kasa import Discover\n\nasync def main():\n    dev = await Discover.discover_single(\"powerstrip.lan\")\n    await dev.update()\n\n        # possible features:\n        # state\n        # rssi\n        # on_since\n        # reboot\n        # led\n        # cloud_connection\n        # current_consumption\n        # consumption_today\n        # consumption_this_month\n        # consumption_total\n        # voltage\n        # current\n   \n    state = dev.features.get(\"state\")\n    voltage = dev.features.get(\"voltage\")\n    current = dev.features.get(\"current\")\n    current_consumption = dev.features.get(\"current_consumption\")\n    consumption_this_month = dev.features.get(\"consumption_this_month\")\n    consumption_today = dev.features.get(\"consumption_today\")\n\n    # open file and write values\n    f = open(\"power.txt\", \"w\")\n    f.write(f'Power On: {state.value}&lt;br>{voltage.value} V {current.value} A {current_consumption.value} W&lt;br>Today: {consumption_today.value} kwh&lt;br>This Month: {consumption_this_month.value} kwh')\n    \nif __name__ == \"__main__\":\n    asyncio.run(main())<\/code><\/pre>\n\n\n\n<p>Which gives me this in WordPress:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"658\" height=\"222\" src=\"https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-14.png\" alt=\"\" class=\"wp-image-1165\" srcset=\"https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-14.png 658w, https:\/\/thecweb.com\/wp-content\/uploads\/2024\/12\/image-14-300x101.png 300w\" sizes=\"auto, (max-width: 658px) 100vw, 658px\" \/><\/figure>\n\n\n\n<p>Not directly of course, I haven&#8217;t gotten that far yet.  For now, the python writes a text file with HTML tags for display.  I have an hourly cron job that uses scp to copy the file over to \/var\/www, and then some php in my functions.php file to load the file, and display it as a wordpress shortcode.<\/p>\n\n\n\n<p>The stupid WordPress part took the longest, but I understand the layout of wordpress a little better.  It has been around and popular for so long, that it is easy finding well written documentation, but I&#8217;ve found that a lot of it is outdated.  It is very annoying to read through something, try it out, and then find your error is because you&#8217;re trying to use a deprecated feature.  <\/p>\n\n\n\n<p>The vision is a cron job that runs a python script to import data directly into the DB, and then php pulls the data from the DB, but the next step is to just get something into to DB.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The starting point for this project is the Kasa powerstrip I posted about, like a month ago. I&#8217;m trying o code up something to log power levels directly to a mysql db. Python Kasa library Python MySQL library Both seem to have good documentation on how to use them. The kasa library requires asyncio, which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14,45],"class_list":["post-1161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-linux","tag-python"],"_links":{"self":[{"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=1161"}],"version-history":[{"count":2,"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1161\/revisions"}],"predecessor-version":[{"id":1166,"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1161\/revisions\/1166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/media\/1162"}],"wp:attachment":[{"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=1161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=1161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=1161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}