Skip to content

Commit

Permalink
Поддержка температуры и влажности
Browse files Browse the repository at this point in the history
Добавлены логи и поправлен WARNING компонента recorder
  • Loading branch information
AlexxIT committed Nov 19, 2019
1 parent 896a451 commit da58cbb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 10 additions & 4 deletions custom_components/sonoff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@


def setup(hass, config):
devices = config[DOMAIN].get('devices', [])
hass.data[DOMAIN] = devices = config[DOMAIN].get('devices', [])

def add_device(device, data: dict):
info = {'device': device, 'data': data}
def add_device(deviceid: str, data: dict):
info = {'deviceid': deviceid, 'data': data}
load_platform(hass, 'switch', DOMAIN, info, config)

listener = EWeLinkListener(devices)
Expand Down Expand Up @@ -111,6 +111,8 @@ def add_service(self, zeroconf: Zeroconf, type_: str, name: str):
for k, v in info.properties.items()
}

_LOGGER.debug(f"Properties: {properties}")

host = str(ipaddress.ip_address(info.address))
deviceid = properties['id']

Expand All @@ -128,13 +130,14 @@ def add_service(self, zeroconf: Zeroconf, type_: str, name: str):

data = decrypt(properties, apikey)
data = json.loads(data)
_LOGGER.debug(f"Data: {data}")
else:
raise NotImplementedError()

self.devices[deviceid] = device = \
EWeLinkDevice(host, deviceid, config, zeroconf)

self._add_device(device, data)
self._add_device(deviceid, data)

def remove_service(self, zeroconf: Zeroconf, type: str, name: str):
_LOGGER.debug(f"Remove service {name}")
Expand Down Expand Up @@ -179,9 +182,12 @@ def update_service(self, zeroconf: Zeroconf, type_: str, name: str):
for k, v in info.properties.items()
}

_LOGGER.debug(f"Properties: {properties}")

if properties.get('encrypt'):
data = decrypt(properties, self.config['apikey'])
data = json.loads(data)
_LOGGER.debug(f"Data: {data}")
else:
raise NotImplementedError()

Expand Down
20 changes: 18 additions & 2 deletions custom_components/sonoff/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@

from homeassistant.components.switch import SwitchDevice

from . import EWeLinkDevice
from . import DOMAIN, EWeLinkDevice

_LOGGER = logging.getLogger(__name__)

ATTRS = ('humidity', 'temperature')


def setup_platform(hass, config, add_entities, discovery_info=None):
# We only want this platform to be set up via discovery.
if discovery_info is None:
return

# TODO: переписать грамотнее
device: EWeLinkDevice = discovery_info['device']
deviceid = discovery_info['deviceid']
device = hass.data[DOMAIN].get(deviceid)
if not isinstance(device, EWeLinkDevice):
_LOGGER.warning(f"Can't setup {deviceid}")
return

data = discovery_info['data']
if 'switch' in data:
add_entities([SonoffSwitch(device, 0, data)])
Expand All @@ -29,6 +36,7 @@ def __init__(self, device: EWeLinkDevice, channel: int = 0,
initdata: dict = None):
self.device = device
self.channel = channel
self._attrs = {}
self._state = None

if initdata:
Expand All @@ -37,6 +45,10 @@ def __init__(self, device: EWeLinkDevice, channel: int = 0,
device.listen(self._update)

def _update(self, data: dict, schedule_update: bool = True):
for k in ATTRS:
if k in data:
self._attrs[k] = data[k]

self._state = data['switches'][self.channel - 1]['switch'] \
if self.channel else data['switch']

Expand All @@ -62,6 +74,10 @@ def state(self):
"""Return the state of the switch."""
return self._state

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

def turn_on(self, **kwargs) -> None:
"""Turn the entity on."""
if self.channel:
Expand Down

0 comments on commit da58cbb

Please sign in to comment.