Skip to content

Commit 1a6250e

Browse files
authored
Merge pull request #65 from mash2k3/dev
Dev
2 parents 06f9426 + 8b16180 commit 1a6250e

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

custom_components/qingping_cgs1/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"iot_class": "local_push",
99
"issue_tracker": "https://github.com/mash2k3/qingping_cgs1/issues",
1010
"requirements": [],
11-
"version": "1.2.8"
11+
"version": "1.2.9"
1212
}

custom_components/qingping_cgs1/sensor.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ async def async_update_data():
317317
#sensors.append(QingpingDeviceSensor(coordinator, config_entry, mac, name, SENSOR_BATTERY, "Battery", PERCENTAGE, SensorDeviceClass.BATTERY, SensorStateClass.MEASUREMENT, device_info))
318318
if model in ["CGS1", "CGS2", "CGDN1", "CGP22C", "CGP22W", "CGP23W"]:
319319
sensors.append(battery_state)
320-
sensors.append(QingpingDeviceSensor(coordinator, config_entry, mac, name, SENSOR_BATTERY, "Battery", PERCENTAGE, SensorDeviceClass.BATTERY, SensorStateClass.MEASUREMENT, device_info))
320+
battery_sensor = QingpingDeviceSensor(coordinator, config_entry, mac, name, SENSOR_BATTERY, "Battery", PERCENTAGE, SensorDeviceClass.BATTERY, SensorStateClass.MEASUREMENT, device_info)
321+
battery_sensor._attr_entity_category = EntityCategory.DIAGNOSTIC
322+
sensors.append(battery_sensor)
321323
sensors.append(QingpingDeviceSensor(coordinator, config_entry, mac, name, SENSOR_TEMPERATURE, "Temperature", native_temp_unit, SensorDeviceClass.TEMPERATURE, SensorStateClass.MEASUREMENT, device_info))
322324
sensors.append(QingpingDeviceSensor(coordinator, config_entry, mac, name, SENSOR_HUMIDITY, "Humidity", PERCENTAGE, SensorDeviceClass.HUMIDITY, SensorStateClass.MEASUREMENT, device_info))
323325

@@ -975,6 +977,11 @@ def icon(self):
975977

976978
async def publish_config(self):
977979
"""Publish configuration message to MQTT."""
980+
# Check if entity has been added to hass yet
981+
if not self.hass:
982+
_LOGGER.debug(f"[{self._mac}] Sensor not yet added to hass, skipping config publish")
983+
return
984+
978985
update_interval = self.coordinator.data.get(CONF_UPDATE_INTERVAL, 15)
979986
topic = f"{MQTT_TOPIC_PREFIX}/{self._mac}/down"
980987

custom_components/qingping_cgs1/tlv_decoder.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def tlv_unpack(byte_array: bytes) -> dict[str, Any]:
7575
}
7676

7777

78-
def decode_th_data(byte_array: bytes) -> dict[str, Any]:
78+
def decode_th_data(byte_array: bytes, product_id: int = 0) -> dict[str, Any]:
7979
"""Decode temperature, humidity, pressure, and battery data."""
8080
if len(byte_array) < 6:
8181
_LOGGER.error("Byte array too short for TH data decoding")
@@ -84,19 +84,25 @@ def decode_th_data(byte_array: bytes) -> dict[str, Any]:
8484
th = bytes_to_int_little_endian(byte_array[0:3])
8585
temperature = ((th >> 12) - 500) / 10
8686
humidity = (th & 0xFFF) / 10
87-
pressure = bytes_to_int_little_endian(byte_array[3:5]) / 100.0 # Convert to kPa
87+
raw_3_5 = bytes_to_int_little_endian(byte_array[3:5])
8888
battery = byte_array[5]
8989

90-
return {
90+
out: dict[str, Any] = {
9191
"dataType": "data",
9292
"timestamp": 0,
9393
"time": "",
9494
"temperature": temperature,
9595
"humidity": humidity,
96-
"pressure": pressure,
9796
"battery": battery,
9897
}
9998

99+
if product_id == 51:
100+
out["co2"] = raw_3_5
101+
else:
102+
out["pressure"] = raw_3_5 / 100.0 # Convert to kPa
103+
104+
return out
105+
100106

101107
def decode_realtime_data(byte_array: bytes, product_id: int = 0) -> dict[str, Any]:
102108
"""Decode real-time sensor data."""
@@ -105,7 +111,7 @@ def decode_realtime_data(byte_array: bytes, product_id: int = 0) -> dict[str, An
105111
return {}
106112

107113
timestamp = bytes_to_int_little_endian(byte_array[0:4])
108-
realtime_data = decode_th_data(byte_array[4:])
114+
realtime_data = decode_th_data(byte_array[4:], product_id)
109115
rssi = byte_array[10]
110116
if rssi >= 128:
111117
rssi = rssi - 256
@@ -134,7 +140,7 @@ def decode_history_data(byte_array: bytes, product_id: int = 0) -> list[dict[str
134140

135141
while index + pack_len <= len(byte_array):
136142
history_pack = byte_array[index:index + pack_len]
137-
history_data = decode_th_data(history_pack)
143+
history_data = decode_th_data(history_pack, product_id)
138144
history_data["timestamp"] = timestamp + duration * i
139145
history_data["dataType"] = "data"
140146
history_data["time"] = fmt_timestamp(history_data["timestamp"])

0 commit comments

Comments
 (0)