Forráskód Böngészése

DRY: add helper method to generate hex string for device ID

Christopher Mullins 6 éve
szülő
commit
0aefdb9839

+ 5 - 4
lib/MQTT/HomeAssistantDiscoveryClient.cpp

@@ -122,8 +122,8 @@ String HomeAssistantDiscoveryClient::buildTopic(const BulbId& bulbId) {
   // make the object ID based on the actual parameters rather than the alias.
   topic += "/";
   topic += MiLightRemoteTypeHelpers::remoteTypeToString(bulbId.deviceType);
-  topic += "_0x";
-  topic += String(bulbId.deviceId, HEX);
+  topic += "_";
+  topic += bulbId.getHexDeviceId();
   topic += "_";
   topic += bulbId.groupId;
   topic += "/config";
@@ -133,10 +133,11 @@ String HomeAssistantDiscoveryClient::buildTopic(const BulbId& bulbId) {
 
 String HomeAssistantDiscoveryClient::bindTopicVariables(const String& topic, const char* alias, const BulbId& bulbId) {
   String boundTopic = topic;
+  String hexDeviceId = bulbId.getHexDeviceId();
 
   boundTopic.replace(":device_alias", alias);
-  boundTopic.replace(":device_id", String("0x") + String(bulbId.deviceId, HEX));
-  boundTopic.replace(":hex_device_id", String("0x") + String(bulbId.deviceId, HEX));
+  boundTopic.replace(":device_id", hexDeviceId);
+  boundTopic.replace(":hex_device_id", hexDeviceId);
   boundTopic.replace(":dec_device_id", String(bulbId.deviceId));
   boundTopic.replace(":device_type", MiLightRemoteTypeHelpers::remoteTypeToString(bulbId.deviceType));
   boundTopic.replace(":group_id", String(bulbId.groupId));

+ 6 - 0
lib/Types/BulbId.cpp

@@ -38,4 +38,10 @@ bool BulbId::operator==(const BulbId &other) {
 uint32_t BulbId::getCompactId() const {
   uint32_t id = (deviceId << 24) | (deviceType << 8) | groupId;
   return id;
+}
+
+String BulbId::getHexDeviceId() const {
+  char hexDeviceId[7];
+  sprintf_P(hexDeviceId, PSTR("0x%X"), deviceId);
+  return hexDeviceId;
 }

+ 2 - 0
lib/Types/BulbId.h

@@ -13,5 +13,7 @@ struct BulbId {
   BulbId(const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType);
   bool operator==(const BulbId& other);
   void operator=(const BulbId& other);
+
   uint32_t getCompactId() const;
+  String getHexDeviceId() const;
 };