Initial commit
This commit is contained in:
63
home_assistant.py
Normal file
63
home_assistant.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
Home Assistant API client for Matrix64 LED display.
|
||||
"""
|
||||
import requests
|
||||
from config import (
|
||||
HA_TOKEN, HASS_URL,
|
||||
BRIGHTNESS_ENTITY_ID, WEATHER_ENTITY_ID,
|
||||
INTERIOR_TEMP_ENTITY_ID, INTERIOR_HUMIDITY_ENTITY_ID
|
||||
)
|
||||
|
||||
|
||||
def get_entity_value(entity_id):
|
||||
"""Fetch entity state from Home Assistant."""
|
||||
headers = {
|
||||
"Authorization": f"Bearer {HA_TOKEN}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
url = f"{HASS_URL}/api/states/{entity_id}"
|
||||
try:
|
||||
response = requests.get(url, headers=headers, timeout=10)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
print(f"Failed to retrieve entity {entity_id}: {response.text}")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Error fetching {entity_id}: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def get_weather():
|
||||
"""Get weather attributes from Home Assistant."""
|
||||
weather = get_entity_value(WEATHER_ENTITY_ID)
|
||||
if weather:
|
||||
return weather.get("attributes", {})
|
||||
return {}
|
||||
|
||||
|
||||
def get_weather_description():
|
||||
"""Get current weather state/condition."""
|
||||
weather = get_entity_value(WEATHER_ENTITY_ID)
|
||||
if weather:
|
||||
return weather.get("state", "unknown")
|
||||
return "unknown"
|
||||
|
||||
|
||||
def get_interior_weather():
|
||||
"""Get interior temperature and humidity from BTH01-3132 sensor."""
|
||||
temp_data = get_entity_value(INTERIOR_TEMP_ENTITY_ID)
|
||||
humidity_data = get_entity_value(INTERIOR_HUMIDITY_ENTITY_ID)
|
||||
|
||||
temperature = temp_data.get("state") if temp_data else None
|
||||
humidity = humidity_data.get("state") if humidity_data else None
|
||||
|
||||
return {"humidity": humidity, "temperature": temperature}
|
||||
|
||||
|
||||
def get_brightness():
|
||||
"""Get screen brightness value."""
|
||||
brightness = get_entity_value(BRIGHTNESS_ENTITY_ID)
|
||||
if brightness:
|
||||
return brightness.get("state")
|
||||
return None
|
||||
Reference in New Issue
Block a user