Skip to content

Commit

Permalink
Fix local connection
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jun 3, 2020
1 parent e56b640 commit e35c7ae
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions custom_components/sonoff/sonoff_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,20 @@ def start(self, handlers: List[Callable], devices: dict = None):
def stop(self, *args):
self._zeroconf.close()

def _zeroconf_handler(self, zeroconf: Zeroconf, ztype: str, zname: str,
zstate: ServiceStateChange):
if zstate == ServiceStateChange.Removed:
_LOGGER.debug(f"Zeroconf Removed: {zname}")
def _zeroconf_handler(self, zeroconf: Zeroconf, service_type: str,
name: str, state_change: ServiceStateChange):
if state_change == ServiceStateChange.Removed:
_LOGGER.debug(f"Zeroconf Removed: {name}")
# TTL of record 5 minutes
deviceid = zname[8:18]
deviceid = name[8:18]
# _LOGGER.debug(f"{deviceid} <= Local2 | Zeroconf Removed Event")
# check if device added
if 'handlers' in self._devices[deviceid]:
coro = self.check_offline(deviceid)
self.loop.create_task(coro)
return

info = zeroconf.get_service_info(ztype, zname)
info = zeroconf.get_service_info(service_type, name)
properties = {
k.decode(): v.decode() if isinstance(v, bytes) else v
for k, v in info.properties.items()
Expand All @@ -119,13 +119,14 @@ def _zeroconf_handler(self, zeroconf: Zeroconf, ztype: str, zname: str,
deviceid = properties['id']
device = self._devices.setdefault(deviceid, {})

log = f"{deviceid} <= Local{state_change.value}"

if properties.get('encrypt'):
devicekey = device.get('devicekey')
if devicekey == 'skip':
return
if not devicekey:
_LOGGER.info(f"{deviceid} <= Local{zstate.value} | "
f"No devicekey for device")
_LOGGER.info(f"{log} | No devicekey for device")
# skip device next time
device['devicekey'] = 'skip'
return
Expand All @@ -141,7 +142,7 @@ def _zeroconf_handler(self, zeroconf: Zeroconf, ztype: str, zname: str,
state = json.loads(data)
seq = properties.get('seq')

_LOGGER.debug(f"{deviceid} <= Local{zstate.value} | {state} | {seq}")
_LOGGER.debug(f"{log} | {state} | {seq}")

host = str(ipaddress.ip_address(info.addresses[0]))
# update every time device host change (alsow first time)
Expand Down

0 comments on commit e35c7ae

Please sign in to comment.