Whenever I change the physical esp32 board running ESPHome, HA sees it as a new ESPHome integration since the MAC address is not the same as the old one.
But I want the new board to fully replace the old board and reference the same database metadata_id’s without creating a new device and entities in HA.
Now I know that one way to get around this is to change all occurrences of the old MAC address in core.device_registry and core.entity_registry files and in the esphome.XXXXXX file for the device.
But for me it would be cleaner just to change the MAC address of the new esp32 board to match that of the old one (which is possible for esp32’s).
I can (successfully) change the Wifi MAC address by running esp_wifi_set_mac(WIFI_IF_STA, new_mac) at priority <= 250 under esphome: on_boot: Indeed, running arp -a confirms the successful change [Note: it can’t be run with higher priority since it requires WiFi to be enabled first]
I can also change Base MAC address by running esp_base_mac_addr_set(new_mac) at priority 1100 (highest possible I think).
However, HA still sees the device as having the old MAC address.
This can be verified by running avahi-browse -r -d local _esphomelib._tcp and looking at the mac= entry under the txt field.
So it seems that somehow mdns in ESPHome is setting the MAC address based on the factory one rather than the new address.
But I can’t see how to get mdns to use the new MAC address – whether by delaying DNS startup or by setting the MAC address even earlier.
So, is there any way to truly change the MAC address so that the mdns MAC address entry changes as well the actual WiFI MAC address?
OK that’s cool - that basically automatically does the same thing as the first solution I mentioned involving manually changing the MAC addresses and IP addresses by editing the relevant registry files – and to be sure your approach is simpler and less prone to error.
But I want to change the MAC address in the ESPHome device itself for several reasons:
I don’t want to have to change the MAC address filter in my router
I want to use the same assignment of IPs tied to MAC address
I can swap back and forth between alternative devices without having to change HA (in my case, I have 2 different displays that I want to switch between)
Of course since both devices will then have the same MAC and IP address, one needs to be careful no to use them at the same time.
So, is there any way to manually set the MAC address for ESPHome?
You solve 1 and 2 by a golden rule: always set static IP for all your devices (inside a device)! And that goes not only for esphome’s, but for all devices you intend to monitor/control from HA. HA doesn’t like changing IP’s . Not at all. And reserving IP in a router is not the correct way either - say that your router breaks or you decide to change your router - you instantly loose all reservations and you’re set with a tons of work (again).
Regarding 3: that’s why unique MAC is for: to be unique. Swapping two devices with same mac is again asking for problems. Each device must have it’s own unique mac. Period. Having two devices with sam mac is kinda not allowed in a network. It’s like we’d have two “puterboy” members here in the forum…
But the problem is that the mdns code seems to start right after the Wifi - while I need to change MAC address after WiFI starts but before mDNS starts
I need a hook to change the WiFi address before mdns starts…
I do set static ip’s for some of my devices but there are also advantages to dhcp so I don’t have to track all my devices.
Also, static ip’s won’t solve #1 since that is MAC address based and not IP based
Regarding 3, I am not using both devices at the same time so there is no conflict.
Also Espressif exposes calls to set the MAC precisely because such use cases exist.
Thank Clyde!
That is very similar to the problem I am trying to solve though I want to change the actual WiFi MAC address, not just the value passed via mDNS that serves as an HA identifier.
A simple solution would be to allow there to be an on_boot priority between when WiFi starts and when mDNS starts since:
Setting the MAC address requires WiFi be set up
The new MAC address needs to be set before the mDNS routine that grabs the MAC address identifier is launched
Even better would be to add a directive like:
wifi:
- mac: XX:XX:XX:XX:XX:XX
which would work on esp32 (but probably not esp8266) to run the following code on startup after WiFi setup and before mDNS
uint8_t custom_mac[6];
// Parse MAC string (format: XX:XX:XX:XX:XX:XX)
if (sscanf(mac_str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&custom_mac[0], &custom_mac[1], &custom_mac[2],
&custom_mac[3], &custom_mac[4], &custom_mac[5]) != 6) {
ESP_LOGE("mac", "Invalid Custom MAC format: %s", mac_str);
return;
}
esp_err_t ret = esp_wifi_set_mac(WIFI_IF_STA, custom_mac);
if (ret == ESP_OK) {
esp_wifi_get_mac(WIFI_IF_STA, current_mac);
ESP_LOGW("mac", "New MAC: %02X:%02X:%02X:%02X:%02X:%02X",
current_mac[0], current_mac[1], current_mac[2],
current_mac[3], current_mac[4], current_mac[5]);
} else {
ESP_LOGE("mac", "Failed to set Custom MAC: %d", ret);
}
No other code would need to be modified since any code run after that would see the new MAC address as the actual MAC address.