Custom Component Question

Hi Community,

So I’m working on converting my python script into a device_tracker custom component and at the same time, learning how to go from procedural python to object-oriented python (so far, it seems straight forward) but I’ve been looking over other community-provided custom components and had questions that I can’t seem to resolve myself (by perusing sources and docs)

a) Can there be more than one class? From what I understand, a “Class” is a blueprint for data and I have three of them; one for zone, one for meta_tracker and one for trigger_entity. Is this allowed?

b) Some custom components I’ve looked at seem to bundle the entire “runtime” under one Class whereas others seem to use no Classes and it looks almost procedural vs OO

I’m sure I’ll have more but for now, these are my biggest ones.

Thanks!

1 Like

Hi there, good to see you working on such stuff! I don’t know the answer, but just wanted to make sure you are aware of this resource:

Much appreciated. I have been spending a lot of time here and youtube videos on OOP for Python but can’t quite figure out the answers I’m looking for :slight_smile:

In a practical sense, you can have as many classes as you want inside a custom component and instantiate them as you please and it will work.

Now, if you read this check list for creating a custom component: https://developers.home-assistant.io/docs/en/creating_component_code_review.html

The HA developers specifically say that:
“All API specific code has to be part of a third party library hosted on PyPi. Home Assistant should only interact with objects and not make direct calls to the API.”

I wrote a custom component (binary sensor) just a couple of weeks ago. It is a simple component, in case you want to take a look at the code, it is here:
https://github.com/jardiamj/homeassistant/blob/master/custom_components/binary_sensor/mcp23017.py

I am using some Python modules hosted in PyPi, you just need to specify them in the REQUIREMENTS HA will install them on the fly. Also, the import has to be done inside the function that requires them.

So, your best bet if you want to try to get your custom component Incorporated into HA later on, would be to turn your Python script into a module hosted in PyPi and put it in the REQUIREMENTS.

You can always do that later, I would suggest keeping the your platform specific code separate in their owns classes as you some appropriate. And create instances of them in the setup_platform function and the class that will inherit from the appropriate homeassistant object.

I hope I am not sounding too confusing. I would like to take a look at your code, do you have it hosted somewhere?

I am also still learning how HA engine works.

Jardi.

Thanks, this does help, a lot. Frankly, while your component looks simple enough, my understanding of Python in this sense is still limited and I have some trouble following what yours is doing but get the general gist of it. I have a working Python-script version here (although this version needed adaption for the newer 0.8x.x releases, minor ones):

Which I’m converting to a custom component now that I figured out where the hang-up is when I upgraded to 0.8x.x – my component doesn’t rely on any third-party components, simply relies on the state engine for zone, trigger and device_tracker so it should be straight-forward.

My biggest stumbling block is I’m trying to learn OOP from a procedural background and how it all works together with HASS.

I don’t have my code anywhere yet (the new stuff) and I’ve talked with @pnbruckner who gave me some advice and his composite device_tracker component shares a lot of similarities with what I’m trying to do here so I’ve been using his code as a template/base in which to adapt mine.

On the plus side, I was able to install home assistant inside PyCharm as a module which has helped me learn a lot faster due to the syntax guiding it does, etc so once I have this fleshed out properly I should be able to test it – as it stands right now it’s 50% custom component and 50% python script so it doesn’t exactly run yet. But if you want to get a general idea of what direction I’m heading in, look at my Zanzito filter script link above and check out his composite device_tracker custom component here:

You do realise zanzito is no longer developed and in fact I think is no longer on google play?

Just don’t want to see you waste your time, you may know more about the future of zanzito than I do :slight_smile: I am sure a lot of people would like to see it revived!

yeah, haven’t looked in a while - switched to owntracks due to zanzito crashing on Oreo but I’ll probably make the switch to Ariela at some point. I still see the Zanzito key (I bought it) which kinda sucks but oh well. The python script and ultimately the custom component I’m working on only relies on device_tracker GPS data, even use it with my wife’s iphone.

End of the day, Zanzito is where I started this journey into Python :slight_smile: and I’ll keep moving forward even if it didn’t.