mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Use attr** in linux-battery (#61883)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
6778e4058e
commit
98e1b7c95d
@ -5,8 +5,12 @@ import os
|
|||||||
from batinfo import Batteries
|
from batinfo import Batteries
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
from homeassistant.const import ATTR_NAME, CONF_NAME, DEVICE_CLASS_BATTERY, PERCENTAGE
|
PLATFORM_SCHEMA,
|
||||||
|
SensorDeviceClass,
|
||||||
|
SensorEntity,
|
||||||
|
)
|
||||||
|
from homeassistant.const import ATTR_NAME, CONF_NAME, PERCENTAGE
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -70,35 +74,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class LinuxBatterySensor(SensorEntity):
|
class LinuxBatterySensor(SensorEntity):
|
||||||
"""Representation of a Linux Battery sensor."""
|
"""Representation of a Linux Battery sensor."""
|
||||||
|
|
||||||
|
_attr_device_class = SensorDeviceClass.BATTERY
|
||||||
|
_attr_native_unit_of_measurement = PERCENTAGE
|
||||||
|
|
||||||
def __init__(self, name, battery_id, system):
|
def __init__(self, name, battery_id, system):
|
||||||
"""Initialize the battery sensor."""
|
"""Initialize the battery sensor."""
|
||||||
self._battery = Batteries()
|
self._battery = Batteries()
|
||||||
|
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
self._battery_stat = None
|
self._battery_stat = None
|
||||||
self._battery_id = battery_id - 1
|
self._battery_id = battery_id - 1
|
||||||
self._system = system
|
self._system = system
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class of the sensor."""
|
|
||||||
return DEVICE_CLASS_BATTERY
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._battery_stat.capacity
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit the value is expressed in."""
|
|
||||||
return PERCENTAGE
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes of the sensor."""
|
"""Return the state attributes of the sensor."""
|
||||||
@ -131,3 +118,4 @@ class LinuxBatterySensor(SensorEntity):
|
|||||||
"""Get the latest data and updates the states."""
|
"""Get the latest data and updates the states."""
|
||||||
self._battery.update()
|
self._battery.update()
|
||||||
self._battery_stat = self._battery.stat[self._battery_id]
|
self._battery_stat = self._battery.stat[self._battery_id]
|
||||||
|
self._attr_native_value = self._battery_stat.capacity
|
||||||
|
Loading…
x
Reference in New Issue
Block a user