Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restructure and improve gateway subdevices #700

Merged
merged 35 commits into from
May 27, 2020
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ae6f351
restructure and improve gateway subdevices
starkillerOG May 20, 2020
5f1cf14
Update gateway.py
starkillerOG May 23, 2020
dbf986d
fix cli
starkillerOG May 23, 2020
8ffb765
black formatting
starkillerOG May 23, 2020
692ebfc
filter out gateway
starkillerOG May 23, 2020
c4c0699
better error handeling
starkillerOG May 23, 2020
581dcb3
common GatewayDevice class for __init__
starkillerOG May 23, 2020
27a89d3
remove duplicate "gateway" from method name
starkillerOG May 23, 2020
d18c65f
use device_type instead of device_name for mapping
starkillerOG May 23, 2020
a21e381
add comments
starkillerOG May 23, 2020
064ffe8
use DeviceType.Gateway
starkillerOG May 23, 2020
96efd88
Update gateway.py
starkillerOG May 23, 2020
651664f
improve discovered info
starkillerOG May 23, 2020
25e52e6
fix formatting
starkillerOG May 23, 2020
d4da84e
better use of dev_info
starkillerOG May 23, 2020
b2bb69d
final black formatting
starkillerOG May 23, 2020
194a57a
process revieuw
starkillerOG May 24, 2020
e85b885
generilize properties of subdevices
starkillerOG May 24, 2020
040e2c1
Subdevice schould not derive from Device
starkillerOG May 24, 2020
fc8f8a7
simplify dataclass props for subdevices
starkillerOG May 26, 2020
1f016ab
optimization
starkillerOG May 26, 2020
2b471c2
remove empty checks and futher simplify dataclass
starkillerOG May 26, 2020
2b7e1b7
Update gateway.py
starkillerOG May 26, 2020
9a4eef7
add back SensorHT
starkillerOG May 26, 2020
f73b167
add back Empty response
starkillerOG May 26, 2020
8b84163
black formatting
starkillerOG May 26, 2020
91fb301
add missing docstrings
starkillerOG May 26, 2020
630ade9
fix except
starkillerOG May 26, 2020
f25a726
fix black
starkillerOG May 26, 2020
b6104d1
Update pyproject.toml
starkillerOG May 26, 2020
a114386
Update pyproject.toml
starkillerOG May 26, 2020
e99accb
fix dataclasses
starkillerOG May 26, 2020
eaba358
replace dataclasses by attr
starkillerOG May 27, 2020
0ab1dc7
add get_local_status command
starkillerOG May 27, 2020
0f05432
remove local.status again
starkillerOG May 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions miio/gateway.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from dataclasses import dataclass
from datetime import datetime
from enum import Enum, IntEnum
from typing import Optional
Expand Down Expand Up @@ -54,6 +55,15 @@ class DeviceType(IntEnum):
RemoteSwitchDouble = 135


@dataclass
class SubDeviceInfo:
sid: str
type_id: int
unknown: int
unknown2: int
fw_ver: int


class Gateway(Device):
"""Main class representing the Xiaomi Gateway.

Expand Down Expand Up @@ -145,14 +155,22 @@ def discover_devices(self):
self._devices = []

for x in range(0, len(devices_raw), 5):
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
# Extract discovered information
dev_info = SubDeviceInfo
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
dev_info.sid = devices_raw[x]
dev_info.type_id = devices_raw[x+1]
dev_info.unknown = devices_raw[x+2]
dev_info.unknown2 = devices_raw[x+3]
dev_info.fw_ver = devices_raw[x+4]

# Construct DeviceType
try:
device_type = DeviceType(devices_raw[x + 1])
device_type = DeviceType(dev_info.type_id)
except ValueError:
_LOGGER.warning(
"Unknown subdevice type '%i': %s discovered, of Xiaomi gateway with ip: %s",
devices_raw[x + 1],
devices_raw[x],
dev_info.type_id,
dev_info.sid,
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved
self.ip,
)
device_type = DeviceType(-1)
Expand All @@ -168,7 +186,7 @@ def discover_devices(self):

# Initialize and save the subdevice, ignoring the gateway itself
if device_type != DeviceType.Gateway:
self._devices.append(subdevice_cls(self, *devices_raw[x : x + 5]))
self._devices.append(subdevice_cls(self, dev_info))

return self._devices

Expand Down Expand Up @@ -552,11 +570,7 @@ class SubDevice(Device):
def __init__(
self,
gw: Gateway = None,
sid: str = None,
type: int = None,
_: int = None,
__: int = None,
___: int = None,
dev_info: SubDeviceInfo = None,
ip: str = None,
token: str = None,
start_id: int = 0,
Expand All @@ -571,14 +585,14 @@ def __init__(
"Creating new device instance, only use this for cli interface"
)

if sid is None:
if dev_info is None:
raise Exception("sid of the subdevice needs to be specified")

self._gw = gw
self.sid = sid
self.sid = dev_info.sid
self._battery = None
try:
self.type = DeviceType(type)
self.type = DeviceType(dev_info.type_id)
except ValueError:
self.type = DeviceType(-1)
starkillerOG marked this conversation as resolved.
Show resolved Hide resolved

Expand Down