def main(): pp = pprint.PrettyPrinter(indent=2) creds = get_login_credentials() power_wall = Powerwall(POWERWALL_HOST) try: power_wall.detect_and_pin_version() except PowerwallUnreachableError as e: print(e) return print("Detected and pinned version: {}".format( power_wall.get_pinned_version())) _ = power_wall.login(creds["password"]) print("Current charge: {}".format(power_wall.get_charge())) print("Device Type: {}".format(power_wall.get_device_type())) print(f"Authenticated: {power_wall.get_api().is_authenticated()}") print(f"Sitemaster: {power_wall.get_sitemaster()}") print(f"Grid status: {power_wall.get_grid_status()}") print(f"Grid service activer: {power_wall.is_grid_services_active()}") site_info = power_wall.get_site_info() print(f"Site info: {pp.pformat(site_info.response)}") status = power_wall.get_status() print(f"Status: {pp.pformat(status.response)}") print(f"Device type: {power_wall.get_device_type()}") print(f"Serial numbers: {power_wall.get_serial_numbers()}") print(f"Version: {power_wall.get_version()}") # # XXX These methods need auth: print(f"Operation mode: {power_wall.get_operation_mode()}") # print(f"Backup reserved pct: {power_wall.get_backup_reserved_percentage()}") print(f"Solars: {power_wall.get_solars()}") print(f"VIN: {power_wall.get_vin()}") meters = power_wall.get_meters() # print(f"Meters: {pp.pformat(meters.response)}") for meter_type in MeterType: meter = meters.get_meter(meter_type) print(f"Meter: {meter_type}") print(f" Energy exported: {meter.energy_exported}") print(f" Energy imported: {meter.energy_imported}") print(f" Instant power: {meter.instant_power}") print(f" meter info: {pp.pformat(meter)}") # print(f" Instant reactive power: {meter.instant_reactive_power}") # print(f" Instant apparent power: {meter.instant_apparent_power}") # print(f" Instant average voltage: {meter.instant_average_voltage}") # print(f" Instant total current: {meter.instant_total_current}") # print(f" Is Active: {meter.is_active()}") # print(f" Is drawing from: {meter.is_drawing_from()}") # print(f" Is sending to: {meter.is_sending_to()}") return
def main(): pp = pprint.PrettyPrinter(indent=2) creds = get_login_credentials() power_wall = Powerwall(BACKUP_GW_ADDR) try: power_wall.detect_and_pin_version() except PowerwallUnreachableError as e: print(e) return print("Detected and pinned version: {}".format( power_wall.get_pinned_version()))
def test_detect_and_pin_version(self): add( Response(responses.GET, url=f"{ENDPOINT}status", json=STATUS_RESPONSE)) vers = version.LooseVersion("1.50.1") pw = Powerwall(ENDPOINT) self.assertEqual(pw.detect_and_pin_version(), vers) self.assertEqual(pw._pin_version, vers)
def main(): """ Get credentials from vault. Poke backup gateway. Push stats to influxdb """ vault = get_hvac_client() bg_creds = vault.secrets.kv.v1.read_secret(BG_GATEWAY_SECRETS_PATH) influxdb_creds = vault.secrets.kv.v1.read_secret(INFLUXDB_CREDS_PATH) powerwall = Powerwall(BG_GATEWAY_HOST) try: powerwall.detect_and_pin_version() except PowerwallUnreachableError as e: print(e) return _ = powerwall.login(bg_creds["email"], bg_creds["password"]) battery_pct_charge = powerwall.get_charge() site_info = powerwall.get_site_info() meters = power_wall.get_meters() meter_data = meters.response for (meter, data) in meters.items(): pass return
def _login_and_fetch_site_info(power_wall: Powerwall, password: str): """Login to the powerwall and fetch the base info.""" if password is not None: power_wall.login(password) power_wall.detect_and_pin_version() return power_wall.get_site_info()
write_api = client.write_api(write_options=SYNCHRONOUS) # Powerwall ip = os.getenv("POWERWALL_IP") if ip is None: raise ValueError("POWERWALL_IP must be set") email = os.getenv("POWERWALL_EMAIL") password = os.getenv("POWERWALL_PASSWORD") power_wall = Powerwall(ip) # Program # Identify the powerwall version power_wall.detect_and_pin_version() print("Detected and pinned version: {}".format( power_wall.get_pinned_version())) print("Current charge: {}".format(power_wall.get_charge())) print("Device Type: {}".format(power_wall.get_device_type())) # Sending Data while True: p = influxdb_client.Point("Measurement").field("Charge", power_wall.get_charge) write_api.write(bucket=bucket, org=org, record=p) sleep(1)
def draw_plot(i, creds, ax, ax2, x_axis, meter_values, battery_pct): """ Read values from the powerwall and plot them vs time. Keyword Arguments: i -- creds -- x_axis -- meter_values -- battery_pct -- """ powerwall = Powerwall(POWERWALL_HOST) try: powerwall.detect_and_pin_version() except PowerwallUnreachableError as e: print(e) return _ = powerwall.login(creds["password"]) # Get new values.. now = datetime.now(tz=pytz.utc) now = now.astimezone(TIMEZONE) x_axis.append(now) battery_pct.append(powerwall.get_charge()) meters = powerwall.get_meters() for meter_type in MeterType: meter = meters.get_meter(meter_type) meter_type = meter_type.value meter_values[meter_type].append(meter.instant_power) # Truncate at our horizon for number of samples to keep # x_axis = x_axis[-NUM_SAMPLE_HORIZON:] battery_pct = battery_pct[-NUM_SAMPLE_HORIZON:] for meter_type in MeterType: meter_type = meter_type.value meter_values[meter_type] = meter_values[meter_type][ -NUM_SAMPLE_HORIZON:] # Write our file out to both by-date and last 24h files as json # data = { "meter_values": meter_values, "x_axis": [x.strftime(DATE_FMT) for x in x_axis], "battery_pct": battery_pct, } try: with open(LAST_DAY_FILE, "w") as f: json.dump(data, f) today_file = HISTORY_FILE_DIR / now.strftime(HISTORY_FILE_FMT) with open(today_file, "w") as f: json.dump(data, f) except Exception: pass ax.clear() ax2.clear() legend_lines = [] legend_names = [] for meter_type in MeterType: meter_type = meter_type.value (l, ) = ax.plot(x_axis, meter_values[meter_type]) legend_lines.append(l) legend_names.append(meter_type) ax.set_ylabel("Wh") ax.grid(which="major", axis="both", color="grey") (l, ) = ax2.plot(x_axis, battery_pct, color="lightblue", linestyle="dashed") legend_lines.append(l) legend_names.append("Battery % Chg") ax2.set_ylabel("% Chg") ax2.grid(which="major", color="lightblue", linestyle="dotted") hours = mdates.HourLocator(interval=1) h_fmt = mdates.DateFormatter("%H", tz=now.tzinfo) qtr_hr = mdates.MinuteLocator(byminute=[15, 30, 45], interval=1) ax.legend(legend_lines, legend_names, loc="best") ax.xaxis.set_major_formatter(h_fmt) ax.xaxis.set_major_locator(hours) ax.xaxis.set_minor_locator(qtr_hr) plt.title("AS Powerwall")