Skip to content

Commit

Permalink
Add more usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
azogue committed Apr 17, 2020
1 parent 4abbcaf commit bcabeae
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
application-import-names = homeassistant,eventsensor
import-order-style = smarkets
inline-quotes = double
max-complexity = 15
multiline-quotes = double
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [v1.0.0](https://github.com/azogue/eventsensor/tree/v1.0.0) (2020-04-17)

**Initial version**
64 changes: 56 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
![Validate with hassfest](https://github.com/azogue/eventsensor/workflows/Validate%20with%20hassfest/badge.svg?branch=master)
[![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg)](https://github.com/custom-components/hacs)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)

<br><a href="https://www.buymeacoffee.com/azogue" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-black.png" width="150px" height="35px" alt="Buy Me A Coffee" style="height: 35px !important;width: 150px !important;" ></a>

# Event sensor

Custom integration to create sensors that track, represent and store specific **events** in Home Assistant.
Custom integration to create sensors that track, represent and store specific Home Assistant **events**.

Created to assign HA entities for ZigBee switches that only generate events when pressed,
but could be useful in other scenarios where some specific event needs to be tracked.
Expand All @@ -16,8 +18,12 @@ Place the `custom_components` folder in your configuration directory

## Configuration

Once installed add to your configuration the desired sensors like this one
(a [Hue tap switch](https://www2.meethue.com/en-us/p/hue-tap-switch/046677473365) integrated in HA via [deCONZ integration](https://www.home-assistant.io/integrations/deconz/))
Once installed add to your yaml configuration the desired sensors like in the next examples.

* Optionally filter events with key-value pairs inside the event data (like identifiers for item generating the event).
* Optionally define a `state_map` to define custom states from the raw event data value.

#### A [Hue tap switch](https://www2.meethue.com/en-us/p/hue-tap-switch/046677473365) integrated in HA via [deCONZ integration](https://www.home-assistant.io/integrations/deconz/)

```yaml
sensor:
Expand All @@ -34,9 +40,6 @@ sensor:
18: 4_click
```

* Optionally filter events with key-value pairs inside the event data (like identifiers for item generating the event).
* Optionally define a `state_map` to define custom states from the raw event data value.

When some event that matches the filters is received, the sensor is updated:

```json
Expand All @@ -59,6 +62,52 @@ When some event that matches the filters is received, the sensor is updated:

Making the sensor state equal to "2_click".

#### A Hue dimmer switch integrated in HA via [hue integration](https://www.home-assistant.io/integrations/hue/)

```yaml
sensor:
- platform: eventsensor
name: Dimmer switch last press
event: hue_event
event_data:
id: switch_bedroom
state: event
state_map:
# these will probably be missed (because of the hue polling)
1000: 1_click
2000: 2_click
3000: 3_click
4000: 4_click
1001: 1_hold
2001: 2_hold
3001: 3_hold
4001: 4_hold
# these will be detected always
1002: 1_click_up
2002: 2_click_up
3002: 3_click_up
4002: 4_click_up
1003: 1_hold_up
2003: 2_hold_up
3003: 3_hold_up
4003: 4_hold_up
```

#### A sensor to catch the event data when new devices are detected

When the legacy `device_tracker` detects a new entity in the network
it fires a specific event carrying the MAC address, host name and new entity_id.

To make a sensor to retain that data until another new device is detected, use this:

```yaml
sensor:
- platform: eventsensor
name: Last detected device
event: device_tracker_new_device
state: mac
```

#### Sensor Configuration

key | optional | type | default | description
Expand All @@ -71,6 +120,5 @@ key | optional | type | default | description

## TODO

- [ ] [HACS](https://hacs.xyz/) integration.
- [ ] Inclusion as [HACS](https://hacs.xyz/) **default** integration (manual addition to HACS is already possible)
- [ ] Config flow to define these sensors via UI, so it could be used easily for debug purposes without any HA restart.
- [ ] Better docs
2 changes: 1 addition & 1 deletion custom_components/eventsensor/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_EVENT,
Expand Down Expand Up @@ -104,7 +105,6 @@ async def async_added_to_hass(self) -> None:
@callback
def async_update_sensor(event: Event):
"""Update state when event is received."""
_LOGGER.debug("%s: Event received -> %s", self.entity_id, event)
if self._event_data.items() < event.data.items():
new_state = event.data[self._state_key]
if new_state in self._state_map:
Expand Down

0 comments on commit bcabeae

Please sign in to comment.