Remove duplicate code in nextcloud (#89681)

pull/89697/head
epenet 2023-03-14 12:14:29 +01:00 committed by GitHub
parent ec1b8b616f
commit 2809a686be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 56 deletions

View File

@ -6,7 +6,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
from .const import DOMAIN
from .entity import NextcloudEntity
BINARY_SENSORS = (
"nextcloud_system_enable_avatars",
@ -32,34 +33,10 @@ def setup_platform(
add_entities(binary_sensors, True)
class NextcloudBinarySensor(BinarySensorEntity):
class NextcloudBinarySensor(NextcloudEntity, BinarySensorEntity):
"""Represents a Nextcloud binary sensor."""
def __init__(self, item):
"""Initialize the Nextcloud binary sensor."""
self._name = item
self._is_on = None
@property
def icon(self):
"""Return the icon for this binary sensor."""
return "mdi:cloud"
@property
def name(self):
"""Return the name for this binary sensor."""
return self._name
@property
def is_on(self):
def is_on(self) -> bool:
"""Return true if the binary sensor is on."""
return self._is_on == "yes"
@property
def unique_id(self):
"""Return the unique ID for this binary sensor."""
return f"{self.hass.data[DOMAIN]['instance']}#{self._name}"
def update(self) -> None:
"""Update the binary sensor."""
self._is_on = self.hass.data[DOMAIN][self._name]
return self._state == "yes"

View File

@ -0,0 +1,26 @@
"""Base entity for the Nextcloud integration."""
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import StateType
from .const import DOMAIN
class NextcloudEntity(Entity):
"""Base Nextcloud entity."""
_attr_icon = "mdi:cloud"
def __init__(self, item: str) -> None:
"""Initialize the Nextcloud entity."""
self._attr_name = item
self.item = item
self._state: StateType = None
@property
def unique_id(self):
"""Return the unique ID for this sensor."""
return f"{self.hass.data[DOMAIN]['instance']}#{self.item}"
def update(self) -> None:
"""Update the sensor."""
self._state = self.hass.data[DOMAIN][self.item]

View File

@ -4,9 +4,10 @@ from __future__ import annotations
from homeassistant.components.sensor import SensorEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
from . import DOMAIN
from .const import DOMAIN
from .entity import NextcloudEntity
SENSORS = (
"nextcloud_system_version",
@ -71,34 +72,10 @@ def setup_platform(
add_entities(sensors, True)
class NextcloudSensor(SensorEntity):
class NextcloudSensor(NextcloudEntity, SensorEntity):
"""Represents a Nextcloud sensor."""
def __init__(self, item):
"""Initialize the Nextcloud sensor."""
self._name = item
self._state = None
@property
def icon(self):
"""Return the icon for this sensor."""
return "mdi:cloud"
@property
def name(self):
"""Return the name for this sensor."""
return self._name
@property
def native_value(self):
def native_value(self) -> StateType:
"""Return the state for this sensor."""
return self._state
@property
def unique_id(self):
"""Return the unique ID for this sensor."""
return f"{self.hass.data[DOMAIN]['instance']}#{self._name}"
def update(self) -> None:
"""Update the sensor."""
self._state = self.hass.data[DOMAIN][self._name]