| 1 |
""" |
| 2 |
Parser for Govee BLE advertisements. |
| 3 |
|
| 4 |
This file is shamelessly copied from the following repository: |
| 5 |
https://github.com/Ernst79/bleparser/blob/c42ae922e1abed2720c7fac993777e1bd59c0c93/package/bleparser/govee.py |
| 6 |
|
| 7 |
MIT License applies. |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
import logging |
| 13 |
import struct |
| 14 |
from dataclasses import dataclass |
| 15 |
from enum import Enum |
| 16 |
|
| 17 |
from bluetooth_data_tools import short_address |
| 18 |
from bluetooth_sensor_state_data import BluetoothData |
| 19 |
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 20 |
from home_assistant_bluetooth import BluetoothServiceInfo |
| 21 |
from sensor_state_data import BinarySensorDeviceClass, SensorLibrary |
| 22 |
|
| 23 |
_LOGGER = logging.getLogger(__name__) |
| 24 |
|
| 25 |
|
| 26 |
PACKED_hHB_LITTLE = struct.Struct("<hHB") |
| 27 |
PACKED_hHB = struct.Struct(">hHB") |
| 28 |
PACKED_hh = struct.Struct(">hh") |
| 29 |
PACKED_hhhchhh_LITTLE = struct.Struct("<hhhchhh") |
| 30 |
PACKED_H = struct.Struct(">H") |
| 31 |
|
| 32 |
PACKED_hhhh = struct.Struct(">hhhh") |
| 33 |
PACKED_hhbhh = struct.Struct(">hhbhh") |
| 34 |
PACKED_hhhhh = struct.Struct(">hhhhh") |
| 35 |
PACKED_hhhhhh = struct.Struct(">hhhhhh") |
| 36 |
|
| 37 |
|
| 38 |
ERROR = "error" |
| 39 |
|
| 40 |
MIN_TEMP = -40 |
| 41 |
MAX_TEMP = 100 |
| 42 |
|
| 43 |
NOT_GOVEE_MANUFACTURER = {76} |
| 44 |
|
| 45 |
PROBE_MAPPING_1_2 = [1, 2] |
| 46 |
PROBE_MAPPING_3_4 = [3, 4] |
| 47 |
|
| 48 |
FOUR_PROBES_MAPPING = dict.fromkeys( |
| 49 |
(1, 65, 129, 193), PROBE_MAPPING_1_2 |
| 50 |
) | dict.fromkeys((2, 66, 130, 194), PROBE_MAPPING_3_4) |
| 51 |
|
| 52 |
SIX_PROBES_MAPPING = { |
| 53 |
0: [[1, 0x01], [2, 0x02]], |
| 54 |
64: [[3, 0x04], [4, 0x08]], |
| 55 |
128: [[5, 0x10], [6, 0x20]], |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
def decode_temp_humid_battery_error(data: bytes) -> tuple[float, float, int, bool]: |
| 60 |
temp, humi = decode_temp_humid(data[0:3]) |
| 61 |
batt = int(data[-1] & 0x7F) |
| 62 |
err = bool(data[-1] & 0x80) |
| 63 |
return temp, humi, batt, err |
| 64 |
|
| 65 |
|
| 66 |
def decode_temp_humid(temp_humid_bytes: bytes) -> tuple[float, float]: |
| 67 |
"""Decode potential negative temperatures.""" |
| 68 |
base_num = ( |
| 69 |
(temp_humid_bytes[0] << 16) + (temp_humid_bytes[1] << 8) + temp_humid_bytes[2] |
| 70 |
) |
| 71 |
is_negative = base_num & 0x800000 |
| 72 |
temp_as_int = base_num & 0x7FFFFF |
| 73 |
temp_as_float = int(temp_as_int / 1000) / 10.0 |
| 74 |
if is_negative: |
| 75 |
temp_as_float = -temp_as_float |
| 76 |
humid = (temp_as_int % 1000) / 10.0 |
| 77 |
return temp_as_float, humid |
| 78 |
|
| 79 |
|
| 80 |
def decode_temps_probes(packet_value: int) -> float: |
| 81 |
"""Filter potential negative temperatures.""" |
| 82 |
if packet_value < 0: |
| 83 |
return 0.0 |
| 84 |
return float(packet_value / 100) |
| 85 |
|
| 86 |
|
| 87 |
def decode_temps_probes_none(packet_value: int) -> float | None: |
| 88 |
"""Filter potential negative temperatures.""" |
| 89 |
if packet_value < 0: |
| 90 |
return None |
| 91 |
return float(packet_value) |
| 92 |
|
| 93 |
|
| 94 |
def hex(data: bytes) -> str: |
| 95 |
"""Return a string object containing two hexadecimal digits for each byte in the instance.""" |
| 96 |
return "b'{}'".format("".join(f"\\x{b:02x}" for b in data)) |
| 97 |
|
| 98 |
|
| 99 |
def decode_temps_from_4_bytes(packet_value: int) -> float: |
| 100 |
"""Decode temperature values (to one decimal place).""" |
| 101 |
if packet_value & 0x80000000: |
| 102 |
# Handle freezing temperatures: the high bit flags a negative value; |
| 103 |
# the remaining bits pack the magnitude identically to a positive |
| 104 |
# reading (same as humidity/pm2.5, which mask 0x7FFFFFFF and divide by |
| 105 |
# 1_000_000 / 1_000). Negate the decoded magnitude. |
| 106 |
packet_value &= 0x7FFFFFFF |
| 107 |
return -float(int(packet_value / 1000000) / 10) |
| 108 |
return float(int(packet_value / 1000000) / 10) |
| 109 |
|
| 110 |
|
| 111 |
def decode_humi_from_4_bytes(packet_value: int) -> float: |
| 112 |
"""Decode humidity values (to one decimal place)""" |
| 113 |
packet_value &= 0x7FFFFFFF |
| 114 |
return float(int((packet_value % 1000000) / 1000) / 10) |
| 115 |
|
| 116 |
|
| 117 |
def decode_pm25_from_4_bytes(packet_value: int) -> int: |
| 118 |
"""Decode humidity values""" |
| 119 |
packet_value &= 0x7FFFFFFF |
| 120 |
return int(packet_value % 1000) |
| 121 |
|
| 122 |
|
| 123 |
def calculate_crc(data: bytes) -> int: |
| 124 |
crc = 0x1D0F |
| 125 |
for b in data: |
| 126 |
for s in range(7, -1, -1): |
| 127 |
mask = 0 |
| 128 |
if (crc >> 15) ^ (b >> s) & 1: |
| 129 |
mask = 0x1021 |
| 130 |
crc = ((crc << 1) ^ mask) & 0xFFFF |
| 131 |
return crc |
| 132 |
|
| 133 |
|
| 134 |
def decrypt_data(key: bytes, data: bytes) -> bytes: |
| 135 |
cipher = Cipher(algorithms.AES(key[::-1]), modes.ECB()) |
| 136 |
decryptor = cipher.decryptor() |
| 137 |
return (decryptor.update(data[::-1]) + decryptor.finalize())[::-1] |
| 138 |
|
| 139 |
|
| 140 |
class SensorType(Enum): |
| 141 |
THERMOMETER = "thermometer" |
| 142 |
BUTTON = "button" |
| 143 |
MOTION = "motion" |
| 144 |
WINDOW = "window" |
| 145 |
VIBRATION = "vibration" |
| 146 |
PRESENCE = "presence" |
| 147 |
PRESSURE = "pressure" |
| 148 |
|
| 149 |
|
| 150 |
@dataclass |
| 151 |
class ModelInfo: |
| 152 |
"""Model information for Govee sensors.""" |
| 153 |
|
| 154 |
model_id: str |
| 155 |
sensor_type: SensorType = SensorType.THERMOMETER |
| 156 |
button_count: int = 0 |
| 157 |
sleepy: bool = False |
| 158 |
# Payload lives only in the scan response, so the device must be actively |
| 159 |
# scanned long enough to capture one full broadcast cycle. |
| 160 |
requires_active_scan: bool = False |
| 161 |
|
| 162 |
|
| 163 |
_MODEL_DB = { |
| 164 |
"H5074": ModelInfo("H5074", requires_active_scan=True), |
| 165 |
"H5075": ModelInfo("H5075", requires_active_scan=True), |
| 166 |
"H5129": ModelInfo("H5129", requires_active_scan=True), |
| 167 |
"H5179": ModelInfo("H5179", requires_active_scan=True), |
| 168 |
"GV5179": ModelInfo("GV5179", requires_active_scan=True), |
| 169 |
"H5121": ModelInfo("H5121", sensor_type=SensorType.MOTION, sleepy=True), |
| 170 |
"H5122": ModelInfo( |
| 171 |
"H5122", sensor_type=SensorType.BUTTON, button_count=1, sleepy=True |
| 172 |
), |
| 173 |
"H5123": ModelInfo("H5123", sensor_type=SensorType.WINDOW, sleepy=True), |
| 174 |
"H5124": ModelInfo("H5124", sensor_type=SensorType.VIBRATION, sleepy=True), |
| 175 |
"H5125": ModelInfo( |
| 176 |
"H5125", sensor_type=SensorType.BUTTON, button_count=6, sleepy=True |
| 177 |
), |
| 178 |
"H5126": ModelInfo( |
| 179 |
"H5126", sensor_type=SensorType.BUTTON, button_count=2, sleepy=True |
| 180 |
), |
| 181 |
"H5127": ModelInfo("H5127", sensor_type=SensorType.PRESENCE, sleepy=True), |
| 182 |
"H5130": ModelInfo( |
| 183 |
"H5130", |
| 184 |
sensor_type=SensorType.PRESSURE, |
| 185 |
button_count=1, |
| 186 |
sleepy=True, |
| 187 |
requires_active_scan=True, |
| 188 |
), |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
def get_model_info(model_id: str) -> ModelInfo: |
| 193 |
"""Get model information for a Govee sensor.""" |
| 194 |
return _MODEL_DB.get(model_id, ModelInfo(model_id)) |
| 195 |
|
| 196 |
|
| 197 |
class GoveeBluetoothDeviceData(BluetoothData): |
| 198 |
"""Data for Govee BLE sensors.""" |
| 199 |
|
| 200 |
def _start_update(self, service_info: BluetoothServiceInfo) -> None: |
| 201 |
"""Update from BLE advertisement data.""" |
| 202 |
_LOGGER.debug("Parsing Govee BLE advertisement data: %s", service_info) |
| 203 |
manufacturer_data = service_info.manufacturer_data |
| 204 |
service_uuids = service_info.service_uuids |
| 205 |
local_name = service_info.name |
| 206 |
address = service_info.address |
| 207 |
self.set_device_manufacturer("Govee") |
| 208 |
|
| 209 |
if local_name.startswith("Govee_"): |
| 210 |
self.set_device_name(service_info.name[6:].replace("_", " ")) |
| 211 |
|
| 212 |
if local_name.startswith("GV"): |
| 213 |
self.set_device_name(service_info.name[2:].replace("_", " ")) |
| 214 |
self.set_precision(2) |
| 215 |
|
| 216 |
for mfr_id, mfr_data in manufacturer_data.items(): |
| 217 |
if mfr_id in NOT_GOVEE_MANUFACTURER: |
| 218 |
continue |
| 219 |
self._process_mfr_data(address, local_name, mfr_id, mfr_data, service_uuids) |
| 220 |
|
| 221 |
@property |
| 222 |
def device_type(self) -> str | None: |
| 223 |
"""Return the device type.""" |
| 224 |
primary_device_id = self.primary_device_id |
| 225 |
if device_type := self._device_id_to_type.get(primary_device_id): |
| 226 |
return device_type.partition("-")[0] |
| 227 |
return None |
| 228 |
|
| 229 |
@property |
| 230 |
def button_count(self) -> int: |
| 231 |
"""Return the number of buttons on the device.""" |
| 232 |
device_type = self.device_type |
| 233 |
assert device_type is not None |
| 234 |
return get_model_info(device_type).button_count |
| 235 |
|
| 236 |
@property |
| 237 |
def sensor_type(self) -> SensorType: |
| 238 |
"""Return the sensor type.""" |
| 239 |
device_type = self.device_type |
| 240 |
assert device_type is not None |
| 241 |
return get_model_info(device_type).sensor_type |
| 242 |
|
| 243 |
@property |
| 244 |
def sleepy(self) -> bool: |
| 245 |
"""Return if the device is sleep.""" |
| 246 |
device_type = self.device_type |
| 247 |
assert device_type is not None |
| 248 |
return get_model_info(device_type).sleepy |
| 249 |
|
| 250 |
@property |
| 251 |
def requires_active_scan(self) -> bool: |
| 252 |
"""Return if the device must be actively scanned to read its payload.""" |
| 253 |
device_type = self.device_type |
| 254 |
assert device_type is not None |
| 255 |
return get_model_info(device_type).requires_active_scan |
| 256 |
|
| 257 |
def _process_mfr_data( |
| 258 |
self, |
| 259 |
address: str, |
| 260 |
local_name: str, |
| 261 |
mgr_id: int, |
| 262 |
data: bytes, |
| 263 |
service_uuids: list[str], |
| 264 |
) -> None: |
| 265 |
"""Parser for Govee sensors.""" |
| 266 |
if debug_logging := _LOGGER.isEnabledFor(logging.DEBUG): |
| 267 |
_LOGGER.debug("Parsing Govee sensor: %s %s", mgr_id, hex(data)) |
| 268 |
msg_length = len(data) |
| 269 |
if msg_length > 25 and b"INTELLI_ROCKS" in data: |
| 270 |
# INTELLI_ROCKS sometimes ends up glued on to the end of the message |
| 271 |
data = data[:-25] |
| 272 |
msg_length = len(data) |
| 273 |
if debug_logging: |
| 274 |
_LOGGER.debug("Cleaned up packet: %s %s", mgr_id, data.hex()) |
| 275 |
|
| 276 |
if msg_length == 24: |
| 277 |
time_ms = data[2:6] |
| 278 |
enc_data = data[6:22] |
| 279 |
enc_crc = data[22:24] |
| 280 |
if not calculate_crc(enc_data) == int.from_bytes(enc_crc, "big"): |
| 281 |
_LOGGER.debug("CRC check failed for H512/3x: %s", hex(data)) |
| 282 |
return |
| 283 |
|
| 284 |
key = time_ms + bytes(12) |
| 285 |
try: |
| 286 |
decrypted = decrypt_data(key, enc_data) |
| 287 |
except ValueError: |
| 288 |
_LOGGER.warning("Failed to decrypt H512/3x: %s", hex(data)) |
| 289 |
return |
| 290 |
_LOGGER.debug("Decrypted H512/3x: %s - %s", decrypted, decrypted.hex()) |
| 291 |
model_id = decrypted[2] |
| 292 |
# GV5121 |
| 293 |
# 01040302640100000000000000000000 |
| 294 |
|
| 295 |
# GV5122 |
| 296 |
# 01050802640000000000000000000000 |
| 297 |
# 01050802640000000000000000000000 |
| 298 |
|
| 299 |
# GV5123 |
| 300 |
# 01050202640200000000000000000000 |
| 301 |
|
| 302 |
# GV5124 |
| 303 |
# 01030902640100000000000000000000 |
| 304 |
|
| 305 |
# GV5125 |
| 306 |
# 01010a02640000000000000000000000 |
| 307 |
|
| 308 |
# GV5126 |
| 309 |
# 01010b02640100000000000000000000 |
| 310 |
|
| 311 |
# GV5130 |
| 312 |
# 010e0d02640000000000000000000000 |
| 313 |
sensor_type = SensorType.BUTTON |
| 314 |
if "GV5121" in local_name or model_id == 3: |
| 315 |
self.set_device_type("H5121") |
| 316 |
self.set_device_name(f"5121{short_address(address)}") |
| 317 |
sensor_type = SensorType.MOTION |
| 318 |
elif "GV5122" in local_name or model_id == 8: |
| 319 |
self.set_device_type("H5122") |
| 320 |
self.set_device_name(f"5122{short_address(address)}") |
| 321 |
elif "GV5123" in local_name or model_id == 2: |
| 322 |
self.set_device_type("H5123") |
| 323 |
self.set_device_name(f"5123{short_address(address)}") |
| 324 |
sensor_type = SensorType.WINDOW |
| 325 |
elif "GV5124" in local_name or model_id == 9: |
| 326 |
self.set_device_type("H5124") |
| 327 |
self.set_device_name(f"5124{short_address(address)}") |
| 328 |
sensor_type = SensorType.VIBRATION |
| 329 |
elif "GV5125" in local_name or model_id == 10: |
| 330 |
self.set_device_type("H5125") |
| 331 |
self.set_device_name(f"5125{short_address(address)}") |
| 332 |
elif "GV5126" in local_name or model_id == 11: |
| 333 |
self.set_device_type("H5126") |
| 334 |
self.set_device_name(f"5126{short_address(address)}") |
| 335 |
elif "GV5130" in local_name or model_id == 13: |
| 336 |
self.set_device_type("H5130") |
| 337 |
self.set_device_name(f"5130{short_address(address)}") |
| 338 |
sensor_type = SensorType.PRESSURE |
| 339 |
else: |
| 340 |
return |
| 341 |
|
| 342 |
battery_percentage = decrypted[4] |
| 343 |
button_number_pressed = decrypted[5] |
| 344 |
self.update_predefined_sensor( |
| 345 |
SensorLibrary.BATTERY__PERCENTAGE, battery_percentage |
| 346 |
) |
| 347 |
if sensor_type is SensorType.WINDOW: |
| 348 |
# H5123 is a door/window sensor |
| 349 |
self.update_predefined_binary_sensor( |
| 350 |
BinarySensorDeviceClass.WINDOW, button_number_pressed == 2 |
| 351 |
) |
| 352 |
elif sensor_type is SensorType.VIBRATION: |
| 353 |
# H5124 is a vibration sensor |
| 354 |
if button_number_pressed == 1: |
| 355 |
self.fire_event("vibration", "vibration") |
| 356 |
elif sensor_type is SensorType.MOTION: |
| 357 |
if button_number_pressed == 1: |
| 358 |
self.fire_event("motion", "motion") |
| 359 |
elif sensor_type is SensorType.PRESSURE: |
| 360 |
# H5130 is a pressure sensor |
| 361 |
if button_number_pressed == 16: |
| 362 |
self.fire_event("button_0", "press") |
| 363 |
else: |
| 364 |
self.update_predefined_binary_sensor( |
| 365 |
BinarySensorDeviceClass.PRESENCE, button_number_pressed == 1 |
| 366 |
) |
| 367 |
else: |
| 368 |
self.fire_event(f"button_{button_number_pressed}", "press") |
| 369 |
return |
| 370 |
|
| 371 |
# Firmware 1.00.13 switched mgr_id 0x8803 -> 0x8843 and the payload |
| 372 |
# prefix ec000101... -> ec000201...; the present/motion byte offsets are |
| 373 |
# unchanged. See issue #264. |
| 374 |
# |
| 375 |
# 0x8803/0x8843 are not real company ids: Govee puts a flags byte right |
| 376 |
# after 0xFF, which BlueZ reads as the low byte of a little-endian |
| 377 |
# company id. 0x43 only means "broadcast v3, encrypted", which most of |
| 378 |
# the modern Govee lineup sets, so the payload prefix must be checked as |
| 379 |
# well or unrelated lights and plugs get published as presence sensors. |
| 380 |
# See issue #283. |
| 381 |
if ( |
| 382 |
msg_length == 6 |
| 383 |
and data.startswith((b"\xec\x00\x01\x01", b"\xec\x00\x02\x01")) |
| 384 |
and ("H5127" in local_name or mgr_id in (0x8803, 0x8843)) |
| 385 |
): |
| 386 |
self.set_device_type("H5127") |
| 387 |
self.set_device_name(f"H5127{short_address(address)}") |
| 388 |
present = data[4] == 1 |
| 389 |
motion = data[5] == 17 |
| 390 |
self.update_predefined_binary_sensor( |
| 391 |
BinarySensorDeviceClass.OCCUPANCY, present |
| 392 |
) |
| 393 |
self.update_predefined_binary_sensor(BinarySensorDeviceClass.MOTION, motion) |
| 394 |
return |
| 395 |
|
| 396 |
if msg_length == 6 and mgr_id in (0x8803, 0x8843) and debug_logging: |
| 397 |
_LOGGER.debug( |
| 398 |
"6 byte Govee packet with key 0x%04x not matched as H5127: %s %s", |
| 399 |
mgr_id, |
| 400 |
local_name, |
| 401 |
data.hex(), |
| 402 |
) |
| 403 |
|
| 404 |
if msg_length == 6 and ( |
| 405 |
(is_5072 := "H5072" in local_name) |
| 406 |
or (is_5075 := "H5075" in local_name) |
| 407 |
or (is_5129 := "H5129" in local_name) |
| 408 |
or mgr_id == 0xEC88 |
| 409 |
): |
| 410 |
if is_5072: |
| 411 |
self.set_device_type("H5072") |
| 412 |
elif is_5075: |
| 413 |
self.set_device_type("H5075") |
| 414 |
elif is_5129: |
| 415 |
self.set_device_type("H5129") |
| 416 |
else: |
| 417 |
self.set_device_type("H5072/H5075") |
| 418 |
temp, humi, batt, err = decode_temp_humid_battery_error(data[1:5]) |
| 419 |
if temp >= MIN_TEMP and temp <= MAX_TEMP and not err: |
| 420 |
self.update_predefined_sensor(SensorLibrary.TEMPERATURE__CELSIUS, temp) |
| 421 |
self.update_predefined_sensor(SensorLibrary.HUMIDITY__PERCENTAGE, humi) |
| 422 |
else: |
| 423 |
_LOGGER.debug( |
| 424 |
"Ignoring invalid sensor values, temperature: %.1f, humidity: %.1f, error: %s", |
| 425 |
temp, |
| 426 |
humi, |
| 427 |
err, |
| 428 |
) |
| 429 |
self.update_predefined_sensor(SensorLibrary.TEMPERATURE__CELSIUS, ERROR) |
| 430 |
self.update_predefined_sensor(SensorLibrary.HUMIDITY__PERCENTAGE, ERROR) |
| 431 |
self.update_predefined_sensor(SensorLibrary.BATTERY__PERCENTAGE, batt) |
| 432 |
return |
| 433 |
|
| 434 |
if msg_length == 8 and ("GV5112" in local_name or "H5112" in local_name): |
| 435 |
self.set_device_type("H5112") |
| 436 |
self.set_device_name(f"H5112 {short_address(address)}") |
| 437 |
|
| 438 |
# Last byte specifies the probe id: 41 for probe 1, 82 for probe 2 |
| 439 |
probe_id = 0 |
| 440 |
if data[7] == 0x41: |
| 441 |
probe_id = 1 |
| 442 |
elif data[7] == 0x82: |
| 443 |
probe_id = 2 |
| 444 |
else: |
| 445 |
_LOGGER.debug( |
| 446 |
"Unknown probe id: %s for a H5112, data: %s", |
| 447 |
data[7], |
| 448 |
hex(data), |
| 449 |
) |
| 450 |
return |
| 451 |
|
| 452 |
temp, humi, batt, err = decode_temp_humid_battery_error(data[2:6]) |
| 453 |
if MIN_TEMP <= temp <= MAX_TEMP and not err: |
| 454 |
self.update_temp_probe(temp, probe_id) |
| 455 |
if probe_id == 1: |
| 456 |
self.update_humidity_probe(humi, probe_id) |
| 457 |
else: |
| 458 |
_LOGGER.debug( |
| 459 |
"Ignoring invalid sensor values, probe: %d, temperature: %.1f, humidity: %.1f, error: %s", |
| 460 |
probe_id, |
| 461 |
temp, |
| 462 |
humi, |
| 463 |
err, |
| 464 |
) |
| 465 |
self.update_temp_probe(ERROR, probe_id) |
| 466 |
if probe_id == 1: |
| 467 |
self.update_humidity_probe(ERROR, probe_id) |
| 468 |
|
| 469 |
self.update_predefined_sensor(SensorLibrary.BATTERY__PERCENTAGE, batt) |
| 470 |
return |
| 471 |
|
| 472 |
if msg_length == 8 and "5140" in local_name: |
| 473 |
self.set_device_type("H5140") |
| 474 |
temp, humi = decode_temp_humid(data[2:5]) |
| 475 |
(co2,) = PACKED_H.unpack(data[5:7]) |
| 476 |
err = bool(data[7]) |
| 477 |
if MIN_TEMP <= temp <= MAX_TEMP and not err: |
| 478 |
self.update_predefined_sensor(SensorLibrary.TEMPERATURE__CELSIUS, temp) |
| 479 |
self.update_predefined_sensor(SensorLibrary.HUMIDITY__PERCENTAGE, humi) |
| 480 |
self.update_predefined_sensor( |
| 481 |
SensorLibrary.CO2__CONCENTRATION_PARTS_PER_MILLION, co2 |
| 482 |
) |
| 483 |
else: |
| 484 |
_LOGGER.debug( |
| 485 |
"Ignoring invalid sensor values, temperature: %.1f, humidity: %.1f, co2: %d, error: %s", |
| 486 |
temp, |
| 487 |
humi, |
| 488 |
co2, |
| 489 |
err, |
| 490 |
) |
| 491 |
self.update_predefined_sensor(SensorLibrary.TEMPERATURE__CELSIUS, ERROR) |
| 492 |
self.update_predefined_sensor(SensorLibrary.HUMIDITY__PERCENTAGE, ERROR) |
| 493 |
self.update_predefined_sensor( |
| 494 |
SensorLibrary.CO2__CONCENTRATION_PARTS_PER_MILLION, ERROR |
| 495 |
) |
| 496 |
return |
| 497 |
|
| 498 |
if msg_length in (6, 8) and ( |
| 499 |
(is_5108 := "H5108" in local_name) |
| 500 |
or (is_5100 := "H5100" in local_name) |
| 501 |
or (is_5101 := "H5101" in local_name) |
| 502 |
or (is_5102 := "H5102" in local_name) |
| 503 |
or (is_5103 := "H5103" in local_name) |
| 504 |
or (is_5104 := "H5104" in local_name) |
| 505 |
or (is_5105 := "H5105" in local_name) |
| 506 |
or (is_5174 := "H5174" in local_name) |
| 507 |
or (is_5177 := "H5177" in local_name) |
| 508 |
or (is_5110 := "H5110" in local_name) |
| 509 |
or (is_5179 := "GV5179" in local_name) |
| 510 |
or (mgr_id == 0x0001 and msg_length == 8) |
| 511 |
): |
| 512 |
if is_5108 or msg_length == 8: |
| 513 |
self.set_device_type("H5108") |
| 514 |
elif is_5100: |
| 515 |
self.set_device_type("H5100") |
| 516 |
elif is_5101: |
| 517 |
self.set_device_type("H5101") |
| 518 |
elif is_5102: |
| 519 |
self.set_device_type("H5102") |
| 520 |
elif is_5103: |
| 521 |
self.set_device_type("H5103") |
| 522 |
elif is_5104: |
| 523 |
self.set_device_type("H5104") |
| 524 |
elif is_5105: |
| 525 |
self.set_device_type("H5105") |
| 526 |
elif is_5174: |
| 527 |
self.set_device_type("H5174") |
| 528 |
elif is_5177: |
| 529 |
self.set_device_type("H5177") |
| 530 |
elif is_5110: |
| 531 |
self.set_device_type("H5110") |
| 532 |
elif is_5179: |
| 533 |
self.set_device_type("GV5179") |
| 534 |
else: |
| 535 |
self.set_device_type("H5101/H5102/H5104/H5108/H5174/H5177/GV5179") |
| 536 |
temp, humi, batt, err = decode_temp_humid_battery_error(data[2:6]) |
| 537 |
if temp >= MIN_TEMP and temp <= MAX_TEMP and not err: |
| 538 |
self.update_predefined_sensor(SensorLibrary.TEMPERATURE__CELSIUS, temp) |
| 539 |
self.update_predefined_sensor(SensorLibrary.HUMIDITY__PERCENTAGE, humi) |
| 540 |
else: |
| 541 |
_LOGGER.debug( |
| 542 |
"Ignoring invalid sensor values, temperature: %.1f, humidity: %.1f, error: %s", |
| 543 |
temp, |
| 544 |
humi, |
| 545 |
err, |
| 546 |
) |
| 547 |
self.update_predefined_sensor(SensorLibrary.TEMPERATURE__CELSIUS, ERROR) |
| 548 |
self.update_predefined_sensor(SensorLibrary.HUMIDITY__PERCENTAGE, ERROR) |
| 549 |
self.update_predefined_sensor(SensorLibrary.BATTERY__PERCENTAGE, batt) |
| 550 |
return |
| 551 |
|
| 552 |
if msg_length == 7 and ("H5074" in local_name or mgr_id == 0xEC88): |
| 553 |
self.set_device_type("H5074") |
| 554 |
(temp, humi, batt) = PACKED_hHB_LITTLE.unpack(data[1:6]) |
| 555 |
self.update_predefined_sensor( |
| 556 |
SensorLibrary.TEMPERATURE__CELSIUS, temp / 100 |
| 557 |
) |
| 558 |
self.update_predefined_sensor( |
| 559 |
SensorLibrary.HUMIDITY__PERCENTAGE, humi / 100 |
| 560 |
) |
| 561 |
self.update_predefined_sensor(SensorLibrary.BATTERY__PERCENTAGE, batt) |
| 562 |
return |
| 563 |
|
| 564 |
if msg_length == 9 and ( |
| 565 |
mgr_id == 0xEC88 |
| 566 |
or "H5051" in local_name |
| 567 |
or "H5052" in local_name |
| 568 |
or "H5071" in local_name |
| 569 |
): |
| 570 |
if "H5071" in local_name: |
| 571 |
self.set_device_type("H5071") |
| 572 |
elif "H5052" in local_name: |
| 573 |
self.set_device_type("H5052") |
| 574 |
else: |
| 575 |
self.set_device_type("H5051") |
| 576 |
self.set_device_name(f"H5051 {short_address(address)}") |
| 577 |
(temp, humi, batt) = PACKED_hHB_LITTLE.unpack(data[1:6]) |
| 578 |
self.update_predefined_sensor( |
| 579 |
SensorLibrary.TEMPERATURE__CELSIUS, temp / 100 |
| 580 |
) |
| 581 |
self.update_predefined_sensor( |
| 582 |
SensorLibrary.HUMIDITY__PERCENTAGE, humi / 100 |
| 583 |
) |
| 584 |
self.update_predefined_sensor(SensorLibrary.BATTERY__PERCENTAGE, batt) |
| 585 |
return |
| 586 |
|
| 587 |
if msg_length == 9 and ( |
| 588 |
"H5178" in local_name or "B5178" in local_name or mgr_id == 0x0001 |
| 589 |
): |
| 590 |
temp, humi, batt, err = decode_temp_humid_battery_error(data[3:7]) |
| 591 |
sensor_id = data[2] |
| 592 |
device_id = "primary" |
| 593 |
if local_name.startswith("H5178") or local_name.startswith("B5178"): |
| 594 |
self.set_title(local_name) |
| 595 |
else: |
| 596 |
self.set_title("H5178") |
| 597 |
if sensor_id == 0: |
| 598 |
self.set_device_name( |
| 599 |
f"{local_name} Primary".replace("_", " "), device_id |
| 600 |
) |
| 601 |
self.set_device_type("H5178", device_id) |
| 602 |
self.set_device_manufacturer("Govee", device_id) |
| 603 |
elif sensor_id == 1: |
| 604 |
device_id = "remote" |
| 605 |
self.set_device_name( |
| 606 |
f"{local_name} Remote".replace("_", " "), device_id |
| 607 |
) |
| 608 |
self.set_device_type("H5178-REMOTE", device_id) |
| 609 |
self.set_device_manufacturer("Govee", device_id) |
| 610 |
elif debug_logging: |
| 611 |
_LOGGER.debug( |
| 612 |
"Unknown sensor id for Govee H5178," |
| 613 |
" please report to the developers, data: %s", |
| 614 |
hex(data), |
| 615 |
) |
| 616 |
if temp >= MIN_TEMP and temp <= MAX_TEMP and not err: |
| 617 |
self.update_predefined_sensor( |
| 618 |
SensorLibrary.TEMPERATURE__CELSIUS, temp, device_id=device_id |
| 619 |
) |
| 620 |
self.update_predefined_sensor( |
| 621 |
SensorLibrary.HUMIDITY__PERCENTAGE, humi, device_id=device_id |
| 622 |
) |
| 623 |
else: |
| 624 |
_LOGGER.debug( |
| 625 |
"Ignoring invalid sensor values, temperature: %.1f, humidity: %.1f, error: %s", |
| 626 |
temp, |
| 627 |
humi, |
| 628 |
err, |
| 629 |
) |
| 630 |
self.update_predefined_sensor( |
| 631 |
SensorLibrary.TEMPERATURE__CELSIUS, ERROR, device_id=device_id |
| 632 |
) |
| 633 |
self.update_predefined_sensor( |
| 634 |
SensorLibrary.HUMIDITY__PERCENTAGE, ERROR, device_id=device_id |
| 635 |
) |
| 636 |
self.update_predefined_sensor( |
| 637 |
SensorLibrary.BATTERY__PERCENTAGE, batt, device_id=device_id |
| 638 |
) |
| 639 |
return |
| 640 |
|
| 641 |
if msg_length == 9 and ("H5179" in local_name or mgr_id == 0x8801): |
| 642 |
self.set_device_type("H5179") |
| 643 |
temp, humi, batt = PACKED_hHB_LITTLE.unpack(data[4:9]) |
| 644 |
self.update_predefined_sensor( |
| 645 |
SensorLibrary.TEMPERATURE__CELSIUS, temp / 100 |
| 646 |
) |
| 647 |
self.update_predefined_sensor( |
| 648 |
SensorLibrary.HUMIDITY__PERCENTAGE, humi / 100 |
| 649 |
) |
| 650 |
self.update_predefined_sensor(SensorLibrary.BATTERY__PERCENTAGE, batt) |
| 651 |
return |
| 652 |
|
| 653 |
if msg_length == 14 and ( |
| 654 |
"H5181" in local_name |
| 655 |
or mgr_id in {0xF861, 0x388A, 0xEA42, 0xAAA2, 0xD14B} |
| 656 |
or "00008151-0000-1000-8000-00805f9b34fb" in service_uuids |
| 657 |
): |
| 658 |
self.set_device_type("H5181") |
| 659 |
self.set_device_name(f"H5181 {short_address(address)}") |
| 660 |
(temp_probe_1, temp_alarm_1) = PACKED_hh.unpack(data[8:12]) |
| 661 |
self.update_temp_probe_with_alarm( |
| 662 |
decode_temps_probes(temp_probe_1), decode_temps_probes(temp_alarm_1), 1 |
| 663 |
) |
| 664 |
return |
| 665 |
|
| 666 |
if msg_length == 17 and ( |
| 667 |
"H5182" in local_name |
| 668 |
or mgr_id == 0x2730 |
| 669 |
or "00008251-0000-1000-8000-00805f9b34fb" in service_uuids |
| 670 |
): |
| 671 |
self.set_device_type("H5182") |
| 672 |
self.set_device_name(f"H5182 {short_address(address)}") |
| 673 |
( |
| 674 |
temp_probe_1, |
| 675 |
temp_alarm_1, |
| 676 |
_, |
| 677 |
temp_probe_2, |
| 678 |
temp_alarm_2, |
| 679 |
) = PACKED_hhbhh.unpack(data[8:17]) |
| 680 |
self.update_temp_probe_with_alarm( |
| 681 |
decode_temps_probes(temp_probe_1), decode_temps_probes(temp_alarm_1), 1 |
| 682 |
) |
| 683 |
self.update_temp_probe_with_alarm( |
| 684 |
decode_temps_probes(temp_probe_2), decode_temps_probes(temp_alarm_2), 2 |
| 685 |
) |
| 686 |
return |
| 687 |
|
| 688 |
if msg_length == 14 and ( |
| 689 |
"H5183" in local_name |
| 690 |
or mgr_id in {0x67DD, 0xE02F, 0xF79F} |
| 691 |
or "00008351-0000-1000-8000-00805f9b34fb" in service_uuids |
| 692 |
): |
| 693 |
self.set_device_type("H5183") |
| 694 |
self.set_device_name(f"H5183 {short_address(address)}") |
| 695 |
(temp_probe_1, temp_alarm_1) = PACKED_hh.unpack(data[8:12]) |
| 696 |
self.update_temp_probe_with_alarm( |
| 697 |
decode_temps_probes(temp_probe_1), decode_temps_probes(temp_alarm_1), 1 |
| 698 |
) |
| 699 |
return |
| 700 |
|
| 701 |
if msg_length == 17 and ( |
| 702 |
"H5184" in local_name |
| 703 |
or mgr_id == 0x1B36 |
| 704 |
or "00008451-0000-1000-8000-00805f9b34fb" in service_uuids |
| 705 |
): |
| 706 |
sensor_id = data[6] |
| 707 |
self.set_device_type("H5184") |
| 708 |
self.set_device_name(f"H5184 {short_address(address)}") |
| 709 |
( |
| 710 |
temp_probe_first, |
| 711 |
temp_alarm_first, |
| 712 |
_, |
| 713 |
temp_probe_second, |
| 714 |
temp_alarm_second, |
| 715 |
) = PACKED_hhbhh.unpack(data[8:17]) |
| 716 |
if not (ids := FOUR_PROBES_MAPPING.get(sensor_id)): |
| 717 |
if debug_logging: |
| 718 |
_LOGGER.debug( |
| 719 |
"Unknown sensor id: %s for a H5184, data: %s", |
| 720 |
sensor_id, |
| 721 |
hex(data), |
| 722 |
) |
| 723 |
return |
| 724 |
self.update_temp_probe_with_alarm( |
| 725 |
decode_temps_probes(temp_probe_first), |
| 726 |
decode_temps_probes(temp_alarm_first), |
| 727 |
ids[0], |
| 728 |
) |
| 729 |
self.update_temp_probe_with_alarm( |
| 730 |
decode_temps_probes(temp_probe_second), |
| 731 |
decode_temps_probes(temp_alarm_second), |
| 732 |
ids[1], |
| 733 |
) |
| 734 |
return |
| 735 |
|
| 736 |
if msg_length == 20 and ( |
| 737 |
"H5185" in local_name |
| 738 |
or mgr_id in (0x4A32, 0x332, 0x4C32) |
| 739 |
or "00008551-0000-1000-8000-00805f9b34fb" in service_uuids |
| 740 |
): |
| 741 |
self.set_device_type("H5185") |
| 742 |
self.set_device_name(f"H5185 {short_address(address)}") |
| 743 |
( |
| 744 |
temp_probe_1, |
| 745 |
temp_alarm_1, |
| 746 |
_, |
| 747 |
temp_probe_2, |
| 748 |
temp_alarm_2, |
| 749 |
) = PACKED_hhhhh.unpack(data[8:18]) |
| 750 |
self.update_temp_probe_with_alarm( |
| 751 |
decode_temps_probes(temp_probe_1), decode_temps_probes(temp_alarm_1), 1 |
| 752 |
) |
| 753 |
self.update_temp_probe_with_alarm( |
| 754 |
decode_temps_probes(temp_probe_2), decode_temps_probes(temp_alarm_2), 2 |
| 755 |
) |
| 756 |
return |
| 757 |
|
| 758 |
if msg_length == 20 and ( |
| 759 |
"H5191" in local_name |
| 760 |
or mgr_id == 0xAC63 |
| 761 |
or "00009151-0000-1000-8000-00805f9b34fb" in service_uuids |
| 762 |
): |
| 763 |
self.set_device_type("H5191") |
| 764 |
self.set_device_name(f"H5191 {short_address(address)}") |
| 765 |
( |
| 766 |
temp_probe_1, |
| 767 |
temp_alarm_1, |
| 768 |
_, |
| 769 |
temp, |
| 770 |
) = PACKED_hhhh.unpack(data[8:16]) |
| 771 |
self.update_temp_probe_with_alarm( |
| 772 |
decode_temps_probes(temp_probe_1), decode_temps_probes(temp_alarm_1), 1 |
| 773 |
) |
| 774 |
self.update_predefined_sensor( |
| 775 |
SensorLibrary.TEMPERATURE__CELSIUS, temp / 100 |
| 776 |
) |
| 777 |
return |
| 778 |
|
| 779 |
if msg_length == 20 and ( |
| 780 |
"H5198" in local_name |
| 781 |
or mgr_id == 0x3022 |
| 782 |
or "00009851-0000-1000-8000-00805f9b34fb" in service_uuids |
| 783 |
): |
| 784 |
sensor_id = data[6] |
| 785 |
self.set_device_type("H5198") |
| 786 |
self.set_device_name(f"H5198 {short_address(address)}") |
| 787 |
( |
| 788 |
temp_probe_first, |
| 789 |
temp_alarm_first, |
| 790 |
low_temp_alarm_first, |
| 791 |
temp_probe_second, |
| 792 |
temp_alarm_second, |
| 793 |
low_temp_alarm_second, |
| 794 |
) = PACKED_hhhhhh.unpack(data[8:20]) |
| 795 |
if not (ids := FOUR_PROBES_MAPPING.get(sensor_id)): |
| 796 |
if debug_logging: |
| 797 |
_LOGGER.debug( |
| 798 |
"Unknown sensor id: %s for a H5198, data: %s", |
| 799 |
sensor_id, |
| 800 |
hex(data), |
| 801 |
) |
| 802 |
return |
| 803 |
self.update_temp_probe_with_alarm( |
| 804 |
decode_temps_probes(temp_probe_first), |
| 805 |
decode_temps_probes(temp_alarm_first), |
| 806 |
ids[0], |
| 807 |
decode_temps_probes(low_temp_alarm_first), |
| 808 |
) |
| 809 |
self.update_temp_probe_with_alarm( |
| 810 |
decode_temps_probes(temp_probe_second), |
| 811 |
decode_temps_probes(temp_alarm_second), |
| 812 |
ids[1], |
| 813 |
decode_temps_probes(low_temp_alarm_second), |
| 814 |
) |
| 815 |
return |
| 816 |
|
| 817 |
if msg_length == 20 and "00005550-0000-1000-8000-00805f9b34fb" in service_uuids: |
| 818 |
self.set_device_type("H5055") |
| 819 |
self.set_device_name(f"H5055 {short_address(address)}") |
| 820 |
( |
| 821 |
temp_probe_first, |
| 822 |
temp_min_first, |
| 823 |
temp_max_first, |
| 824 |
_, |
| 825 |
temp_probe_second, |
| 826 |
temp_min_second, |
| 827 |
temp_max_second, |
| 828 |
) = PACKED_hhhchhh_LITTLE.unpack(data[5:18]) |
| 829 |
sensor_ids = data[3] |
| 830 |
if not (pids := SIX_PROBES_MAPPING.get(sensor_ids & 0xC0)): |
| 831 |
if debug_logging: |
| 832 |
_LOGGER.debug( |
| 833 |
"Unknown sensor id: %s for a H5055, data: %s", |
| 834 |
sensor_ids, |
| 835 |
hex(data), |
| 836 |
) |
| 837 |
return |
| 838 |
if sensor_ids & pids[0][1]: |
| 839 |
self.update_temp_probe_with_alarm( |
| 840 |
temp_probe_first, |
| 841 |
decode_temps_probes_none(temp_max_first), |
| 842 |
pids[0][0], |
| 843 |
decode_temps_probes_none(temp_min_first), |
| 844 |
) |
| 845 |
if sensor_ids & pids[1][1]: |
| 846 |
self.update_temp_probe_with_alarm( |
| 847 |
temp_probe_second, |
| 848 |
decode_temps_probes_none(temp_max_second), |
| 849 |
pids[1][0], |
| 850 |
decode_temps_probes_none(temp_min_second), |
| 851 |
) |
| 852 |
batt = int(data[2] & 0x7F) |
| 853 |
self.update_predefined_sensor(SensorLibrary.BATTERY__PERCENTAGE, batt) |
| 854 |
return |
| 855 |
|
| 856 |
if msg_length == 6 and "H5106" in local_name: |
| 857 |
self.set_device_type("H5106") |
| 858 |
self.set_device_name(f"H5106 {short_address(address)}") |
| 859 |
packet_5106 = data[2:6].hex() |
| 860 |
four_bytes = int(packet_5106, 16) |
| 861 |
temp = decode_temps_from_4_bytes(four_bytes) |
| 862 |
humi = decode_humi_from_4_bytes(four_bytes) |
| 863 |
pm25 = decode_pm25_from_4_bytes(four_bytes) |
| 864 |
self.update_predefined_sensor(SensorLibrary.TEMPERATURE__CELSIUS, temp) |
| 865 |
self.update_predefined_sensor(SensorLibrary.HUMIDITY__PERCENTAGE, humi) |
| 866 |
self.update_predefined_sensor( |
| 867 |
SensorLibrary.PM25__CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, pm25 |
| 868 |
) |
| 869 |
return |
| 870 |
|
| 871 |
def update_temp_probe(self, temp: float | str, probe_id: int) -> None: |
| 872 |
"""Update the temperature probe.""" |
| 873 |
self.update_predefined_sensor( |
| 874 |
SensorLibrary.TEMPERATURE__CELSIUS, |
| 875 |
temp, |
| 876 |
key=f"temperature_probe_{probe_id}", |
| 877 |
name=f"Temperature Probe {probe_id}", |
| 878 |
) |
| 879 |
|
| 880 |
def update_humidity_probe(self, humi: float | str, probe_id: int) -> None: |
| 881 |
"""Update the humidity probe.""" |
| 882 |
self.update_predefined_sensor( |
| 883 |
SensorLibrary.HUMIDITY__PERCENTAGE, |
| 884 |
humi, |
| 885 |
key=f"humidity_probe_{probe_id}", |
| 886 |
name=f"Humidity Probe {probe_id}", |
| 887 |
) |
| 888 |
|
| 889 |
def update_temp_probe_with_alarm( |
| 890 |
self, |
| 891 |
temp: float, |
| 892 |
alarm_temp: float | None, |
| 893 |
probe_id: int, |
| 894 |
low_alarm_temp: float | None = None, |
| 895 |
) -> None: |
| 896 |
"""Update the temperature probe with the alarm temperature.""" |
| 897 |
self.update_temp_probe(temp, probe_id) |
| 898 |
if alarm_temp is not None: |
| 899 |
self.update_predefined_sensor( |
| 900 |
SensorLibrary.TEMPERATURE__CELSIUS, |
| 901 |
alarm_temp, |
| 902 |
key=f"temperature_alarm_probe_{probe_id}", |
| 903 |
name=f"Temperature Alarm Probe {probe_id}", |
| 904 |
) |
| 905 |
if low_alarm_temp is not None: |
| 906 |
self.update_predefined_sensor( |
| 907 |
SensorLibrary.TEMPERATURE__CELSIUS, |
| 908 |
low_alarm_temp, |
| 909 |
key=f"low_temperature_alarm_probe_{probe_id}", |
| 910 |
name=f"Low Temperature Alarm Probe {probe_id}", |
| 911 |
) |