Skip to content

Commit

Permalink
Fix Zigbee Button
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Sep 28, 2020
1 parent 65f5b02 commit adfdc4e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
17 changes: 0 additions & 17 deletions custom_components/sonoff/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ async def async_setup_platform(hass, config, add_entities,
uiid = registry.devices[deviceid].get('uiid')
if uiid == 102:
add_entities([WiFiDoorWindowSensor(registry, deviceid)])
elif uiid == 1000:
add_entities([ZigBeeSwitchSensor(registry, deviceid)])
elif uiid == 2026:
add_entities([ZigBeeMotionSensor(registry, deviceid)])
elif uiid == 3026:
Expand Down Expand Up @@ -111,21 +109,6 @@ def _update_handler(self, state: dict, attrs: dict):
self.schedule_update_ha_state()


class ZigBeeSwitchSensor(EWeLinkBinarySensor):
def _update_handler(self, state: dict, attrs: dict):
self._attrs.update(attrs)

if 'key' in state:
self._is_on = (state['key'] == 1)

self.schedule_update_ha_state()

@property
def available(self) -> bool:
device: dict = self.registry.devices[self.deviceid]
return device['available']


class ZigBeeMotionSensor(EWeLinkBinarySensor):
def _update_handler(self, state: dict, attrs: dict):
self._attrs.update(attrs)
Expand Down
51 changes: 51 additions & 0 deletions custom_components/sonoff/sensor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from typing import Optional

from homeassistant.const import DEVICE_CLASS_TEMPERATURE, \
Expand Down Expand Up @@ -48,6 +49,9 @@ async def async_setup_platform(hass, config, add_entities,
add_entities([EWeLinkSensor(registry, deviceid, attr)
for attr in SONOFF_SC])

elif uiid == 1000:
add_entities([ZigBeeButtonSensor(registry, deviceid)])

elif uiid == 1770:
add_entities([EWeLinkSensor(registry, deviceid, 'temperature'),
EWeLinkSensor(registry, deviceid, 'humidity')])
Expand Down Expand Up @@ -112,3 +116,50 @@ def unit_of_measurement(self):
@property
def icon(self):
return SENSORS[self._attr][2] if self._attr in SENSORS else None


BUTTON_STATES = ['single', 'double', 'hold']


class ZigBeeButtonSensor(EWeLinkDevice, Entity):
_state = ''

async def async_added_to_hass(self) -> None:
# don't call update at startup
self._init(force_refresh=False)

def _update_handler(self, state: dict, attrs: dict):
self._attrs.update(attrs)

if 'key' in state:
self._state = BUTTON_STATES[state['key']]
self.async_write_ha_state()
time.sleep(.5)
self._state = ''

self.schedule_update_ha_state()

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

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

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

@property
def state(self) -> str:
return self._state

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

@property
def available(self) -> bool:
device: dict = self.registry.devices[self.deviceid]
return device['available']
2 changes: 1 addition & 1 deletion custom_components/sonoff/sonoff_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ async def login(self, username: str, password: str) -> bool:
payload = {pname: username, 'password': password}
resp = await self._api('login', 'api/user/login', payload)

if resp.get('error') == 406:
if resp is None or resp.get('error') == 406:
self.init_app_v4()
resp = await self._api('login', 'api/user/login', payload)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/sonoff/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def init_device_class(default_class: str = 'switch'):
102: 'binary_sensor', # Sonoff DW2 Door/Window sensor
104: 'light', # RGB+CCT color bulb
107: switchx,
1000: 'binary_sensor', # zigbee_ON_OFF_SWITCH_1000
1000: 'sensor', # zigbee_ON_OFF_SWITCH_1000
1256: 'light', # ZCL_HA_DEVICEID_ON_OFF_LIGHT
1770: 'sensor', # ZCL_HA_DEVICEID_TEMPERATURE_SENSOR
2026: 'binary_sensor', # ZIGBEE_MOBILE_SENSOR
Expand Down

0 comments on commit adfdc4e

Please sign in to comment.