Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
This commit is contained in:
1 parent
dd1608db0d
commit
f5cbc9d208
5 files changed
+385
-73
No files matched your search
@@ -11,6 +11,7 @@ import pytest
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import hue
|
||||
from homeassistant.components.hue import sensor_base as hue_sensor_base
|
||||
from homeassistant.components.hue.hue_event import CONF_HUE_EVENT
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -241,6 +242,33 @@ UNSUPPORTED_SENSOR = {
|
||||
"uniqueid": "arbitrary",
|
||||
"recycle": True,
|
||||
}
|
||||
HUE_TAP_REMOTE_1 = {
|
||||
"state": {"buttonevent": 17, "lastupdated": "2019-06-22T14:43:50"},
|
||||
"swupdate": {"state": "notupdatable", "lastinstall": None},
|
||||
"config": {"on": True},
|
||||
"name": "Hue Tap",
|
||||
"type": "ZGPSwitch",
|
||||
"modelid": "ZGPSWITCH",
|
||||
"manufacturername": "Philips",
|
||||
"productname": "Hue tap switch",
|
||||
"diversityid": "d8cde5d5-0eef-4b95-b0f0-71ddd2952af4",
|
||||
"uniqueid": "00:00:00:00:00:44:23:08-f2",
|
||||
"capabilities": {"certified": True, "primary": True, "inputs": []},
|
||||
}
|
||||
HUE_DIMMER_REMOTE_1 = {
|
||||
"state": {"buttonevent": 4002, "lastupdated": "2019-12-28T21:58:02"},
|
||||
"swupdate": {"state": "noupdates", "lastinstall": "2019-10-13T13:16:15"},
|
||||
"config": {"on": True, "battery": 100, "reachable": True, "pending": []},
|
||||
"name": "Hue dimmer switch 1",
|
||||
"type": "ZLLSwitch",
|
||||
"modelid": "RWL021",
|
||||
"manufacturername": "Philips",
|
||||
"productname": "Hue dimmer switch",
|
||||
"diversityid": "73bbabea-3420-499a-9856-46bf437e119b",
|
||||
"swversion": "6.1.1.28573",
|
||||
"uniqueid": "00:17:88:01:10:3e:3a:dc-02-fc00",
|
||||
"capabilities": {"certified": True, "primary": True, "inputs": []},
|
||||
}
|
||||
SENSOR_RESPONSE = {
|
||||
"1": PRESENCE_SENSOR_1_PRESENT,
|
||||
"2": LIGHT_LEVEL_SENSOR_1,
|
||||
@@ -248,6 +276,8 @@ SENSOR_RESPONSE = {
|
||||
"4": PRESENCE_SENSOR_2_NOT_PRESENT,
|
||||
"5": LIGHT_LEVEL_SENSOR_2,
|
||||
"6": TEMPERATURE_SENSOR_2,
|
||||
"7": HUE_TAP_REMOTE_1,
|
||||
"8": HUE_DIMMER_REMOTE_1,
|
||||
}
|
||||
|
||||
|
||||
@@ -341,8 +371,8 @@ async def test_sensors_with_multiple_bridges(hass, mock_bridge):
|
||||
|
||||
assert len(mock_bridge.mock_requests) == 1
|
||||
assert len(mock_bridge_2.mock_requests) == 1
|
||||
# 3 "physical" sensors with 3 virtual sensors each
|
||||
assert len(hass.states.async_all()) == 9
|
||||
# 3 "physical" sensors with 3 virtual sensors each + 1 battery sensor
|
||||
assert len(hass.states.async_all()) == 10
|
||||
|
||||
|
||||
async def test_sensors(hass, mock_bridge):
|
||||
@@ -351,7 +381,7 @@ async def test_sensors(hass, mock_bridge):
|
||||
await setup_bridge(hass, mock_bridge)
|
||||
assert len(mock_bridge.mock_requests) == 1
|
||||
# 2 "physical" sensors with 3 virtual sensors each
|
||||
assert len(hass.states.async_all()) == 6
|
||||
assert len(hass.states.async_all()) == 7
|
||||
|
||||
presence_sensor_1 = hass.states.get("binary_sensor.living_room_sensor_motion")
|
||||
light_level_sensor_1 = hass.states.get("sensor.living_room_sensor_light_level")
|
||||
@@ -377,6 +407,11 @@ async def test_sensors(hass, mock_bridge):
|
||||
assert temperature_sensor_2.state == "18.75"
|
||||
assert temperature_sensor_2.name == "Kitchen sensor temperature"
|
||||
|
||||
battery_remote_1 = hass.states.get("sensor.hue_dimmer_switch_1_battery_level")
|
||||
assert battery_remote_1 is not None
|
||||
assert battery_remote_1.state == "100"
|
||||
assert battery_remote_1.name == "Hue dimmer switch 1 battery level"
|
||||
|
||||
|
||||
async def test_unsupported_sensors(hass, mock_bridge):
|
||||
"""Test that unsupported sensors don't get added and don't fail."""
|
||||
@@ -385,8 +420,8 @@ async def test_unsupported_sensors(hass, mock_bridge):
|
||||
mock_bridge.mock_sensor_responses.append(response_with_unsupported)
|
||||
await setup_bridge(hass, mock_bridge)
|
||||
assert len(mock_bridge.mock_requests) == 1
|
||||
# 2 "physical" sensors with 3 virtual sensors each
|
||||
assert len(hass.states.async_all()) == 6
|
||||
# 2 "physical" sensors with 3 virtual sensors each + 1 battery sensor
|
||||
assert len(hass.states.async_all()) == 7
|
||||
|
||||
|
||||
async def test_new_sensor_discovered(hass, mock_bridge):
|
||||
@@ -395,14 +430,14 @@ async def test_new_sensor_discovered(hass, mock_bridge):
|
||||
|
||||
await setup_bridge(hass, mock_bridge)
|
||||
assert len(mock_bridge.mock_requests) == 1
|
||||
assert len(hass.states.async_all()) == 6
|
||||
assert len(hass.states.async_all()) == 7
|
||||
|
||||
new_sensor_response = dict(SENSOR_RESPONSE)
|
||||
new_sensor_response.update(
|
||||
{
|
||||
"7": PRESENCE_SENSOR_3_PRESENT,
|
||||
"8": LIGHT_LEVEL_SENSOR_3,
|
||||
"9": TEMPERATURE_SENSOR_3,
|
||||
"9": PRESENCE_SENSOR_3_PRESENT,
|
||||
"10": LIGHT_LEVEL_SENSOR_3,
|
||||
"11": TEMPERATURE_SENSOR_3,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -413,7 +448,7 @@ async def test_new_sensor_discovered(hass, mock_bridge):
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_bridge.mock_requests) == 2
|
||||
assert len(hass.states.async_all()) == 9
|
||||
assert len(hass.states.async_all()) == 10
|
||||
|
||||
presence = hass.states.get("binary_sensor.bedroom_sensor_motion")
|
||||
assert presence is not None
|
||||
@@ -429,7 +464,7 @@ async def test_sensor_removed(hass, mock_bridge):
|
||||
|
||||
await setup_bridge(hass, mock_bridge)
|
||||
assert len(mock_bridge.mock_requests) == 1
|
||||
assert len(hass.states.async_all()) == 6
|
||||
assert len(hass.states.async_all()) == 7
|
||||
|
||||
mock_bridge.mock_sensor_responses.clear()
|
||||
keys = ("1", "2", "3")
|
||||
@@ -466,3 +501,121 @@ async def test_update_unauthorized(hass, mock_bridge):
|
||||
assert len(mock_bridge.mock_requests) == 0
|
||||
assert len(hass.states.async_all()) == 0
|
||||
assert len(mock_bridge.handle_unauthorized_error.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_hue_events(hass, mock_bridge):
|
||||
"""Test that hue remotes fire events when pressed."""
|
||||
mock_bridge.mock_sensor_responses.append(SENSOR_RESPONSE)
|
||||
|
||||
mock_listener = Mock()
|
||||
unsub = hass.bus.async_listen(CONF_HUE_EVENT, mock_listener)
|
||||
|
||||
await setup_bridge(hass, mock_bridge)
|
||||
assert len(mock_bridge.mock_requests) == 1
|
||||
assert len(hass.states.async_all()) == 7
|
||||
assert len(mock_listener.mock_calls) == 0
|
||||
|
||||
new_sensor_response = dict(SENSOR_RESPONSE)
|
||||
new_sensor_response["7"]["state"] = {
|
||||
"buttonevent": 18,
|
||||
"lastupdated": "2019-12-28T22:58:02",
|
||||
}
|
||||
mock_bridge.mock_sensor_responses.append(new_sensor_response)
|
||||
|
||||
# Force updates to run again
|
||||
await mock_bridge.sensor_manager.coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_bridge.mock_requests) == 2
|
||||
assert len(hass.states.async_all()) == 7
|
||||
assert len(mock_listener.mock_calls) == 1
|
||||
assert mock_listener.mock_calls[0][1][0].data == {
|
||||
"id": "hue_tap",
|
||||
"unique_id": "00:00:00:00:00:44:23:08-f2",
|
||||
"event": 18,
|
||||
"last_updated": "2019-12-28T22:58:02",
|
||||
}
|
||||
|
||||
new_sensor_response = dict(new_sensor_response)
|
||||
new_sensor_response["8"]["state"] = {
|
||||
"buttonevent": 3002,
|
||||
"lastupdated": "2019-12-28T22:58:01",
|
||||
}
|
||||
mock_bridge.mock_sensor_responses.append(new_sensor_response)
|
||||
|
||||
# Force updates to run again
|
||||
await mock_bridge.sensor_manager.coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_bridge.mock_requests) == 3
|
||||
assert len(hass.states.async_all()) == 7
|
||||
assert len(mock_listener.mock_calls) == 2
|
||||
assert mock_listener.mock_calls[1][1][0].data == {
|
||||
"id": "hue_dimmer_switch_1",
|
||||
"unique_id": "00:17:88:01:10:3e:3a:dc-02-fc00",
|
||||
"event": 3002,
|
||||
"last_updated": "2019-12-28T22:58:01",
|
||||
}
|
||||
|
||||
# Add a new remote. In discovery the new event is registered **but not fired**
|
||||
new_sensor_response = dict(new_sensor_response)
|
||||
new_sensor_response["21"] = {
|
||||
"state": {
|
||||
"rotaryevent": 2,
|
||||
"expectedrotation": 208,
|
||||
"expectedeventduration": 400,
|
||||
"lastupdated": "2020-01-31T15:56:19",
|
||||
},
|
||||
"swupdate": {"state": "noupdates", "lastinstall": "2019-11-26T03:35:21"},
|
||||
"config": {"on": True, "battery": 100, "reachable": True, "pending": []},
|
||||
"name": "Lutron Aurora 1",
|
||||
"type": "ZLLRelativeRotary",
|
||||
"modelid": "Z3-1BRL",
|
||||
"manufacturername": "Lutron",
|
||||
"productname": "Lutron Aurora",
|
||||
"diversityid": "2c3a75ff-55c4-4e4d-8c44-82d330b8eb9b",
|
||||
"swversion": "3.4",
|
||||
"uniqueid": "ff:ff:00:0f:e7:fd:bc:b7-01-fc00-0014",
|
||||
"capabilities": {
|
||||
"certified": True,
|
||||
"primary": True,
|
||||
"inputs": [
|
||||
{
|
||||
"repeatintervals": [400],
|
||||
"events": [
|
||||
{"rotaryevent": 1, "eventtype": "start"},
|
||||
{"rotaryevent": 2, "eventtype": "repeat"},
|
||||
],
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
mock_bridge.mock_sensor_responses.append(new_sensor_response)
|
||||
|
||||
# Force updates to run again
|
||||
await mock_bridge.sensor_manager.coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_bridge.mock_requests) == 4
|
||||
assert len(hass.states.async_all()) == 8
|
||||
assert len(mock_listener.mock_calls) == 2
|
||||
|
||||
# A new press fires the event
|
||||
new_sensor_response["21"]["state"]["lastupdated"] = "2020-01-31T15:57:19"
|
||||
mock_bridge.mock_sensor_responses.append(new_sensor_response)
|
||||
|
||||
# Force updates to run again
|
||||
await mock_bridge.sensor_manager.coordinator.async_refresh()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(mock_bridge.mock_requests) == 5
|
||||
assert len(hass.states.async_all()) == 8
|
||||
assert len(mock_listener.mock_calls) == 3
|
||||
assert mock_listener.mock_calls[2][1][0].data == {
|
||||
"id": "lutron_aurora_1",
|
||||
"unique_id": "ff:ff:00:0f:e7:fd:bc:b7-01-fc00-0014",
|
||||
"event": 2,
|
||||
"last_updated": "2020-01-31T15:57:19",
|
||||
}
|
||||
|
||||
unsub()
|
||||
Reference in new issue
Block a user