Skip to main content

Python binding for FHEM to support Python modules

Project description

Download python Version LastCommit BuyCoffee

fhempy (BETA)

fhempy allows the usage of Python 3 (NOT 2!) language to write FHEM modules. Python 3.7 or higher is required, therefore I recommend using buster.

This repository includes following working modules:

Module Description
ble_presence Presence detection incl. RSSI for Bluetooth Low Energy
ble_reset Resets all Bluetooth interfaces every X hours
bt_presence Presence detection incl. RSSI for Bluetooth
discover_mdns Discover mDNS (e.g. googlecast) devices
discover_ble Discover Bluetooth LE devices
discover_upnp Discover UPnP devices
dlna_dmr Control DLNA MediaRenderer devices
eq3bt Control EQ3 Bluetooth thermostat
erelax_vaillant Control eRelax Vaillant
esphome Installs and starts the ESP Home dashboard for easy ESP Home device management
gfprobt Control GF Pro Bluetooth irrigation control
googlecast Control Cast devices and stream Spotify
helloworld Hello World example for developers to start writing their own module
meross Control Meross devices
miflora Xiaomi BLE Plant Sensor
miio Control Xiaomi WiFi devices
mitemp Xiaomi BLE Temperature/Humidity Sensor
nespresso_ble Nespresso Bluetooth coffee machine
object_detection TensorFlow Lite object detection
ring Ring doorbell/chime/cam
seatconnect Control your Seat/Cupra car
spotify Control Spotify Connect and use FHEM as Spotify Connect player
skodaconnect Control your skoda car
tuya Not recommended: Control tuya devices via local key
tuya_cloud Recommended: Control all tuya devices via cloud incl. real-time updates
warema Control Warema devices
wienerlinien Wiener Linien departure times
xiaomi_gateway3 Xiaomi Gateway V3 (only V3!)
xiaomi_tokens Retrieve all Xiaomi Tokens from Cloud

Installation

Python >=3.7 is required, Python 2 won't work!

Console

Debian 11 (Bullseye)

Copy & paste this command if you are running Debian Bullseye.

sudo apt install python3 python3-pip python3-dev libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libglib2.0-dev libdbus-1-dev bluez libbluetooth-dev git libprotocol-websocket-perl

Others

Use the following 2 commands if you run Debian 10 (Buster) or older (e.g. stretch, jessie, ...).

sudo apt install python3 python3-pip python3-dev libffi-dev libssl-dev libjpeg-dev zlib1g-dev autoconf build-essential libglib2.0-dev libdbus-1-dev bluez libbluetooth-dev git
sudo cpan Protocol::WebSocket

FHEM

update add https://raw.githubusercontent.com/dominikkarall/fhempy/master/controls_pythonbinding.txt
update
define local_pybinding BindingsIo fhempy

All further requirements are installed automatically via pip as soon as the specific module is used the first time.

Usage in FHEM (examples)

This are just a few examples for some modules, please see the modules readme linked in the table above for more details

  • define castdevice fhempy googlecast "Living Room"
  • define eq3bt fhempy eq3bt 00:11:22:33:44:66:77
  • define upnp fhempy discover_upnp

fhempy peers (e.g. extend Bluetooth range)

fhempy allows to run modules locally (same device as FHEM runs on) or on remote peers. Those remote peers only make sense if you want to extend the range of bluetooth or want to distribute the load of some modules to other more powerfull devices (e.g. video object detection).

Installation (short)

Only on remote peers, do not run this commands on the FHEM instance.

pip3 install --upgrade fhempy
# systemd service installation
curl -sL https://raw.githubusercontent.com/dominikkarall/fhempy/master/install_systemd_fhempy.sh | sudo -E bash -

Installation (long)

The following steps are only needed if you want to install fhempy on a remote peer, you should not run them on your FHEM installation.

  • Install fhempy with user pi: pip3 install --upgrade fhempy
  • Make sure your main fhempy instance (within FHEM) is running
  • Test fhempy by just running it with user pi, type fhempy and enter. Wait a few seconds until it gets discovered and you see the incoming FHEM connection.
  • Systemd configuration for autostart
    • curl -sL https://raw.githubusercontent.com/dominikkarall/fhempy/master/install_systemd_fhempy.sh | sudo -E bash -
    • fhempy is run with user pi, you can change that in the fhempy.service file in /etc/systemd/system/
  • FHEM configuration
    • The remote peer is autodiscovered and will show up in FHEM as device e.g. fhempy_remote_192_168_1_50
    • You can move any device to the remote peer by changing the IODev of the device.
    • If autodiscovery doesn't work (it's based on zeroconf), you can define it with define fhempy_remote_IP BindingsIo IP:15733 fhempy

Log file

journalctl -u fhempy.service -f

Update

Just do set remote_pybinding update and the remote peer will install the new package via pip and restart afterwads.

Functionality

10_BindingsIo

This module is a DevIo device which builds a language neutral communicaton bridge in JSON via websockets.

10_fhempyServer

This module just starts the fhempy server instance

10_fhempy

This module is used as the bridge to BindingsIo. It calls BindingsIo with IOWrite.

fhempy

This is the Python server instance which handles JSON websocket messages from BindingsIo. Based on the message it executes the proper function and replies to BindingsIo via websocket.

Call flow

This example shows how Define function is called from the Python module.

  1. define castdevice fhempy googlecast "Living Room"
  2. fhempy sends IOWrite to BindingsIo
  3. BindingsIo sends a JSON websocket message to fhempy
  4. fhempy loads the corresponding module (e.g. googlecast), creates an instance of the object (e.g. googlecast) and calls the Define function on that instance
  5. Define function is executed within the Python context, as long as the function is executed, FHEM waits for the answer the same way as it does for Perl modules
  6. Python Define returns the result via JSON via websocket to BindingsIo

At any time within the functions FHEM functons like readingsSingleUpdate(...) can be called by using the fhem.py module (fhem.readingsSingleUpdate(...)). There are just a few functions supported at the moment.

Flow Chart

Write your own module

Check helloworld example for writing an own module. Be aware that no function which is called from FHEM is allowed to run longer than 1s. In general no blocking code should be used with asyncio. If you want to call blocking code, use run_in_executor (see googlecast code).

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fhempy-0.1.162.tar.gz (4.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fhempy-0.1.162-py3-none-any.whl (4.6 MB view details)

Uploaded Python 3

File details

Details for the file fhempy-0.1.162.tar.gz.

File metadata

  • Download URL: fhempy-0.1.162.tar.gz
  • Upload date:
  • Size: 4.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for fhempy-0.1.162.tar.gz
Algorithm Hash digest
SHA256 d25b7c83f74efa33b29df003c1a7c3d012f43aab63470b959ba3a0cf25c5b8c8
MD5 ff9fa8601b0f16f7094cb12573408423
BLAKE2b-256 b7b8f0b1ec3fbfd7b40ea075d0624a5ecbd4778c3023347d55d985c9a9db4779

See more details on using hashes here.

File details

Details for the file fhempy-0.1.162-py3-none-any.whl.

File metadata

  • Download URL: fhempy-0.1.162-py3-none-any.whl
  • Upload date:
  • Size: 4.6 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for fhempy-0.1.162-py3-none-any.whl
Algorithm Hash digest
SHA256 db454ba6396868e2d17b9bfb1e36e70e067b87a501235e4ea9c718b82ff5666d
MD5 30ebbaa6ebcd82157c1b900226bd706e
BLAKE2b-256 fdca0975a7af2f582361d205a77271a3622e3ae4cf087612aed5f6d3b78ac0a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page