Skip to content

Instantly share code, notes, and snippets.

@fpletz
Last active February 23, 2024 00:47
Show Gist options
  • Save fpletz/d071c72e45d17ba274fd61ca7a465033 to your computer and use it in GitHub Desktop.
Save fpletz/d071c72e45d17ba274fd61ca7a465033 to your computer and use it in GitHub Desktop.
aioesphomeapi minimal examples
#!/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()
#!/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()
#!/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()
@klattimer
Copy link

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?

APIVersion(major=1, minor=6)
DeviceInfo(uses_password=False, name='status-indicator', mac_address='84:F3:EB:B1:20:C5', compilation_time='Dec 19 2021, 12:24:39', model='PLATFORMIO_ESP01', has_deep_sleep=False, esphome_version='2021.8.2', project_name='', project_version='', webserver_port=0)
([LightInfo(object_id='led1', key=4265521425, name='LED1', unique_id='status-indicatorlightled1', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led2', key=4265521426, name='LED2', unique_id='status-indicatorlightled2', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led3', key=4265521427, name='LED3', unique_id='status-indicatorlightled3', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led4', key=4265521428, name='LED4', unique_id='status-indicatorlightled4', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led5', key=4265521429, name='LED5', unique_id='status-indicatorlightled5', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led6', key=4265521430, name='LED6', unique_id='status-indicatorlightled6', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led7', key=4265521431, name='LED7', unique_id='status-indicatorlightled7', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led8', key=4265521432, name='LED8', unique_id='status-indicatorlightled8', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led9', key=4265521433, name='LED9', unique_id='status-indicatorlightled9', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led10', key=1303428595, name='LED10', unique_id='status-indicatorlightled10', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led11', key=1303428594, name='LED11', unique_id='status-indicatorlightled11', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led12', key=1303428593, name='LED12', unique_id='status-indicatorlightled12', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led13', key=1303428592, name='LED13', unique_id='status-indicatorlightled13', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led14', key=1303428599, name='LED14', unique_id='status-indicatorlightled14', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led15', key=1303428598, name='LED15', unique_id='status-indicatorlightled15', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False), LightInfo(object_id='led16', key=1303428597, name='LED16', unique_id='status-indicatorlightled16', disabled_by_default=False, icon='', entity_category=<EntityCategory.NONE: 0>, supported_color_modes=[35], min_mireds=0.0, max_mireds=0.0, effects=[], legacy_supports_brightness=True, legacy_supports_rgb=True, legacy_supports_white_value=False, legacy_supports_color_temperature=False)], [])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment