Last active
February 23, 2024 00:47
-
-
Save fpletz/d071c72e45d17ba274fd61ca7a465033 to your computer and use it in GitHub Desktop.
aioesphomeapi minimal examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import aioesphomeapi | |
import asyncio | |
async def main(): | |
loop = asyncio.get_running_loop() | |
cli = aioesphomeapi.APIClient(loop, "foobarhostname", 6053, "foobarpassword") | |
await cli.connect(login=True) | |
def cb(state): | |
#if type(state) == aioesphomeapi.BinarySensorState: | |
print(state) | |
await cli.subscribe_states(cb) | |
l = await cli.list_entities_services() | |
print(l) | |
loop = asyncio.get_event_loop() | |
try: | |
asyncio.ensure_future(main()) | |
loop.run_forever() | |
except KeyboardInterrupt: | |
pass | |
finally: | |
loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import aioesphomeapi | |
import asyncio | |
loop = asyncio.new_event_loop() | |
cli = aioesphomeapi.APIClient(loop, "foobarhostname", 6053, "foobarpassword") | |
loop.run_until_complete(cli.connect(login=True)) | |
def cb(state): | |
print(state) | |
loop.run_until_complete(cli.subscribe_states(cb)) | |
loop.run_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import aioesphomeapi | |
import asyncio | |
from influxdb import InfluxDBClient | |
from influxdb import SeriesHelper | |
influxclient = InfluxDBClient('influxhost', 8086, database="dbname") | |
class EspSeries(SeriesHelper): | |
class Meta: | |
client = influxclient | |
series_name = 'events.stats.{sensor}' | |
fields = [ 'value' ] | |
tags = [ 'sensor' ] | |
bulk_size = 1 | |
autocommit = True | |
async def main(): | |
loop = asyncio.get_running_loop() | |
cli = aioesphomeapi.APIClient(loop, "foobarhostname", 6053, "foobarpassword") | |
await cli.connect(login=True) | |
sensors, services = await cli.list_entities_services() | |
sensor_by_keys = dict((sensor.key, sensor.name) for sensor in sensors) | |
def cb(state): | |
if type(state) == aioesphomeapi.SensorState: | |
print(state) | |
print(sensor_by_keys[state.key]) | |
print(state.state) | |
EspSeries(sensor=sensor_by_keys[state.key], value=state.state) | |
await cli.subscribe_states(cb) | |
loop = asyncio.get_event_loop() | |
try: | |
asyncio.ensure_future(main()) | |
loop.run_forever() | |
except KeyboardInterrupt: | |
pass | |
finally: | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I've got an LED setup that presents as follows, I want to be able to turn on/off certain LED's and set their colour, would you be able to provide me an example?