Source code for toio.cube.api.battery
# -*- coding: utf-8 -*-
# ************************************************************
#
# battery.py
#
# Copyright 2022 Sony Interactive Entertainment Inc.
#
# ************************************************************
import pprint
from typing import Optional
from toio.cube.api.base_class import CubeCharacteristic, CubeResponse
from toio.device_interface import GattReadData
from toio.logger import get_toio_logger
from toio.toio_uuid import TOIO_UUID_BATTERY_INFO
logger = get_toio_logger(__name__)
BatteryResponseType = BatteryInformation
"""
Response type of battery characteristic
"""
[docs]class Battery(CubeCharacteristic):
"""
Battery characteristic
References:
https://toio.github.io/toio-spec/en/docs/ble_battery
"""
[docs] @staticmethod
def is_my_data(payload: GattReadData) -> Optional[BatteryResponseType]:
if BatteryInformation.is_myself(payload):
return BatteryInformation(payload)
else:
return None
[docs] def __init__(self, interface):
super().__init__(interface, TOIO_UUID_BATTERY_INFO)
[docs] async def read(self) -> Optional[BatteryResponseType]:
"""
Read the battery level
Returns:
BatteryInformation or None
(None returns when read fails)
References:
https://toio.github.io/toio-spec/en/docs/ble_battery#read-operations
"""
payload = await self._read()
return self.is_my_data(payload)