Sunday, January 19, 2025

Remotely connect to Home Assistant hosted behind an ISP NAT

The home assistant instance I use is hosted on a Raspberry Pi 4 and it's connected to internet via an ISP who does not provide public IP addresses for home usage. Therefore it's not straightforward to allow access to the home assistance instance from internet.

Therefore I decided to use reverse SSH tunnel between the home assistant (raspberry pi) and my seedbox which already has an nginx instance running. 

you can gain SSH access to home assistant via Advanced SSH & Web Terminal add-on. 

Below command has been executed on home assistant to create the reverse SSH tunnel with the seedbox,

ssh  -fTNR  18123:localhost:8123 <user in seedbox>@<ip of the seedbox> -p <ssh port of the seedbox> -i <path to SSH private key to connect to seedbox>

-f for ssh background execution
-T to disable pseudo terminal allocation
-N to restrict remote command execution
-R [bind_address:]port:host:hostport. This specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side. Please refer man page of SSH for more details.

Once the SSH command is executed, you should be able to see a server is running in the remote machine, (in this case port 18123 in my seedbox),

netstat -anpt | grep 8123
tcp        0      0 127.0.0.1:18123         0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:18123               :::*                    LISTEN      -  

Please note that the bind address is 127.0.0.1 by default. if you want to change this, you will have to set clientspecified as the value for GatewayPorts configuration in /etc/ssh/sshd_config file of the remote server and then restart sshd service. Instead of configuring that (due to security risk), you can still expose the service running on localhost to internet via nginx.

below snippet added to the default file in /etc/nginx/sites-enabled directory,

location / {
     proxy_pass http://127.0.0.1:18123;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection 'upgrade';
     proxy_set_header Host $host;
     proxy_cache_bypass $http_upgrade;
 }

To avoid SSH tunnel connectivity issues, enable ssh keep alive for the SSH client. I added below 2 lines to ~/.ssh/config file in the home assistant. 

ServerAliveInterval 15
ServerAliveCountMax 3

Sunday, January 12, 2025

How to configure a Tuya compatible smart switch with Home Assistant

Prepare Raspberry Pi 4 with Home Assistant OS 

I used my Raspberry Pi 4 to install Home Assistant. The OS versions mentioned below may not be as yours. 

Use Raspberry Pi Imager to prepare the SD card. Select "Other specific-purpose OS" when you select the operation system and then select "Home assistants and home automation" and then "Home Assistant" and finally select "Home Assistant OS 14.1 (RPI 4/400)"






Log in to home assistant web portal when everything is ready.

Install HACS

Go to settings, click on Add-ons and then click on ADD-ON STORE button located in the right hand corner at the bottom. 



Then click on Repositories from the menu you get clicking on the three dots at top right hand corner.
Enter https://github.com/hacs/addons as the repository 


Then go back to settings, select Add-ons and click on 
ADD-ON STORE button. Then search for hacs. You will see the "Get HACS" repository. 


click on it and hit INSTALL button to get HACS installed. click on Start once the installation is done. 
Then restart Home Assistant. 

Once restarted, go to settings, click on Devices & Services.Click on ADD INTEGRATION button and search for HACS. select HACS and proceed with the instructions provided. once the github authorization is completed, HACS will be appeared in the Integrations list.



Open HACS by clicking on HACS in the left hand side menu. Since the default Local Tuya available with HACS was not working with protocol 3.5, I had to use a forked version of the original Local Tuya repository. Click on the 3 dots at top right hand side and select Custom Repositories.
 


Enter 
https://github.com/xZetsubou/localtuya as Repository and Integration as Type. Once that is added, the forked version of Local Tuya will be appeared in HACS. 


Install Tuya Local

Click on the three dots of the new Local Tuya appeared and hit Download.


Restart Home Assistant to complete the installation.

Configure Tuya Local

Go to Settings, select Devices & Services. Click on ADD INTEGRATION button and select Local Tuya. I didn't enter any details for Cloud API account configuration as I selected Disable Cloud API.
Now Local Tuya is appeared in the Integrations. 


 Click on Local Tuya and click Configure. Select Add new device. As it does not automatically detect the device, go ahead with Add Device Manually option. 

Notes: You need several information to proceed from here. However make sure you have configured your devices in your mobile app, either Smart Life or Tuya Smart.

1. Device ID

You can find out the Device ID from the application installed in your mobile (Smart Life or Tuya Smart).  Open the App, Select the device, click on the pencil icon at the top right hand corner and click on Device Information to find out the device ID. 


2. Local Key. 

You can find out the Local Key from https://auth.tuya.com/. Create an account there if you don't have a login already. 
Log in to Tuya account.  Click on Cloud and click on Create Cloud Project.



Make sure you select the correct Data Center. Otherwise it won't work. This document has the data center mapping. Select the data center based on the country you selected when you configured the mobile App. 
https://developer.tuya.com/en/docs/iot/oem-app-data-center-distributed?id=Kafi0ku9l07qb

In my case, it's Central Europe data center.


Then proceed with the default options provided.
Once the project is set up, go to Cloud again and Open the project. Select Devices and then Link App Account. Then click on Add App Account. A QR code will appear. 


Open your mobile App and scan it and confirm Login. Now the device(s) will be added to the project.



Open API Explorer from Cloud.

Expand Device Management and select Query Device Details. Enter the Device you found out in the step 1 and hit Submit Request button. 



It will provide a JSON response. You should be able to find the value for local_key in the JSON.

Now you have the Device ID and Local Key to proceed. Make sure to add a scan interval, otherwise the information will not get updated in Home Assistant portal. 



Once Submit, another window will appear. Select Configure device entities manually. Choose entity as Switch and proceed. To proceed further you need to know about DP IDs. You can find out the DP IDs from https://auth.tuya.com portal. 

Make sure you select Smart Home Basic Service and expand Smart Home Device Control section. Select Get Device Specification Attribute. Then enter your device ID and hit Submit Request button. 
A JSON response will appear with the DP ID mapping. This will be useful when the device is added to Home Assistant. 


I configured below values for the smart plug,



The device will be appeared in Local Tuya now,





Prepare a Dashboard

To plot the current, power utilization and voltage, I have added those attributes as entities. 
Go to Local Tuya, select Configure and select Reconfigure existing device.


Select Add new entity(s)


Select Sensor for the entity. Repeat the same for Power and Voltage. 









Saturday, February 15, 2020

High CPU utilization just after display sleep in Hackintosh

I noticed that after a few seconds the monitors went sleep, the CPU utilization and iGPU utilization of my hackintosh went significantly high. This was clearly noticed in the Intel Power Gadget as well,




I kept a terminal session open redirecting the output of top command in terminal (top -o cpu > bla.txt). From the output I was able to find out that mediaanalysisd process was utilizing the CPU and iGPU.

After unloading mediaanalysisd, the issue went away.

Uload command I used,
launchctl unload -w /System/Library/LaunchAgents/com.apple.mediaanalysisd.plist

Monday, February 10, 2020

Restore Google Chrome and Firefox Bookmarks from a backup

I had an issue with my Hackintosh, so that it wouldn't boot anymore. However I was able to mount the disk of the machine to another newly built Hackintosh which helped me to access the files of the troubled Hackintosh.

First I plugged the disk to the new Hackintosh

For Google Chrome,


1. Navigate to below location of the disk,

/Volumes/<volume name> - Data/Users/<username>/Library/Application Support/Google/Chrome/Default

2. There should be two Bookmark files in the directory:'Bookmarks' and 'Bookmarks.bak'

3. Check the latest by executing diff command

4. Copy the latest to below location of your new machine

/Users/<username>/Library/Application Support/Google/Chrome/Default


For Firefox,


1. Navigate to below location of the disk,

/Volumes/<volume name> - Data/Users/<username>/Library/Application Support/Firefox/Profiles/wzghbr3j.default/bookmarkbackups


2. There should be multiple files in the directory.
e.g.:

bookmarks-2020-02-08_744_<some-encoded-values>.jsonlz4
bookmarks-2020-02-09_744_<some-encoded-values>.jsonlz4


3. Pick the latest file and copy it to the desktop


4. Open Firefox, Select Bookmark and then Show All BookMarks


5. Click on Restore and then select choose file


6. Select the file from the Desktop

How to disable parsec-fbf which prevents Catalina Sleep functionality

1. Execute below command to find out which processes block sleep,

pmset -g assertions

2. Execute below command to disable parsec-fbf

launchctl unload -w /System/Library/LaunchAgents/com.apple.parsec-fbf.plist