I want to create an integration, but don’t know how to program…

Well, like it says. I’m capable of reading other people’s code and getting the gist of what it does, but there’s no way I’d be able to write code from scratch without a lot of learning.

I have a Magnetar Blu Ray player, and there’s no current integration for it. I have a list of hex commands that can control the player, and I’ve been able to use them to control the player using both Telnet and a packet sender. I’d like to turn this list into an integration.

Is there a skeleton code for an integration where I can just plug in an IP address and port to target, then name a bunch of commands and plug in the hex codes to send to that address?

I’m assuming it’s Just Not That Simple, but if anyone can point me in the right direction I’d be very appreciative!

1 Like

I donot know how to integrate with Magnetar itself but if you want to go down (up) a path then I would start to look at existing media players. There are those part of core and those in HACS. The latter allows you a lot of freedom whcih can be helpful during growth and getting feedback…moving this to core would be the final step then.
In HACS you have wiim, denon , etc. you could copy a repo and start form there?

Thanks for the reply.

I could try installing the Oppo integration from HACS, since the players are quite similar. Not sure I’d know where to go from there, but I might start looking at it in the next couple of days.

The oppo one is pretty old and although it might still work, I would recommend to take bits and pieces of a newer ‘core’ integration to stay more in line with HA, e.g. have a look at the media_player.py and compare that with the old ones.
Also note that quite a few integrations use an underlying library for controlling the device, I am not sure if this exists for Magnetar…if not, then you would have to set these up locally in your integration…e.g. the yamaha one below

As you already stated it is not Just That Simple but can be lots of fun too :slight_smile: But… if you want to only control a few bits and pieces and not go all this way then posisbly you can create a set of command_line (or rest?? I donot know how to connect) entities instead, I did a few for things that the Denon integration does not cover.

core/homeassistant/components/denonavr at dev · home-assistant/core

yamaha_ynca/custom_components/yamaha_ynca at dev · mvdwetering/yamaha_ynca

If you look at this code, you might get how hex commands work

Well, I’ve spent the past hour or so looking over the code for the Oppo integration. Wondering if I can learn enough to modify the heck out of someone else’s code and make it work.

Something I don’t understand, though: it looks like the person who created the Oppo integration created both an integration and an SDK. The integration imports things from the SDK, but the SDK isn’t included in the integration when it’s downloaded (manually or through HACS). Others online have had success integrating their Oppo players, so the code works.

Is there something I’m missing about how integrations and SDKs work in Home Assistant? Do SDKs not need to be downloaded in order to be called upon by integrations?!?

Yeah…that is what I meant with the others loading libraries. The library is loaded upon install, via manfifest.json
ha_oppoudp/custom_components/oppo_udp/manifest.json at master · simbaja/ha_oppoudp

Not a real developer myself but from what I understand … in theory one does not need to have a separate library but it makes it easier to maintain when things change on the provider (in this case: oppo) side and keeps the HA part lean. Since no-one has created a library for Magnetar yet, it may be overkill to create it now, if you keep it a HACS integration then this will be fine, for core they will demand a lib and core-alike setup.

If the oppo one comes close to what you need, there is no harm in asking simbaja for his/her view for your ideas.

Again, thanks! I’m looking at manifest.json right now, and it lists the SDK in “requirements.” But in the installation instructions it only mentions downloading the files from ha_oppoudp (the integration itself) and not anything from the SDK.

Am I missing something in how this is handled? Would a more experienced HA user than myself know to download an SDK separately without it being in the instructions?Or is the library kept on a server somewhere and downloaded whenever an Integration puts it under requirements?

When you place the files in the custom_components folder (or use HACS) then during the HA-loading, the manifest is read by HA to load the libraries
The ha_oppoudp code/files then refer to that, i.e. you will find multiple lines

from oppoudpsdk import ...

e.g. here
ha_oppoudp/custom_components/oppo_udp/media_player.py at master · simbaja/ha_oppoudp

Also, good point - it hadn’t occurred to me that Simbaja would be on these forums and might be willing to help. I sent a message just in case!

I would, if you would ask me then I would not build a library and try to keep it as simple as possible in a first throw… to get it working without a library is already quite a challenge.

This is one of my old ones, it was not built by me but is a originally copy from someone elses (forgot who) and this uses a simple http to get / send data Wiim. In a similar way you could execute the command to your Magnetar I would guess, I think you need a terminal type of request (never done this before)

wiim_ng/custom_components/wiim_ng/media_player.py at main · vingerha/wiim_ng

Yep, that was the line that led me to finding the SDK’s code on GitHub!

I still feel like I’m missing something. I definitely understand that the Integration tells HA to load the library. However, I’m confused as to how HA has access to the library, since it wasn’t downloaded as part of the integration and isn’t a native part of HA Core. The code doesn’t list a URL where the library would be accessed or automatically downloaded from.

Is there some other way that HA knows that the library exists and loads it when asked to do so by the Integration?

HA reads manifest.json and the does a pip install .... so it becomes part of the HA environment. Those libraries are open published pypy

GOT it! Thanks for explaining. Back to trying to understand this code, then!

Here’s a broader tip for your learning: by default, Home Assistant sets the logging level to WARNING. If you switch it to INFO or DEBUG, things like this become a lot more obvious, because you’ll see Home Assistant installing the library via pip.install.

To change it: Logger - Home Assistant

I’ll take a look.

Admittedly, I’ve been reading the code on GitHub rather than through HA. Might be time to change that too.

Not to be off-putting but writing an integration from 0 / zero is more that quite a bit of work. If you have the time and see this as part of fun … then really do continue, this is how I learned and gained/lost. But if your resources are limited, then one could try to deviate to simpler solutions :slight_smile:
I did some further searching and Magnetar seems to be very specific… your effort should benefit more people too so effort/gain ??

Yeah, I doubt I’m going to try to make an integration from scratch. But I’m betting that I can fork Simbaja’s Oppo integration to make something that works for Magnetar. We’ll see how far I go, I suppose!

Good luck then, tip 1 is to not implement config-flow before you have an actually working media_player entity. I used a lot of hardcoded attribs to see if it actually was stable and then spent the rest of the time on tuning it…80/20 > 20/80 rule