mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Remove duplicate code in nextcloud (#89681)
This commit is contained in:
parent
ec1b8b616f
commit
2809a686be
@ -6,7 +6,8 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from . import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .entity import NextcloudEntity
|
||||||
|
|
||||||
BINARY_SENSORS = (
|
BINARY_SENSORS = (
|
||||||
"nextcloud_system_enable_avatars",
|
"nextcloud_system_enable_avatars",
|
||||||
@ -32,34 +33,10 @@ def setup_platform(
|
|||||||
add_entities(binary_sensors, True)
|
add_entities(binary_sensors, True)
|
||||||
|
|
||||||
|
|
||||||
class NextcloudBinarySensor(BinarySensorEntity):
|
class NextcloudBinarySensor(NextcloudEntity, BinarySensorEntity):
|
||||||
"""Represents a Nextcloud binary sensor."""
|
"""Represents a Nextcloud binary sensor."""
|
||||||
|
|
||||||
def __init__(self, item):
|
|
||||||
"""Initialize the Nextcloud binary sensor."""
|
|
||||||
self._name = item
|
|
||||||
self._is_on = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def is_on(self) -> bool:
|
||||||
"""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):
|
|
||||||
"""Return true if the binary sensor is on."""
|
"""Return true if the binary sensor is on."""
|
||||||
return self._is_on == "yes"
|
return self._state == "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]
|
|
||||||
|
26
homeassistant/components/nextcloud/entity.py
Normal file
26
homeassistant/components/nextcloud/entity.py
Normal 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]
|
@ -4,9 +4,10 @@ from __future__ import annotations
|
|||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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 = (
|
SENSORS = (
|
||||||
"nextcloud_system_version",
|
"nextcloud_system_version",
|
||||||
@ -71,34 +72,10 @@ def setup_platform(
|
|||||||
add_entities(sensors, True)
|
add_entities(sensors, True)
|
||||||
|
|
||||||
|
|
||||||
class NextcloudSensor(SensorEntity):
|
class NextcloudSensor(NextcloudEntity, SensorEntity):
|
||||||
"""Represents a Nextcloud sensor."""
|
"""Represents a Nextcloud sensor."""
|
||||||
|
|
||||||
def __init__(self, item):
|
|
||||||
"""Initialize the Nextcloud sensor."""
|
|
||||||
self._name = item
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def native_value(self) -> StateType:
|
||||||
"""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):
|
|
||||||
"""Return the state for this sensor."""
|
"""Return the state for this sensor."""
|
||||||
return self._state
|
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]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user