Files
ad-nix/apps/hello_world/hello.py
2024-08-27 19:17:02 -05:00

50 lines
1.6 KiB
Python

import asyncio
from appdaemon.adbase import ADBase
from appdaemon.plugins.hass.hassapi import Hass
class MyClass(Hass):
def initialize(self):
return
def my_callback(self, title: str, message: str, **kwargs) -> None:
self.log(f'{title}: {message}')
self.log(kwargs)
class HelloWorld(ADBase):
def initialize(self):
self.adapi = self.get_ad_api()
def my_callback(self, cb_args: dict) -> None:
self.adapi.log(f'{cb_args["title"]}: {cb_args["message"]}')
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