Custom component alarm - need help

Hello,

I have a very old alarm, which works great, with an old web admin site.
Some years ago, I’ve developped a standalone python script which permit me to (depending the function called) :

  • retrieve the alarm status
  • set the alarm on
  • set the alarm off.

I’d like to integrate it into Home Assistant.
I’ve tried to read all the documentation, see some example, but it is still not clear for me.

I don’t really know where to start. But as my tool is already working, I’m pretty sure that there is not so much work :frowning:

Is it a sensor that I should develop ? What should be the structure ?
Any advice ?

THanks.

Well…python a script and communication protocol might help here is shown …without any information it’s hard to guess

Well it is something pretty simple, a website scrapping with beautifulsoup and mechanize.
The python script reads the config.ini file, and use it then to login to MySite, and based on the method, it retrieves status.

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import mechanize
import configparser
import io
import ssl

class MySiteException(Exception):
    def __init__(self, value):
         ...
    def __str__(self):
         ...
class MySite:
    def __init__(self, config):
        ssl._create_default_https_context = ssl._create_unverified_context
        self.config = config
        self.browser = mechanize.Browser()
        self.browser.set_handle_robots(False)
        self.browser.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36')]
        
    def login(self):
         ...
    def logout(self):
         ...
    def set_alarm(self):
         ...
    def unset_alarm(self):
         ...
    def get_state(self):
        ...
        return result
            
if __name__ == '__main__':
    config = configparser.ConfigParser()
    config.read('config.ini')
    MySite = MySite(config)
    MySite.login()
    print(MySite.get_state())
    MySite.logout()

Based on what you’ve written, this sounds like a switch. It has a state and can be turned on/off. Have you looked at the developer docs?

Yes. I’ve read it.
But in fact, I’m a little lost what should I put into init.py file.
Should I create a dedicated sensor.py or switch.py, where it will monitor the changing state ?

Is it in the init.py that I should enable my “object” MySite ? But what if the connection is lost ? From what I understand the init is run only one time.

To summarize :

  • where (in which HA file) should I call my method login() / logout() ?
  • in which should I call my method getState() ?

The goal is to have an up to date state, not only getting the state just one time.

For simplicity, you can put everything in the __init__.py file. Check out some examples in the source code under the homeassistant/components folder.

You can put your MySite code in there. You will need to create a switch entity class that instantiates your MySite class in one of the setup methods.

You can also create a switch.py file and put it in there. But you always need to have the __init__.py file (it can be empty).

I’d suggest first creating a switch that just sets the state according to the turn off and turn on methods. Then implement your MySite functionality.

Check out the scaffolding script referred to in the dev docs, they might be able to get you up and running on this first part.

Thanks, it starting to be clear.

If I put everything in init.py, what is the check interval ? Does HA will check the state just one time, at the initialization ? Or will it do it regularly ?

Maybe the best solution is to have a switch.py and a state.py

So I’ve created a MySite component, which calls a MySite.binary_sensor.

In my configuration.yml I’ve put :
MySite:
myconfig…

binary_sensor:

  • platform : MySite
    myconfig

It looks like it works but

  1. How do I show the MySite state in the web interface ?
  2. How can I share the same config between the module and the binary sensor ? instead having the 2 lines like shown above.