Skip to content
This repository has been archived by the owner on Feb 11, 2023. It is now read-only.

Commit

Permalink
enhancement: handle more mac address input formats
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab committed Feb 8, 2020
1 parent 4d62965 commit 97b86c0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Here is a sample config (you can view [config.sample.json](./config.sample.json)
"name": "Bathroom scale",
"email": "[email protected]",
"password": "myWithingsPassword",
"mac": "0ab2c3d4e5f6", // Do not add separators
"mac": "0a:b2:c3:d4:e5:f6",
"levels": [
350, // Excellent
1000, // Good
Expand Down
2 changes: 1 addition & 1 deletion config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"name": "Bathroom scale",
"email": "[email protected]",
"password": "myWithingsPassword",
"mac": "0ab2c3d4e5f6",
"mac": "0a:b2:c3:d4:e5:f6",
"levels": [350, 1000, 2500, 5000]
}
]
Expand Down
7 changes: 4 additions & 3 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
},
"mac": {
"title": "MAC address",
"description": "The MAC address of your smart scale. It can be found where the batteries are located. Please enter this info without any separators.",
"description": "The MAC address of your smart scale. It can be found where the batteries are located.",
"type": "string",
"pattern": "^[a-fA-F0-9]{12}$",
"maxLength": 12,
"placeholder": "0a:b2:c3:d4:e5:f6",
"pattern": "^((\\w{2})[:-]?){6}$",
"maxLength": 17,
"required": true
},
"levels": {
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class WithingsScale {
private readonly log: any,
private readonly config: Config,
) {
this.api = new WithingsApi(config.email, config.password, config.mac);
this.api = new WithingsApi(config.email, config.password, this.formattedMacAddress);
this.api.on('error', ({ message, error }) => this.log.warn(message, '/', error.message || error));

if (!config.levels || config.levels.length !== DEFAULT_AIR_QUALITY_LEVELS.length - 1) {
Expand Down Expand Up @@ -88,7 +88,7 @@ class WithingsScale {
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, 'Withings')
.setCharacteristic(Characteristic.Model, 'Smart Body Analyzer')
.setCharacteristic(Characteristic.SerialNumber, this.config.mac.replace(/(..)/g, ':$1').slice(1).toUpperCase())
.setCharacteristic(Characteristic.SerialNumber, this.formattedMacAddress)
.setCharacteristic(Characteristic.FirmwareRevision, pkg.version)
;

Expand All @@ -105,6 +105,10 @@ class WithingsScale {
callback();
}

private get formattedMacAddress() {
return this.config.mac.replace(/\W/g, '').match(/\w{2}/g).join(':').toUpperCase();
}

private async initServiceEvents(
type: DataType,
characteristic: Hap.Characteristic,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class WithingsApi {
const data = JSON.parse(query.text) as ApiResponse;
if (data.status === 0) {
this.device = (data as DeviceApiResponse).body.associations
.find(({ deviceproperties }) => deviceproperties.macaddress.replace(/:/g, '') === this.mac)
.find(({ deviceproperties }) => deviceproperties.macaddress.toUpperCase() === this.mac)
;

if (this.device) {
Expand Down

0 comments on commit 97b86c0

Please sign in to comment.