New integration: Magic Swithcbot

Tags: #<Tag:0x00007fc42631a8e8>

Hi there.

I am developing a new component that allows to integrate Magic Switchbot devices, similar to others like the “Switchbot”. It uses BLE (Bluetooth Low Energy) and has 2 modes:

  • Push mode.
    It allows us to push momentary switches. It presses for a second and then retracts its little “finger”.
  • On/Off mode.
    With the help of a “hook” that you can stick, it has 2 states: “On” state presses and stays there and “Off” state pulls from the hook and lets the original switch to deactivate.

The component creates the new switch integration “magicswitchbot” and also publishes a service that allows us to activate the “push” mode.

You can follow the progress of the component in my GitHub repository:

You can test the component witch HACS using this as a “custom repository”.

NEWS: the component is now part of the “default” HACS repositories. You don’t need to add it as custom repository any more.

Also there’s a Python library I developed that’s used internally by the component and can be used on other projects. There’s a lot of documentation here:

I hope it works for you.

6 Likes

New release v0.0.3 at the component repository. Available in HACS.

A lot of changes.

The connection strategy has changed:

  • In previous releases, the connection was made at start and tried to be kept active every minute (when we got the battery level) and every time we issued a command to the device.
  • Now, there is no connection made until the moment we need to do it. When you:
    • Turn on.
    • Turn off.
    • Call the “push” service.

If no connection is active, the component tries to connect (3 times, for 5 seconds evert attempt) and then execute the command. If the command succeeds, the state of the switch is changed accordingly. If it fails, the state does not change and the attribute last_action gets the Error value.

The attribute battery_level is updated only when a command is executed.
Once the command is executed, the connection stays for 30 seconds. If you don’t issue a command in this time, the connection is closed. There’s also an attribute connection that holds a boolean with the current connection status.

This approach has some advantages:

  • You can control the device from several points: another HA instance, the original app, etc.
  • The connection is established only when needed.

But also it has some cons:

  • If the connection is not established when you want to switch on or off the device, it has to connect first, so it is slower.
  • You don’t have an updated value of the battery if no action is taken on the switch.

I hope it works OK for you.

At the moment It works perfectly! Very good job!! It’s perfect to activate my A/C from home assistant. Thank you very much!!

1 Like

New release: v1.0.0

Hope it helps.
Thanks @chronoszgz for pointing me about this bug.

I finaly got my magicswitchbot and I’m able to work with it using the on/off method. Unfortunately I’m not able to make it work with the push method, probably because I’m doing something wrong. I’m running Hassio and the latest official HA 2021.4.2.
When I use the service to send the push nothing happens and I see in the log "There is no Magic Switchbot defined as ‘switch.magic_sofa_lamp’ but it is defined because I can use the on/off switch. Probably I’m not using the correct syntax calling the service.
I’m using the following data:

service: magicswitchbot.push
data:
  entity_id: switch.magic_sofa_lamp

Please can anyone help me?
Thanks in advance

Hello, and thanks for testing the component.
First I need to check your switch configuration, not only the service call.

Could you paste your magicswitchbot switch setting in Home Assistant?

And then if you could paste your logs in debug mode it would be the most helpful.
Thanks!

Very exciting to test tonight because the original switchbot is not as good as the MSB. Bo battery, many compatibility problem…
Thank you

I hope it goes well

Hi there!
First off all. Thanks for the development of such a great solution to use this magicswitchbot.
I’ve installed the integration and sometimes the delay is larger than 5secs. And while ha is waiting for the switchbot to respond and realise the sent command, my whole ha gets stuck.
I don’t know if this is happening to someone else.

This is what I got from the logs:

Logger: homeassistant.helpers.entity
Source: helpers/entity.py:371
First occurred: 11:47:29 PM (1 occurrences)
Last logged: 11:47:29 PM

Updating state for switch.magicswitchbot (<class 'custom_components.magicswitchbot.switch.MagicSwitchbotSwitch'>) took 0.505 seconds. Please report it to the custom component author.

Hey, thanks!

I cannot reproduce these “freezes” in my installation, but there are more users reporting them.
I think there must be some kind of interaction between my component and some other components using bluetooth. This is an old known behaviour of the bluetooth stack and I’m trying to find a fix.

Hello,
Thank you for writing this integration. Since I don’t have all the skills and not linux, would it be possible to integrate it in HACS?
Thank you for your feedback and again thank you for the time spent on this integration

Hi,
I had also earlier this “freezes” issue, but after disabling bluetooth device trackers, the problem was solved. I am using this skill every day for (about) more then a week now, but without any issue. I am very glad for your work. Thank you!

1 Like

Hello.

The component is integrated in HACS but yet not in default repositories. You have to add the repository as “custom”. Please read the documentation in github README.

1 Like

Hi, thanks for the link.
How can i do this ?
" You can install this component with HACS. Add https://github.com/ec-blaster/magicswitchbot-homeassistant as an integration “custom repository” and install it. "
This can be a stupid question but im new in home assistant. I love the compatibility and opportunity of this software
Best regards. Bowen

Hi,

Thanks for sharing @ec-blaster!

I am a complete noob with regards to HA. I just got to make my first automations and scripts.
At the moment I am trying to understand and incorporate your code to integrate my MagicSwitchBot, but I am not able to get it to work.

Can somebody help me out a little to get me started? To be honest I do not understand much from the article on git-hub yet. Where do I for example run the sudo functions you are referring to? How can I run some py scripts on HA?

All help, tips and links to additional material to read are much appreciated!

Cheers,
Job

1 Like

1
2


Hi again,

I got a hole lot closer I feel. In the meantime I read a lot and watched some videos. I was able to integrate it as a custom repository via HACS.
However, the most important thing, it does not work yet. I can toggle the switch, but nothing happens. Can you see from these snapshots anything vital that I miss?

Thanks a lot!
Job

Edit: sorry for spamming this thread. After a lot of restarting and random tries I got it to work. I feel it only works for me when I have the Bluetooth tracker enabled. It started working when I had a timer active from the magic app itself, but might very much be random coincidence.

Only thing that I cannot get to work yet is an automation or script with switch. Anybody with issues there too?

Hi, @MassiveDynamic , and sorry for the delay, I’ve been busy.

In HACS you can search for “default” repositories or you can add your own ones.
My repository is not yet in the default list, so to add it as a cusstom repository you must do the foloowing:

  • Select the “Integrations” options in HACS.
  • At the top right you have a three- points icon. Press it and you get a menu.
  • In this menu, select the option “Custom repositories”.
  • A dialog appears and at the bottom you have three fields:
    • Custom repository URL: Enter here my repository url.
    • Category: Select “Integration”.
    • Now press the “Add” Button.
  • Now you have a new entry in the repository list, with the name Magic Switchbot.
  • Select it and install.
  • Now you only have to restart HASS and the component must be working.

Hope it helps.

Hi, @smartjob, and thanks for your feedback.

Don’t worry to be such a noob, we all are or were in some moment!

In regard to what you say, I think your configuration is fine, but some notes about the component and the device:

  • There is some kind of interaction when you use the same bluetooth adapter for different tasks.
  • In HASS you can install several components using bluetooth (device trackers, temperature sensors, my component, etc), but in the end, if 2 or more components are requesting to connect, they may clash, and this is the root cause of several freezes some users have. It’s difficult to synchronize several pieces of software built by several people in a way that they cooperate… Anyway I’m at it…
  • Other things to consider is that the device seems to have a poor antena or whatever so in my experience, you have to place the bluetooth adapter relatively near, or you can get into connection losts.
  • The original app is another piece of the puzzle: you cannot control the Magic Switchbot devices from two clients at the same time. This is: if you keep the smartphone app open, it stays connected to the switch, so it cannot be controlled by HASS. You must close the app before controlling it.

Anyway, if you have problems, you can attach your logs (preferably in debug mode) here or as a github issue and maybe I can investigate further.

Good evening.
I want to thank you for this tutorial. Don’t worry about the weather. I am very happy to have passed this step whose tutorials did not help me. Hoping to understand for myself even using the documentation later without having to bother you again.
Thank you again and see you soon :slight_smile:

1 Like

THANK YOU it work perfectly !!!

1 Like