Skip to content

Commit

Permalink
Fix can_local check for devices with parent #1366
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 17, 2024
1 parent 814cac4 commit 8957566
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
1 change: 0 additions & 1 deletion custom_components/sonoff/core/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,5 +535,4 @@ def setup_diy(device: dict) -> XDevice:
device["name"] = "Unknown DIY"
device["extra"] = {"uiid": 0}
device["productModel"] = ltype
# device["online"] = False
return device
6 changes: 2 additions & 4 deletions custom_components/sonoff/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ def set_state(self, params: dict):
pass

def internal_available(self) -> bool:
device = self.device.get("parent") or self.device
return (self.ewelink.cloud.online and device.get("online")) or (
self.ewelink.local.online and device.get("local")
)
ok = self.ewelink.can_cloud(self.device) or self.ewelink.can_local(self.device)
return ok

def internal_update(self, params: dict = None):
available = self.internal_available()
Expand Down
28 changes: 20 additions & 8 deletions custom_components/sonoff/core/ewelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ async def send(
else:
main_device = device

can_local = self.local.online and main_device.get("local")
can_cloud = self.cloud.online and main_device.get("online")
can_local = self.can_local(device)
can_cloud = self.can_cloud(device)

if can_local and can_cloud:
# try to send a command locally (wait no more than a second)
Expand Down Expand Up @@ -166,7 +166,7 @@ async def send_bulk(self, device: XDevice, params: dict):
return await self.send(device, device.pop("params_bulk"))

async def send_cloud(self, device: XDevice, params: dict = None, query=True):
if not self.cloud.online or not device.get("online"):
if not self.can_cloud(device):
return
ok = await self.cloud.send(device, params)
if ok == "online" and query and params:
Expand Down Expand Up @@ -326,24 +326,24 @@ def update_device(self, device: XDevice):
# [5] POW, [32] POWR2, [182] S40, [190] POWR3 - one channel, only cloud update
# [181] THR316D/THR320D
if uiid in (5, 32, 182, 190, 181):
if self.cloud.online and device.get("online"):
if self.can_cloud(device):
params = {"uiActive": 60}
asyncio.create_task(self.cloud.send(device, params, timeout=0))

# DUALR3 - two channels, local and cloud update
elif uiid == 126:
if self.local.online and device.get("local"):
if self.can_local(device):
# empty params is OK
asyncio.create_task(self.local.send(device, command="statistics"))
elif self.cloud.online and device.get("online"):
elif self.can_cloud(device):
params = {"uiActive": {"all": 1, "time": 60}}
asyncio.create_task(self.cloud.send(device, params, timeout=0))

# SPM-4Relay - four channels, separate update for each channel
elif uiid == 130:
if self.local.online and device.get("local"):
if self.can_local(device):
asyncio.create_task(self.update_spm_pow(device, False))
if self.cloud.online and device.get("online"):
if self.can_cloud(device):
asyncio.create_task(self.update_spm_pow(device, True))

# checks if device still available via LAN
Expand All @@ -362,3 +362,15 @@ async def update_spm_pow(self, device: XDevice, cloud_mode: bool):
await self.cloud.send(device, params, timeout=0)
else:
await self.local.send(device, params, command="statistics")

def can_cloud(self, device: XDevice) -> bool:
if not self.cloud.online:
return False
return device.get("online")

def can_local(self, device: XDevice) -> bool:
if not self.local.online:
return False
if "parent" in device:
return device["parent"].get("local")
return device.get("local")

0 comments on commit 8957566

Please sign in to comment.