iOS/Sources/Shared/Common/Extensions/String+HA.swift
Copilot 9e8df8ad9d
Format BSSID MAC addresses with leading zeros for consistency with Android (#4095)
## Summary
BSSID sensor reported MAC addresses without leading zeros (e.g.,
`18:e8:29:a7:e9:b`), inconsistent with Android companion app and
standard MAC address notation. This breaks cross-platform room presence
tracking.

**Changes:**
- Added `String.formattedBSSID` extension that pads hex octets to 2
characters
- Applied formatting to BSSID sensor state in `ConnectivitySensor`
- Added test coverage for formatting edge cases

```swift
// Before: "18:e8:29:a7:e9:b"
// After:  "18:e8:29:a7:e9:0b"
sensor.State = bssid.formattedBSSID
```

## Screenshots
N/A - Sensor value formatting change only

## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#

## Any other notes
**Breaking change:** Users with BSSID-based automations must update to
the new zero-padded format. This aligns iOS with Android, standard MAC
notation, and network management tools like UniFi.

<!-- START COPILOT ORIGINAL PROMPT -->



<details>

<summary>Original prompt</summary>

> 
> ----
> 
> *This section details on the original issue you should resolve*
> 
> <issue_title>BSSID mac address format isn't consistant with Android
Companion app</issue_title>
> <issue_description>**iOS device model, version and app version**
> <!-- Please include your device 'Model Name' and 'Software Version' as
listed in iOS Settings>General>About. Please also give the app version
listed beneath "Home Assistant Companion" in the App Configuration>About
menu within the app, please include the number in brackets -->
> 
> Model Name: IPhone 6S
> Software Version: IOS 14.4.1
> App version: 2021.77
> 
> **Home Assistant Core Version**
> version | core-2021.3.4
> -- | --
> installation_type | Home Assistant Container
> dev | false
> hassio | false
> docker | true
> virtualenv | false
> python_version | 3.8.7
> os_name | Linux
> os_version | 5.3.18-3-pve
> arch | x86_64
> timezone | Europe/Paris
> 
> <details><summary>Home Assistant Community Store</summary>
> 
> GitHub API | ok
> -- | --
> Github API Calls Remaining | 4998
> Installed Version | 1.11.0
> Stage | running
> Available Repositories | 850
> Installed Repositories | 8
> 
> </details>
> 
> <details><summary>Home Assistant Cloud</summary>
> 
> logged_in | false
> -- | --
> can_reach_cert_server | ok
> can_reach_cloud_auth | ok
> can_reach_cloud | ok
> 
> </details>
> 
> <details><summary>Lovelace</summary>
> 
> dashboards | 9
> -- | --
> resources | 6
> views | 31
> mode | storage
> 
> </details>
> 
> **Describe the bug**
> The BSSID sensor reports access point mac address with leading zeros
omitted
> example:
> 
> ```
> 18:e8:29:a7:e9:b
> ```
> The last number is 0x0b
> 
> An adroid device connected the the same access point returns 
> 
> ```
> 18:e8:29:a7:e9:0b
> ```
> **To Reproduce**
> You need to have an access point with mac bytes lower then 0x10
> 
> **Expected behavior**
> Having same BSSID representation than reported by Android App
companion
> 
> **Screenshots**
> Iphone:
>
![image](https://user-images.githubusercontent.com/5980377/113318644-f3901d00-9310-11eb-94a8-63e53675946d.png)
> 
> Android phone
>
![image](https://user-images.githubusercontent.com/5980377/113318689-00ad0c00-9311-11eb-8594-8daa837dbb2a.png)
> 
> 
> **Additional context**
> I'm using BSSID to track room presence based on connected AP. I'm
trying to share BSSID --> access point for all household mobile.
> 
> I'd prefer the version with leading 0 witch is aligned with Unify
management tool.
> 
> </issue_description>
> 
> ## Comments on the Issue (you are @copilot in this section)
> 
> <comments>
> <comment_new><author>@zacwest</author><body>
> We're just slurping the value we read from the system, so we'd need to
sanitize the input to change it. It would also be a breaking change for
modifying what values are sent up, which is less-than-ideal since users
get auto-updated in the apps. I'm not sure there's a great path forward
for resolving this.</body></comment_new>
> </comments>
> 


</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes home-assistant/iOS#1563

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
2025-12-16 12:38:06 +00:00

50 lines
1.4 KiB
Swift

import CoreGraphics
import Foundation
import UIKit
public extension String {
var djb2hash: Int {
unicodeScalars.map(\.value).reduce(5381) { ($0 << 5) &+ $0 &+ Int($1) }
}
var containsJinjaTemplate: Bool {
contains("{{") || contains("{%") || contains("{#")
}
/// Capitalizes the first character of the string.
var capitalizedFirst: String {
guard let first else {
return self
}
return first.uppercased() + dropFirst()
}
var leadingCapitalized: String {
guard let first else {
return self
}
return first.uppercased() + dropFirst()
}
/// Formats a BSSID MAC address with proper zero-padding for each octet.
/// Converts "18:e8:29:a7:e9:b" to "18:e8:29:a7:e9:0b"
var formattedBSSID: String {
let components = split(separator: ":")
guard components.count == 6 else {
// Not a valid MAC address format, return as-is
return self
}
return components.map { component -> String in
// Pad each component to 2 characters with leading zeros
// Components should be 1 or 2 characters for valid MAC addresses
component.count == 1 ? "0" + component : String(component)
}.joined(separator: ":")
}
}
public extension String? {
var orEmpty: String {
self ?? ""
}
}