pymee
pymee is the backbone of the Home Assistant homee integration.
pymee is an unofficial python library for interacting with the homee smart home/home automation platform. It uses the websockets library to connect to a local homee cube and maintains a list of nodes (devices), attributes, groups, users and more that are updated whenever new data arrives from homee.
Large parts of this library are directly ported from the awesome homeeApi javascript library.
Installation
Install from PyPI:
pip install pymee
Usage
Getting started
pymee should be used with asyncio
:
from pymee import Homee
import asyncio
import logging
logging.getLogger().setLevel(logging.DEBUG)
async def main():
homee = Homee("<HOMEE IP>", "<USERNAME>", "<PASSWORD>")
homeeTask = asyncio.create_task(homee.run())
await homee.wait_until_connected()
homee.disconnect()
await homee.wait_until_disconnected()
asyncio.run(main())
Access devices and attributes
Devices are represented as "nodes" in the api. All nodes are available in the list Homee.nodes
and are represented by the HomeeNode
class.
Each node has a list of attributes accessible from HomeeNode.attributes
. The attributes on a node represent the different attributes on a device, i.e. if a light is turned on or the target temperature of a thermostat. Attributes can be identified by the HomeeAttribute.type
property. You can compare the type with the values from pymee.const.AttributeType
to figure out what each attribute represents. The value can be accessed with the HomeeAttribute.current_value
property.
If you need to change the value of an attribute you can use Homee.set_value()
:
node = homee.get_node_by_id(5)
await homee.set_value(node.id, node.get_attribute_by_type(AttributeType.ON_OFF).id, 1)
Receiving updates
The Homee
class can be inherited to receive events:
class MyHomee(Homee):
async def on_connected(self):
pass
async def on_disconnected(self):
pass
async def on_error(self, error: str):
pass
async def on_message(self, msg: dict):
pass
async def on_attribute_updated(self, attribute_data: dict, node: HomeeNode):
pass
You can also add a listener to specific nodes to receive attribute updates:
def my_node_handler(node: HomeeNode, attribute: HomeeAttribute):
logging.info(f"Attribute {attribute.id} of node {node.name} was updated!")
node = homee.get_node_by_id(5)
remove_listener = node.add_on_changed_listener(my_node_handler)
remove_listener()
To manually request updates from Homee, you can use the following functions:
homee.update_node(self, nodeId: int)
"""Request current data for a node."""
homee.update_attribute(self, nodeId: int, attributeId: int)
"""Request current data for an attribute"""
More examples
Example implementation that dumps all info into a json file and logs whenever a light is turned on or off:
from pymee.const import NodeProfile, AttributeType
from pymee.model import HomeeAttribute
class JsonHomee(Homee):
async def on_message(self, msg: dict):
if list(msg)[0] == "all":
f = open("homee.json", "w")
f.write(json.dumps(msg))
f.close()
async def on_attribute_updated(self, attribute_data, node):
attribute = HomeeAttribute(attribute_data)
if attribute.current_value == attribute.target_value:
return
if (
node.profile == NodeProfile.DIMMABLE_EXTENDED_COLOR_LIGHT
and attribute.type == AttributeType.ON_OFF
):
self._log(
f"[Light] {node.name} turned {'on' if attribute.target_value == 1 else 'off'}"
)
License
MIT