50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
import asyncio
|
|
import json
|
|
|
|
from appdaemon.adapi import ADAPI
|
|
from appdaemon.plugins.hass.hassapi import Hass
|
|
|
|
|
|
class HelloWorld(Hass):
|
|
def initialize(self):
|
|
self.log('Hello World')
|
|
# self.listen_state(
|
|
# callback=self.temp_callback,
|
|
# entity_id='sensor.temperature_nest',
|
|
# attribute='state',
|
|
# threshold=65.0,
|
|
# )
|
|
# self.listen_state(
|
|
# callback=self.temp_callback,
|
|
# entity_id='light.living_room',
|
|
# attribute='state',
|
|
# threshold=65.0,
|
|
# )
|
|
|
|
async def temp_callback(self, entity, attribute, old, new, kwargs):
|
|
self.log('Temp callback')
|
|
temp = await self.get_state('sensor.temperature_nest')
|
|
# self.log(json.dumps(temp, indent=4))
|
|
self.AD.loop.create_task(self.unreliable_call())
|
|
if float(temp) <= kwargs['threshold']:
|
|
self.log(f'{entity} is below the threshold')
|
|
self.AD.loop.create_task(self.unreliable_call())
|
|
|
|
self.log('Doing some other, more reliable stuff')
|
|
await asyncio.sleep(2.0)
|
|
self.log(f'{entity} done')
|
|
|
|
async def unreliable_call(self):
|
|
self.log('Calling unreliable cloud service....')
|
|
await asyncio.sleep(5.0)
|
|
# await self.call_service('climate/set_temperature', entity_id='climate.living_room', temperature=70)
|
|
self.log('Cloud service returned')
|
|
|
|
def entities_ending_with(self, ending_str: str):
|
|
entities = [
|
|
entity_id
|
|
for entity_id, state in self.get_state().items()
|
|
if entity_id.endswith(ending_str)
|
|
]
|
|
return entities
|