Skip to content

Commit

Permalink
Expanded debug options
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Feb 20, 2020
1 parent 038c10f commit 452a5cc
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 4 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 0.2.12 - 2020-02-20

### Added

- Service `sonoff.send_command` to send low-level commands.

### Changed

- Unknown devices will be added as binary_sensor. For debugging purposes

## 0.2.11 - 2020-02-14

### Added
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,49 @@ Not show devices.

## Component Debugging

Add to your `configuration.yaml`:

```yaml
logger:
default: info
logs:
custom_components.sonoff: debug
```

Only devices with firmware 3 and higher are supported.

All unknown devices with command `switch` support will be added as `switch`.

All other unknown devices will be added as `binary_sensor` (always `off`). The full state of the device is displayed in its attributes.

The component adds the service `sonoff.send_command` to send low-level commands.

Example service params to single switch:

```yaml
device: 1000123456
command: switch
switch: 'on'
```

Example service params to multi-channel switch:

```
device: 1000123456
command: switches
switches: [{outlet: 0, switch: 'off'}]
```

Example service params to dimmer:

```
device: 1000123456
command: dimmable
switch: 'on'
brightness: 50
mode: 0
```

## Useful Links

- https://github.com/peterbuga/HASS-sonoff-ewelink
Expand Down
21 changes: 17 additions & 4 deletions custom_components/sonoff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import voluptuous as vol
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD, CONF_DEVICES, \
CONF_NAME, CONF_DEVICE_CLASS
from homeassistant.core import ServiceCall
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.discovery import load_platform

Expand Down Expand Up @@ -94,10 +95,8 @@ def add_device(devicecfg: dict, state: dict):
device_class = 'switch'
elif 'switches' in state:
device_class = ['switch'] * 4

if not device_class:
_LOGGER.error(f"Unknown device_class {deviceid}")
return
else:
device_class = 'binary_sensor'

if isinstance(device_class, str):
# read single device_class
Expand All @@ -120,6 +119,20 @@ def add_device(devicecfg: dict, state: dict):
listener = EWeLinkListener(devices)
listener.listen(add_device)

async def send_command(call: ServiceCall):
data = dict(call.data)

deviceid = str(data.pop('device'))
command = data.pop('command')

device = devices.get(deviceid)
if isinstance(device, EWeLinkDevice):
device.send(command, data)
else:
_LOGGER.error(f"Device {deviceid} not found")

hass.services.register(DOMAIN, 'send_command', send_command)

return True


Expand Down
59 changes: 59 additions & 0 deletions custom_components/sonoff/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import Optional
import json
from homeassistant.components.binary_sensor import BinarySensorDevice

from . import DOMAIN, EWeLinkDevice


def setup_platform(hass, config, add_entities, discovery_info=None):
if discovery_info is None:
return

deviceid = discovery_info['deviceid']
device = hass.data[DOMAIN][deviceid]
add_entities([EWeLinkBinarySensor(device)])


class EWeLinkBinarySensor(BinarySensorDevice):
def __init__(self, device):
self.device = device
self._attrs = {}
self._name = None

self._update(device)

device.listen(self._update)

async def async_added_to_hass(self) -> None:
self._name = self.device.name

def _update(self, device: EWeLinkDevice):
state = {k: json.dumps(v) for k, v in device.state.items()}
self._attrs.update(state)

if self.hass:
self.schedule_update_ha_state()

@property
def should_poll(self) -> bool:
return False

@property
def unique_id(self) -> Optional[str]:
return self.device.deviceid

@property
def name(self) -> Optional[str]:
return self._name

@property
def supported_features(self):
return 0

@property
def state_attributes(self):
return self._attrs

@property
def is_on(self):
return False
9 changes: 9 additions & 0 deletions custom_components/sonoff/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
send_command:
description: Sends a command to a device.
fields:
device:
description: Device ID to send command to.
example: '1000123456'
command:
description: A single command to send.
example: 'switch'

0 comments on commit 452a5cc

Please sign in to comment.