From e5f0da75e25d2088bf31842c7b60e75b0d8a3458 Mon Sep 17 00:00:00 2001
From: Tom Harris <tharris@harrisnj.net>
Date: Wed, 18 Jul 2018 10:11:54 -0400
Subject: [PATCH] Mini-Remote events (#15523)

* Add event handler to capture binary sensor on messages

* Log event trigger

* Log event firing

* Capture platform correctly

* Fix test for platform eq binary_sensor

* Create sensor events

* Add light and battery sensors

* Bump insteonplm version to 0.11.6

* Fix naming of BUTTON_PRESSED_STATE_NAME

* Fix naming of fire event methods

* Add logging

* Add DOMAIN definition

* Get state name from plm.devices

* Remove stale reference to button ID

* Fix reference to state name

* Remove incorrect ref to self

* Log remote button pressed event

* Change mode to button_mode and fix values to array

* Rename CONF_MODE to CONF_BUTTON_MODE

* Log platform create with mode

* Properly assign button_mode to track mode

* Implement is_on

* Change mini-remotes to events only

* Remove button_mode config option

* Fix reference to _fire_button_on_off_event

* Bump insteon version to 0.11.7

* Flake8 clean up

* Flake8 cleanup

* Use % format in logging per pylint

* Code review updates

* Resolve conflict

* Lint
---
 .../components/binary_sensor/insteon_plm.py   | 11 ++++--
 .../components/insteon_plm/__init__.py        | 35 ++++++++++++++++---
 requirements_all.txt                          |  2 +-
 3 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/homeassistant/components/binary_sensor/insteon_plm.py b/homeassistant/components/binary_sensor/insteon_plm.py
index 9cb87b31749..25fc3fb5d73 100644
--- a/homeassistant/components/binary_sensor/insteon_plm.py
+++ b/homeassistant/components/binary_sensor/insteon_plm.py
@@ -17,7 +17,9 @@ _LOGGER = logging.getLogger(__name__)
 SENSOR_TYPES = {'openClosedSensor': 'opening',
                 'motionSensor': 'motion',
                 'doorSensor': 'door',
-                'wetLeakSensor': 'moisture'}
+                'wetLeakSensor': 'moisture',
+                'lightSensor': 'light',
+                'batterySensor': 'battery'}
 
 
 @asyncio.coroutine
@@ -54,4 +56,9 @@ class InsteonPLMBinarySensor(InsteonPLMEntity, BinarySensorDevice):
     @property
     def is_on(self):
         """Return the boolean response if the node is on."""
-        return bool(self._insteon_device_state.value)
+        on_val = bool(self._insteon_device_state.value)
+
+        if self._insteon_device_state.name == 'lightSensor':
+            return not on_val
+
+        return on_val
diff --git a/homeassistant/components/insteon_plm/__init__.py b/homeassistant/components/insteon_plm/__init__.py
index 82fc6b02266..ef631223894 100644
--- a/homeassistant/components/insteon_plm/__init__.py
+++ b/homeassistant/components/insteon_plm/__init__.py
@@ -17,7 +17,7 @@ import homeassistant.helpers.config_validation as cv
 from homeassistant.helpers import discovery
 from homeassistant.helpers.entity import Entity
 
-REQUIREMENTS = ['insteonplm==0.11.3']
+REQUIREMENTS = ['insteonplm==0.11.7']
 
 _LOGGER = logging.getLogger(__name__)
 
@@ -55,6 +55,11 @@ SRV_HOUSECODE = 'housecode'
 HOUSECODES = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
               'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
 
+BUTTON_PRESSED_STATE_NAME = 'onLevelButton'
+EVENT_BUTTON_ON = 'insteon_plm.button_on'
+EVENT_BUTTON_OFF = 'insteon_plm.button_off'
+EVENT_CONF_BUTTON = 'button'
+
 CONF_DEVICE_OVERRIDE_SCHEMA = vol.All(
     cv.deprecated(CONF_PLATFORM), vol.Schema({
         vol.Required(CONF_ADDRESS): cv.string,
@@ -130,9 +135,14 @@ def async_setup(hass, config):
         """Detect device from transport to be delegated to platform."""
         for state_key in device.states:
             platform_info = ipdb[device.states[state_key]]
-            if platform_info:
+            if platform_info and platform_info.platform:
                 platform = platform_info.platform
-                if platform:
+
+                if platform == 'on_off_events':
+                    device.states[state_key].register_updates(
+                        _fire_button_on_off_event)
+
+                else:
                     _LOGGER.info("New INSTEON PLM device: %s (%s) %s",
                                  device.address,
                                  device.states[state_key].name,
@@ -223,6 +233,23 @@ def async_setup(hass, config):
                                schema=X10_HOUSECODE_SCHEMA)
         _LOGGER.debug("Insteon_plm Services registered")
 
+    def _fire_button_on_off_event(address, group, val):
+        # Firing an event when a button is pressed.
+        device = plm.devices[address.hex]
+        state_name = device.states[group].name
+        button = ("" if state_name == BUTTON_PRESSED_STATE_NAME
+                  else state_name[-1].lower())
+        schema = {CONF_ADDRESS: address.hex}
+        if button != "":
+            schema[EVENT_CONF_BUTTON] = button
+        if val:
+            event = EVENT_BUTTON_ON
+        else:
+            event = EVENT_BUTTON_OFF
+        _LOGGER.debug('Firing event %s with address %s and button %s',
+                      event, address.hex, button)
+        hass.bus.fire(event, schema)
+
     _LOGGER.info("Looking for PLM on %s", port)
     conn = yield from insteonplm.Connection.create(
         device=port,
@@ -329,7 +356,7 @@ class IPDB(object):
 
                        State(DimmableSwitch_Fan, 'fan'),
                        State(DimmableSwitch, 'light'),
-                       State(DimmableRemote, 'binary_sensor'),
+                       State(DimmableRemote, 'on_off_events'),
 
                        State(X10DimmableSwitch, 'light'),
                        State(X10OnOffSwitch, 'switch'),
diff --git a/requirements_all.txt b/requirements_all.txt
index 8ef15e4bd18..1ef32386148 100644
--- a/requirements_all.txt
+++ b/requirements_all.txt
@@ -452,7 +452,7 @@ influxdb==5.0.0
 insteonlocal==0.53
 
 # homeassistant.components.insteon_plm
-insteonplm==0.11.3
+insteonplm==0.11.7
 
 # homeassistant.components.sensor.iperf3
 iperf3==0.1.10
-- 
GitLab