Git Product home page Git Product logo

py-xiaomi-home's Introduction

xiaomi-home

Pythonic bindings for Xiaomi Smart Home suite

Basic Usage

Discover gateways and subdevices by sending read commands.

from mihome.base import XiaomiConnection
from mihome.gateway import Gateway

conn = XiaomiConnection()
gateway_data = conn.whois()

gateway = Gateway(
	connection=conn,
	sid=gateway_data['sid'],
	ip=gateway_data['ip'],
	port=gateway_data['port']
)
gateway.register_subdevices()

Listen to any devices events

def switch_cb(item):
	# publish to mqtt / kafka / redis
	print item

for switch in gateway.connected_devices['switch']:
	switch.listen(callback=switch_cb)

Read device status by sending read and getting read_ack

plug = gateway.connected_devices['plug'][0]
print plug.read()

Write to device by sending write and getting write_ack Make sure to export password as env var before using writes

$ export XIAOMI_PASSWORD=password
plug = gateway.connected_devices['plug'][0]
print plug.write({'status': 'on'})

Turn on party mode for Gateway Light

from time import sleep
import random
alpha = (
	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 
	"A", "B", "C", "D", "E", "F"
)

while True:
	color = '99%s' % ''.join([random.choice(alpha) for _ in range(6)])
	gateway.write(
		{'rgb': int(color, 16)},
		ack=False
	)
	sleep(0.2)

Save all devices to config

from mihome.config_manager import YamlConfig

config = YamlConfig([gateway])
config.save()

Load config to get all devices This saves you time from doing discovery everytime you start up the code

from mihome.config_manager import YamlConfig

config = YamlConfig()
gateways = config.load(conn, Gateway)
for gateway in gateways:
	gateway.register_subdevices()
	print gateway.connected_devices

py-xiaomi-home's People

Contributors

geeknam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

py-xiaomi-home's Issues

Listen to multiple devices in parallel

Hi there,

first I'd like to thank you for your efforts in making these very handy python scripts.
Since you use a "while" loop for listening to the connection with the filters for one single device it is currently not possible to register a defined list of devices which where filtered and triggered in parallel.

I just cloned the stream listener from the connection class and hacked it into my frist "bridge script" for MQTT. I just disabled the filtering so all the messages (also "hearbeats") were passed to MQTT.

If you are going to extend you project, this kind of feature would be really appreciated.

Thanks,
Erik

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.