Skip to content
This repository has been archived by the owner on Jul 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #233 from azogue/chore/more-remote-samples
Browse files Browse the repository at this point in the history
Chore: more parsing tests
  • Loading branch information
robmarkcole authored Mar 13, 2020
2 parents e4ca16d + 3b1f2a9 commit 1b37a68
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 53 deletions.
112 changes: 60 additions & 52 deletions custom_components/huesensor/hue_api_response.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
"""Hue API data parsing for sensors."""
import logging
from typing import Any, Callable, Iterable, Dict, Optional, Tuple

from aiohue.sensors import (
ZGP_SWITCH_BUTTON_1,
ZGP_SWITCH_BUTTON_2,
ZGP_SWITCH_BUTTON_3,
ZGP_SWITCH_BUTTON_4,
ZLL_SWITCH_BUTTON_1_INITIAL_PRESS,
ZLL_SWITCH_BUTTON_2_INITIAL_PRESS,
ZLL_SWITCH_BUTTON_3_INITIAL_PRESS,
ZLL_SWITCH_BUTTON_4_INITIAL_PRESS,
ZLL_SWITCH_BUTTON_1_HOLD,
ZLL_SWITCH_BUTTON_2_HOLD,
ZLL_SWITCH_BUTTON_3_HOLD,
ZLL_SWITCH_BUTTON_4_HOLD,
ZLL_SWITCH_BUTTON_1_SHORT_RELEASED,
ZLL_SWITCH_BUTTON_2_SHORT_RELEASED,
ZLL_SWITCH_BUTTON_3_SHORT_RELEASED,
ZLL_SWITCH_BUTTON_4_SHORT_RELEASED,
ZLL_SWITCH_BUTTON_1_LONG_RELEASED,
ZLL_SWITCH_BUTTON_2_LONG_RELEASED,
ZLL_SWITCH_BUTTON_3_LONG_RELEASED,
ZLL_SWITCH_BUTTON_4_LONG_RELEASED,
)
from homeassistant.const import STATE_OFF, STATE_ON

REMOTE_MODELS = ("RWL", "ROM", "FOH", "ZGP", "Z3-")
Expand Down Expand Up @@ -50,13 +71,30 @@
98: "double_lower_press",
99: "double_lower_release",
}
RWL_RESPONSE_CODES = {
"0": "_click",
"1": "_hold",
"2": "_click_up",
"3": "_hold_up",
RWL_BUTTONS = {
ZLL_SWITCH_BUTTON_1_INITIAL_PRESS: "1_click",
ZLL_SWITCH_BUTTON_2_INITIAL_PRESS: "1_click",
ZLL_SWITCH_BUTTON_3_INITIAL_PRESS: "1_click",
ZLL_SWITCH_BUTTON_4_INITIAL_PRESS: "1_click",
ZLL_SWITCH_BUTTON_1_HOLD: "1_hold",
ZLL_SWITCH_BUTTON_2_HOLD: "2_hold",
ZLL_SWITCH_BUTTON_3_HOLD: "3_hold",
ZLL_SWITCH_BUTTON_4_HOLD: "4_hold",
ZLL_SWITCH_BUTTON_1_SHORT_RELEASED: "1_click_up",
ZLL_SWITCH_BUTTON_2_SHORT_RELEASED: "2_click_up",
ZLL_SWITCH_BUTTON_3_SHORT_RELEASED: "3_click_up",
ZLL_SWITCH_BUTTON_4_SHORT_RELEASED: "4_click_up",
ZLL_SWITCH_BUTTON_1_LONG_RELEASED: "1_hold_up",
ZLL_SWITCH_BUTTON_2_LONG_RELEASED: "2_hold_up",
ZLL_SWITCH_BUTTON_3_LONG_RELEASED: "3_hold_up",
ZLL_SWITCH_BUTTON_4_LONG_RELEASED: "4_hold_up",
}
TAP_BUTTONS = {
ZGP_SWITCH_BUTTON_1: "1_click",
ZGP_SWITCH_BUTTON_2: "2_click",
ZGP_SWITCH_BUTTON_3: "3_click",
ZGP_SWITCH_BUTTON_4: "4_click",
}
TAP_BUTTONS = {34: "1_click", 16: "2_click", 17: "3_click", 18: "4_click"}
Z3_BUTTON = {
1000: "initial_press",
1001: "repeat",
Expand Down Expand Up @@ -96,21 +134,17 @@ def parse_sml(response: Dict[str, Any]) -> Dict[str, Any]:
}

elif response["type"] == "ZLLTemperature":
if response["state"]["temperature"] is not None:
data = {"temperature": response["state"]["temperature"] / 100.0}
else:
data = {"temperature": "No temperature data"}
temp = response["state"]["temperature"]
temp = temp / 100.0 if temp is not None else "No temperature data"
data = {"temperature": temp}

elif response["type"] == "ZLLPresence":
name_raw = response["name"]
arr = name_raw.split()
arr.insert(-1, "motion")
name = " ".join(arr)
hue_state = response["state"]["presence"]
if hue_state is True:
state = STATE_ON
else:
state = STATE_OFF
state = STATE_ON if hue_state is True else STATE_OFF

data = {
"model": "SML",
Expand All @@ -127,30 +161,22 @@ def parse_sml(response: Dict[str, Any]) -> Dict[str, Any]:

def parse_zgp(response: Dict[str, Any]) -> Dict[str, Any]:
"""Parse the json response for a ZGPSWITCH Hue Tap."""
press = response["state"]["buttonevent"]
if press is None or press not in TAP_BUTTONS:
button = "No data"
else:
button = TAP_BUTTONS[press]
button = TAP_BUTTONS.get(response["state"]["buttonevent"], "No data")

data = {
return {
"model": "ZGP",
"name": response["name"],
"state": button,
"last_button_event": button,
"last_updated": response["state"]["lastupdated"].split("T"),
}
return data


def parse_rwl(response: Dict[str, Any]) -> Dict[str, Any]:
"""Parse the json response for a RWL Hue remote."""
button = None
if response["state"]["buttonevent"]:
press = str(response["state"]["buttonevent"])
button = str(press)[0] + RWL_RESPONSE_CODES[press[-1]]
button = RWL_BUTTONS.get(response["state"]["buttonevent"])

data = {
return {
"model": "RWL",
"name": response["name"],
"state": button,
Expand All @@ -160,37 +186,29 @@ def parse_rwl(response: Dict[str, Any]) -> Dict[str, Any]:
"last_button_event": button,
"last_updated": response["state"]["lastupdated"].split("T"),
}
return data


def parse_foh(response: Dict[str, Any]) -> Dict[str, Any]:
"""Parse the JSON response for a FOHSWITCH (type still = ZGPSwitch)."""
press = response["state"]["buttonevent"]
if press is None or press not in FOH_BUTTONS:
button = "No data"
else:
button = FOH_BUTTONS[press]
button = FOH_BUTTONS.get(press, "No data")

data = {
return {
"model": "FOH",
"name": response["name"],
"state": button,
"last_button_event": button,
"last_updated": response["state"]["lastupdated"].split("T"),
}
return data


def parse_z3_rotary(response: Dict[str, Any]) -> Dict[str, Any]:
"""Parse the json response for a Lutron Aurora Rotary Event."""
turn = response["state"]["rotaryevent"]
dial = Z3_DIAL.get(turn, "No data")
dial_position = response["state"]["expectedrotation"]
if turn is None or turn not in Z3_DIAL:
dial = "No data"
else:
dial = Z3_DIAL[turn]

data = {
return {
"model": "Z3-",
"name": response["name"],
"dial_state": dial,
Expand All @@ -201,34 +219,24 @@ def parse_z3_rotary(response: Dict[str, Any]) -> Dict[str, Any]:
"reachable": response["config"]["reachable"],
"last_updated": response["state"]["lastupdated"].split("T"),
}
return data


def parse_z3_switch(response: Dict[str, Any]) -> Dict[str, Any]:
"""Parse the json response for a Lutron Aurora."""
press = response["state"]["buttonevent"]
logging.warning(press)
if press is None or press not in Z3_BUTTON:
button = "No data"
else:
button = Z3_BUTTON[press]
button = Z3_BUTTON.get(press, "No data")

data = {
return {
"last_button_event": button,
"state": button,
"last_updated": response["state"]["lastupdated"].split("T"),
}
return data


def _ident_raw_sensor(
raw_sensor_data: Dict[str, Any]
) -> Tuple[Optional[str], Callable]:
"""Identify sensor types and return unique identifier and parser."""

def _default_parser(*x):
return x

model_id = raw_sensor_data["modelid"][0:3]
unique_sensor_id = raw_sensor_data["uniqueid"]

Expand Down Expand Up @@ -260,7 +268,7 @@ def _default_parser(*x):
sensor_key = model_id + "_" + unique_sensor_id
return sensor_key, parse_z3_switch

return None, _default_parser
return None, None


def parse_hue_api_response(sensors: Iterable[Dict[str, Any]]):
Expand Down
123 changes: 123 additions & 0 deletions tests/sensor_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,79 @@
"uniqueid": "00:00:00:00:00:44:23:08-f2",
"capabilities": {"certified": True, "primary": True, "inputs": []},
}
MOCK_FOH = {
"state": {"buttonevent": 21, "lastupdated": "2019-04-22T07:48:56"},
"swupdate": {"state": "notupdatable", "lastinstall": None},
"config": {"on": True},
"name": "Bed Switch",
"type": "ZGPSwitch",
"modelid": "FOHSWITCH",
"manufacturername": "PhilipsFoH",
"productname": "Friends of Hue Switch",
"diversityid": "ded6468f-6b26-4a75-9582-f2b52d36a5a3",
"uniqueid": "00:00:00:00:01:70:xx:xx-xx",
"capabilities": {
"certified": True,
"inputs": [
{
"repeatintervals": [],
"events": [
{"buttonevent": 16, "eventtype": "initial_press"},
{"buttonevent": 20, "eventtype": "short_release"},
],
},
{
"repeatintervals": [],
"events": [
{"buttonevent": 17, "eventtype": "initial_press"},
{"buttonevent": 21, "eventtype": "short_release"},
],
},
{
"repeatintervals": [],
"events": [
{"buttonevent": 19, "eventtype": "initial_press"},
{"buttonevent": 23, "eventtype": "short_release"},
],
},
{
"repeatintervals": [],
"events": [
{"buttonevent": 18, "eventtype": "initial_press"},
{"buttonevent": 22, "eventtype": "short_release"},
],
},
],
},
}
MOCK_ROM = {
"state": {"buttonevent": 1002, "lastupdated": "2019-11-16T11:48:24"},
"swupdate": {"state": "noupdates", "lastinstall": "2019-11-16T10:54:58"},
"config": {"on": True, "battery": 100, "reachable": True, "pending": []},
"name": "Hue Smart button 2",
"type": "ZLLSwitch",
"modelid": "ROM001",
"manufacturername": "Philips",
"productname": "Hue Smart button",
"diversityid": "8b18a40c-eb6a-40d7-a0af-eb0906613d41",
"swversion": "2.21.0_r29784",
"uniqueid": "00:17:88:01:06:06:81:5c-01-fc00",
"capabilities": {
"certified": True,
"primary": True,
"inputs": [
{
"repeatintervals": [800],
"events": [
{"buttonevent": 1000, "eventtype": "initial_press"},
{"buttonevent": 1001, "eventtype": "repeat"},
{"buttonevent": 1002, "eventtype": "short_release"},
{"buttonevent": 1003, "eventtype": "long_release"},
],
}
],
},
}
MOCK_RWL = {
"state": {"buttonevent": 4002, "lastupdated": "2019-12-28T21:58:02"},
"swupdate": {"state": "noupdates", "lastinstall": "2019-10-13T13:16:15"},
Expand Down Expand Up @@ -153,6 +226,34 @@
],
},
}
MOCK_Z3_SWITCH = {
"state": {"buttonevent": 1002, "lastupdated": "2019-09-01T17:45:47"},
"swupdate": {"state": "noupdates", "lastinstall": "2019-09-01T15:26:15"},
"config": {"on": True, "battery": 100, "reachable": True, "pending": []},
"name": "Lutron Aurora 2",
"type": "ZLLSwitch",
"modelid": "Z3-1BRL",
"manufacturername": "Lutron",
"productname": "Lutron Aurora",
"diversityid": "2c3a75ff-55c4-4e4d-8c44-82d330b8eb9b",
"swversion": "3.1",
"uniqueid": "ff:ff:00:0f:e7:fe:95:cd-01-fc00",
"capabilities": {
"certified": True,
"primary": False,
"inputs": [
{
"repeatintervals": [800],
"events": [
{"buttonevent": 1000, "eventtype": "initial_press"},
{"buttonevent": 1001, "eventtype": "repeat"},
{"buttonevent": 1002, "eventtype": "short_release"},
{"buttonevent": 1003, "eventtype": "long_release"},
],
}
],
},
}

PARSED_ZGP = {
"last_button_event": "3_click",
Expand All @@ -161,6 +262,23 @@
"name": "Hue Tap",
"state": "3_click",
}
PARSED_FOH = {
"last_button_event": "left_lower_release",
"last_updated": ["2019-04-22", "07:48:56"],
"model": "FOH",
"name": "Bed Switch",
"state": "left_lower_release",
}
PARSED_ROM = {
"battery": 100,
"last_button_event": "1_click_up",
"last_updated": ["2019-11-16", "11:48:24"],
"model": "RWL",
"name": "Hue Smart button 2",
"on": True,
"reachable": True,
"state": "1_click_up",
}
PARSED_RWL = {
"battery": 100,
"last_button_event": "4_click_up",
Expand All @@ -182,6 +300,11 @@
"reachable": True,
"last_updated": ["2020-01-31", "15:56:19"],
}
PARSED_Z3_SWITCH = {
"last_button_event": "short_release",
"state": "short_release",
"last_updated": ["2019-09-01", "17:45:47"],
}

# Hue geofences
MOCK_GEOFENCE = {
Expand Down
Loading

0 comments on commit 1b37a68

Please sign in to comment.