Created
July 18, 2019 13:35
-
-
Save dlashua/3cea23ea7022e934a13ad005ad46fb9d to your computer and use it in GitHub Desktop.
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
import appdaemon.plugins.hass.hassapi as hass | |
import datetime | |
# YAML CONFIG | |
# battery: | |
# module: battery | |
# class: Battery | |
# threshold: 35 | |
# always_send: False | |
# th: | |
# zwave.front_deadbolt: 85 | |
class Battery(hass.Hass): | |
def initialize(self): | |
time = datetime.time(5, 0, 0) | |
self.run_daily(self.check_batteries_cbt, time) | |
self.listen_event(self.check_batteries_cbe, "check_batteries") | |
self.th = self.args.get('th', {}) | |
self.check_batteries({}) | |
def check_batteries_cbe(self, event_name, data, kwargs): | |
self.check_batteries(force=True) | |
def check_batteries_cbt(self, kwargs): | |
self.check_batteries() | |
def get_battery_level(self, device): | |
if "entity_id" not in device: | |
return (False, None) | |
entity_id = device['entity_id'] | |
if "attributes" not in device: | |
return (False, None) | |
battery = None | |
if ( | |
"device_class" in device["attributes"] | |
and device["attributes"]["device_class"] == "battery" | |
): | |
battery = int(device["state"]) | |
elif "battery" in device["attributes"]: | |
try: | |
battery_int = int(device["attributes"]["battery"]) | |
battery = battery_int | |
except (ValueError, TypeError): | |
if device["attributes"]["battery"] == "OK": | |
battery = 90 | |
else: | |
battery = 1 | |
elif "battery_level" in device["attributes"]: | |
battery = device["attributes"]["battery_level"] | |
elif "battery_low" in device["attributes"]: | |
if int(device["attributes"]["battery_low"]) != 0: | |
battery = 1 | |
else: | |
battery = 90 | |
low = False | |
if battery is not None: | |
th = int(self.args["threshold"]) | |
if entity_id in self.th: | |
th = int(self.th[entity_id]) | |
if battery < th: | |
low = True | |
return (low, battery) | |
def check_batteries(self, force=False): | |
devices = self.get_state() | |
values = {} | |
low = [] | |
for device in devices: | |
self.log(f"Checking Device {device}") | |
if devices[device] is not None: | |
(lowdevice, battery) = self.get_battery_level(devices[device]) | |
if battery is not None: | |
values[device] = battery | |
if lowdevice: | |
low.append(device) | |
message = "Battery Level Report\n\n" | |
if low: | |
message += "The following devices are low:\n\n" | |
for device in low: | |
message = "{}{} (= {})\n".format( | |
message, self.friendly_name(device), values[device]) | |
message += "\n\n" | |
message += "Battery Levels:\n\n" | |
for device in sorted(values, key=values.get): | |
message += "{}: {}\n".format( | |
self.friendly_name(device), values[device]) | |
if ( | |
low | |
or (self.args.get('always_send', False) is True) | |
or (force is True) | |
): | |
self.call_service( | |
"persistent_notification/create", | |
title="Battery Notification", | |
message=message, | |
notification_id="battery_check" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment