Skip to content

Commit

Permalink
rewrite yeelight dimmer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 committed Sep 23, 2023
1 parent 886aa69 commit fa6ead3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
20 changes: 10 additions & 10 deletions custom_components/ble_monitor/ble_parser/xiaomi.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,10 @@ def obj1001(xobj, device_type):
elif button_type == 8:
bathroom_remote_command = "heat"

# press type and dimmer
# press type and dimmer steps
button_press_type = "no press"
btn_switch_press_type = "no press"
dimmer = None
steps = None

if press == 0:
button_press_type = "single press"
Expand All @@ -430,24 +430,24 @@ def obj1001(xobj, device_type):
elif press == 3:
if button_type == 0:
button_press_type = "short press"
dimmer = value
steps = value
if button_type == 1:
button_press_type = "long press"
dimmer = value
steps = value
elif press == 4:
if button_type == 0:
if value <= 127:
button_press_type = "rotate right"
dimmer = value
steps = value
else:
button_press_type = "rotate left"
dimmer = 256 - value
steps = 256 - value
elif button_type <= 127:
button_press_type = "rotate right (pressed)"
dimmer = button_type
steps = button_type
else:
button_press_type = "rotate left (pressed)"
dimmer = 256 - button_type
steps = 256 - button_type
elif press == 5:
button_press_type = "short press"
elif press == 6:
Expand Down Expand Up @@ -477,8 +477,8 @@ def obj1001(xobj, device_type):
result["bathroom heater remote"] = bathroom_remote_command
result["button"] = button_press_type
elif device_type == "YLKG07YL/YLKG08YL":
result["dimmer"] = dimmer
result["button"] = button_press_type
result["steps"] = steps
result["dimmer"] = button_press_type
elif device_type == "K9B-1BTN":
result["button switch"] = btn_switch_press_type
result["one btn switch"] = one_btn_switch
Expand Down
8 changes: 4 additions & 4 deletions custom_components/ble_monitor/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,22 +1024,22 @@ class DimmerSensor(InstantUpdateSensor):
def __init__(self, config, key, devtype, firmware, entity_description, manufacturer=None):
"""Initialize the sensor."""
super().__init__(config, key, devtype, firmware, entity_description, manufacturer)
self._button = "button"
self._dimmer = self.entity_description.key
self._steps = "steps"

def collect(self, data, period_cnt, batt_attr=None):
"""Measurements collector."""
if self.enabled is False:
self.pending_update = False
return
self._state = data[self._button] + " " + str(data[self._dimmer]) + " steps"
self._state = data[self._dimmer] + " " + str(data[self._steps]) + " steps"
self._extra_state_attributes["last_packet_id"] = data["packet"]
self._extra_state_attributes["firmware"] = data["firmware"]
self._extra_state_attributes['mac_address' if self.is_beacon else 'uuid'] = dict_get_or_normalize(
data, CONF_MAC, CONF_UUID
)
self._extra_state_attributes["dimmer_value"] = data[self._dimmer]
self._extra_state_attributes["last_type_of_press"] = data[self._button]
self._extra_state_attributes["dimmer_value"] = data[self._steps]
self._extra_state_attributes["last_type_of_press"] = data[self._dimmer]
if batt_attr is not None:
self._extra_state_attributes[ATTR_BATTERY_LEVEL] = batt_attr
self.pending_update = True
Expand Down
8 changes: 4 additions & 4 deletions custom_components/ble_monitor/test/test_xiaomi_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ def test_Xiaomi_YLKG07YL_press(self):
assert sensor_msg["mac"] == "F82441C5988B"
assert sensor_msg["packet"] == 210
assert sensor_msg["data"]
assert sensor_msg["dimmer"] == 1
assert sensor_msg["button"] == "short press"
assert sensor_msg["steps"] == 1
assert sensor_msg["dimmer"] == "short press"
assert sensor_msg["rssi"] == -34

def test_Xiaomi_YLKG07YL_rotate(self):
Expand All @@ -568,8 +568,8 @@ def test_Xiaomi_YLKG07YL_rotate(self):
assert sensor_msg["mac"] == "F82441C5988B"
assert sensor_msg["packet"] == 54
assert sensor_msg["data"]
assert sensor_msg["dimmer"] == 1
assert sensor_msg["button"] == "rotate left"
assert sensor_msg["steps"] == 1
assert sensor_msg["dimmer"] == "rotate left"
assert sensor_msg["rssi"] == -17

def test_Xiaomi_K9B(self):
Expand Down

0 comments on commit fa6ead3

Please sign in to comment.