From 944ac4f3c2c4db1e0af62735b20b1520132c0faa Mon Sep 17 00:00:00 2001 From: Andrew Chatham Date: Thu, 15 Aug 2019 04:40:39 -0700 Subject: [PATCH] Expose Lutron RA2 occupancy sensors (#25854) * Expose Lutron RA2 motion sensors * Remove unused _LOGGER * Remove unused logging import --- homeassistant/components/lutron/__init__.py | 16 +++++- .../components/lutron/binary_sensor.py | 56 +++++++++++++++++++ homeassistant/components/lutron/manifest.json | 2 +- requirements_all.txt | 2 +- 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 homeassistant/components/lutron/binary_sensor.py diff --git a/homeassistant/components/lutron/__init__.py b/homeassistant/components/lutron/__init__.py index 1ef9a9374f7..9f4d81df044 100644 --- a/homeassistant/components/lutron/__init__.py +++ b/homeassistant/components/lutron/__init__.py @@ -40,7 +40,13 @@ def setup(hass, base_config): hass.data[LUTRON_BUTTONS] = [] hass.data[LUTRON_CONTROLLER] = None - hass.data[LUTRON_DEVICES] = {"light": [], "cover": [], "switch": [], "scene": []} + hass.data[LUTRON_DEVICES] = { + "light": [], + "cover": [], + "switch": [], + "scene": [], + "binary_sensor": [], + } config = base_config.get(DOMAIN) hass.data[LUTRON_CONTROLLER] = Lutron( @@ -76,9 +82,13 @@ def setup(hass, base_config): ) hass.data[LUTRON_BUTTONS].append(LutronButton(hass, keypad, button)) + if area.occupancy_group is not None: + hass.data[LUTRON_DEVICES]["binary_sensor"].append( + (area.name, area.occupancy_group) + ) - for component in ("light", "cover", "switch", "scene"): - discovery.load_platform(hass, component, DOMAIN, None, base_config) + for component in ("light", "cover", "switch", "scene", "binary_sensor"): + discovery.load_platform(hass, component, DOMAIN, {}, base_config) return True diff --git a/homeassistant/components/lutron/binary_sensor.py b/homeassistant/components/lutron/binary_sensor.py new file mode 100644 index 00000000000..a86d56c325f --- /dev/null +++ b/homeassistant/components/lutron/binary_sensor.py @@ -0,0 +1,56 @@ +"""Support for Lutron Powr Savr occupancy sensors.""" +from pylutron import OccupancyGroup + +from homeassistant.components.binary_sensor import ( + BinarySensorDevice, + DEVICE_CLASS_OCCUPANCY, +) + +from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the Lutron occupancy sensors.""" + if discovery_info is None: + return + devs = [] + for (area_name, device) in hass.data[LUTRON_DEVICES]["binary_sensor"]: + dev = LutronOccupancySensor(area_name, device, hass.data[LUTRON_CONTROLLER]) + devs.append(dev) + + add_entities(devs) + + +class LutronOccupancySensor(LutronDevice, BinarySensorDevice): + """Representation of a Lutron Occupancy Group. + + The Lutron integration API reports "occupancy groups" rather than + individual sensors. If two sensors are in the same room, they're + reported as a single occupancy group. + """ + + @property + def is_on(self): + """Return true if the binary sensor is on.""" + # Error cases will end up treated as unoccupied. + return self._lutron_device.state == OccupancyGroup.State.OCCUPIED + + @property + def device_class(self): + """Return that this is an occupancy sensor.""" + return DEVICE_CLASS_OCCUPANCY + + @property + def name(self): + """Return the name of the device.""" + # The default LutronDevice naming would create 'Kitchen Occ Kitchen', + # but since there can only be one OccupancyGroup per area we go + # with something shorter. + return f"{self._area_name} Occupancy" + + @property + def device_state_attributes(self): + """Return the state attributes.""" + attr = {} + attr["lutron_integration_id"] = self._lutron_device.id + return attr diff --git a/homeassistant/components/lutron/manifest.json b/homeassistant/components/lutron/manifest.json index ab7ca3d919f..bece55ae09d 100644 --- a/homeassistant/components/lutron/manifest.json +++ b/homeassistant/components/lutron/manifest.json @@ -3,7 +3,7 @@ "name": "Lutron", "documentation": "https://www.home-assistant.io/components/lutron", "requirements": [ - "pylutron==0.2.1" + "pylutron==0.2.2" ], "dependencies": [], "codeowners": [] diff --git a/requirements_all.txt b/requirements_all.txt index 0008a61c872..f05be3a96d8 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1258,7 +1258,7 @@ pyloopenergy==0.1.3 pylutron-caseta==0.5.0 # homeassistant.components.lutron -pylutron==0.2.1 +pylutron==0.2.2 # homeassistant.components.mailgun pymailgunner==1.4