Add Nuki battery percentage sensor (#84968)
* Nuki: add battery percentage + add to device registry * Remove unused import * Fixing linting and sorting issues * Update homeassistant/components/nuki/sensor.py Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com> * Shorthand for adding entities * Use _attr_has_entity_name for battery sensor * Fix linting issue * Remove device registry changes * Exclude from coverage * Use _attr_ instead of properties * Clean up Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
30c9f4f926
commit
fc00c6d885
3 changed files with 50 additions and 1 deletions
|
@ -870,6 +870,7 @@ omit =
|
||||||
homeassistant/components/nuki/binary_sensor.py
|
homeassistant/components/nuki/binary_sensor.py
|
||||||
homeassistant/components/nuki/const.py
|
homeassistant/components/nuki/const.py
|
||||||
homeassistant/components/nuki/lock.py
|
homeassistant/components/nuki/lock.py
|
||||||
|
homeassistant/components/nuki/sensor.py
|
||||||
homeassistant/components/nut/diagnostics.py
|
homeassistant/components/nut/diagnostics.py
|
||||||
homeassistant/components/nx584/alarm_control_panel.py
|
homeassistant/components/nx584/alarm_control_panel.py
|
||||||
homeassistant/components/nzbget/coordinator.py
|
homeassistant/components/nzbget/coordinator.py
|
||||||
|
|
|
@ -33,7 +33,7 @@ from .helpers import parse_id
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.LOCK]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.LOCK, Platform.SENSOR]
|
||||||
UPDATE_INTERVAL = timedelta(seconds=30)
|
UPDATE_INTERVAL = timedelta(seconds=30)
|
||||||
|
|
||||||
|
|
||||||
|
|
48
homeassistant/components/nuki/sensor.py
Normal file
48
homeassistant/components/nuki/sensor.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
"""Battery sensor for the Nuki Lock."""
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import PERCENTAGE
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import NukiEntity
|
||||||
|
from .const import ATTR_NUKI_ID, DATA_COORDINATOR, DATA_LOCKS, DOMAIN as NUKI_DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up the Nuki lock sensor."""
|
||||||
|
data = hass.data[NUKI_DOMAIN][entry.entry_id]
|
||||||
|
coordinator = data[DATA_COORDINATOR]
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
NukiBatterySensor(coordinator, lock) for lock in data[DATA_LOCKS]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NukiBatterySensor(NukiEntity, SensorEntity):
|
||||||
|
"""Representation of a Nuki Lock Battery sensor."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
_attr_name = "Battery"
|
||||||
|
_attr_native_unit_of_measurement = PERCENTAGE
|
||||||
|
_attr_device_class = SensorDeviceClass.BATTERY
|
||||||
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self) -> str:
|
||||||
|
"""Return a unique ID."""
|
||||||
|
return f"{self._nuki_device.nuki_id}_battery_level"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def extra_state_attributes(self):
|
||||||
|
"""Return the device specific state attributes."""
|
||||||
|
return {ATTR_NUKI_ID: self._nuki_device.nuki_id}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> float:
|
||||||
|
"""Return the state of the sensor."""
|
||||||
|
return self._nuki_device.battery_charge
|
Loading…
Add table
Add a link
Reference in a new issue