Python Socket App

So I have a python script that connects to a remote port via IP and listens for json based output. I would like this to stay connected all the time as the device sends status updates and I can trigger events when this happens, as well as update the status of the device. (I will have multiple devices as well)

I have set this up using Appdeamon and its working, but I am not sure this is the best way to do this as I believe it will hold up a thread, and because of this it does not ever reload new code in appdaemon unless I restart appdaemon.

So I am wondering if there is a better/easier way to have this run all the time in Hassio? Is this the best way? will this cause any issues?

Just hacking away here, but its doing what I want. Let me know what you think.
Thanks!

In the future, post these questions in the AppDaemon section so that they are more easily found by AppDaemon users.

You are right, holding up the AppDaemon thread isn’t the best way to go. It will cause issues with other AppDaemon apps being able to respond in a timely manner.

Solutions (in order of least to most difficult, in my opinion):

  1. Upgrading to the current AppDaemon 4.0 “dev” will allow you to use async in AppDaemon Apps. This will solve your problem by not tying up the thread. This is made a bit more difficult if you’re using the hass.io addon for AppDaemon since it doesn’t support AD 4.0 yet. However, these instructions (thanks to @basnijholt ) should get you started: https://github.com/basnijholt/home-assistant-config/blob/bbb34cd0917783a3f1ff7f647dabdaea857367fd/appdaemon/hassio_appdaemon4_beta_installation_instructions.md

  2. Writing this code as a separate process that calls a webhook when an update is seen. This takes some extra effort because you’ll have to manage the running process yourself.

  3. Writing a custom_component for Home Assistant that handles all of this.

  4. Writing a hass.io addon that basically accomplishes option #2 in a different way.

1 Like

Thanks Daniel,

I moved it to Appdameon, I thought about that after I posted! :slight_smile:

So I just installed AppDaemon for this purpose, but I see how it could be useful so I want to do it right. I will take a look at version 4, but I feel like I still may not be doing it the correct way.

I am not a coder, but I will take a look at the custom_component, and or plugin route but I feel that will take much more research!

Thanks for the response!
Bill

Feel free to share the AppDaemon code you have if it isn’t working correctly. We’re happy to help!

1 Like

Hi, Bill. I wonder if you could share a bit about how to configure the AppDaemon’s IP and port to set up the TCP Server? When I tried something like:

import socket

class HelloWorld(hass.Hass):
  #ServerIP and port config.
  PORT = 5000    #udp protocol port
  ServerIp = '192.168.10.52'
  #*************config end*************

  def initialize(self):
    self.log("Ready the server socket!")
    self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP
    self.server_address = (self.ServerIp, self.PORT)
    self.server_socket.bind(self.server_address)

I got OSError: [Errno 99] Address not available. In fact, I found that the IP address of AppDaemon seemed to be 172.30.33.x. I wonder how did you solve this problem? Thank you.