Refactor Trend to use async_setup_platform
(#78216)
* Move trend constants to const.py * Migrate to async_setup_platform * Fix test * Reorder attrs
This commit is contained in:
parent
c38b1e7727
commit
3d3aa824b3
4 changed files with 34 additions and 20 deletions
|
@ -2,5 +2,4 @@
|
|||
|
||||
from homeassistant.const import Platform
|
||||
|
||||
DOMAIN = "trend"
|
||||
PLATFORMS = [Platform.BINARY_SENSOR]
|
||||
|
|
|
@ -30,26 +30,26 @@ import homeassistant.helpers.config_validation as cv
|
|||
from homeassistant.helpers.entity import generate_entity_id
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.event import async_track_state_change_event
|
||||
from homeassistant.helpers.reload import setup_reload_service
|
||||
from homeassistant.helpers.reload import async_setup_reload_service
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from . import DOMAIN, PLATFORMS
|
||||
from . import PLATFORMS
|
||||
from .const import (
|
||||
ATTR_GRADIENT,
|
||||
ATTR_INVERT,
|
||||
ATTR_MIN_GRADIENT,
|
||||
ATTR_SAMPLE_COUNT,
|
||||
ATTR_SAMPLE_DURATION,
|
||||
CONF_INVERT,
|
||||
CONF_MAX_SAMPLES,
|
||||
CONF_MIN_GRADIENT,
|
||||
CONF_SAMPLE_DURATION,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_ATTRIBUTE = "attribute"
|
||||
ATTR_GRADIENT = "gradient"
|
||||
ATTR_MIN_GRADIENT = "min_gradient"
|
||||
ATTR_INVERT = "invert"
|
||||
ATTR_SAMPLE_DURATION = "sample_duration"
|
||||
ATTR_SAMPLE_COUNT = "sample_count"
|
||||
|
||||
CONF_INVERT = "invert"
|
||||
CONF_MAX_SAMPLES = "max_samples"
|
||||
CONF_MIN_GRADIENT = "min_gradient"
|
||||
CONF_SAMPLE_DURATION = "sample_duration"
|
||||
|
||||
SENSOR_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
|
@ -68,14 +68,14 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
def setup_platform(
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the trend sensors."""
|
||||
setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
|
||||
sensors = []
|
||||
|
||||
|
@ -106,7 +106,8 @@ def setup_platform(
|
|||
if not sensors:
|
||||
_LOGGER.error("No sensors added")
|
||||
return
|
||||
add_entities(sensors)
|
||||
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class SensorTrend(BinarySensorEntity):
|
||||
|
|
14
homeassistant/components/trend/const.py
Normal file
14
homeassistant/components/trend/const.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
"""Constant values for Trend integration."""
|
||||
DOMAIN = "trend"
|
||||
|
||||
ATTR_ATTRIBUTE = "attribute"
|
||||
ATTR_GRADIENT = "gradient"
|
||||
ATTR_INVERT = "invert"
|
||||
ATTR_MIN_GRADIENT = "min_gradient"
|
||||
ATTR_SAMPLE_DURATION = "sample_duration"
|
||||
ATTR_SAMPLE_COUNT = "sample_count"
|
||||
|
||||
CONF_INVERT = "invert"
|
||||
CONF_MAX_SAMPLES = "max_samples"
|
||||
CONF_MIN_GRADIENT = "min_gradient"
|
||||
CONF_SAMPLE_DURATION = "sample_duration"
|
|
@ -3,7 +3,7 @@ from datetime import timedelta
|
|||
from unittest.mock import patch
|
||||
|
||||
from homeassistant import config as hass_config, setup
|
||||
from homeassistant.components.trend import DOMAIN
|
||||
from homeassistant.components.trend.const import DOMAIN
|
||||
from homeassistant.const import SERVICE_RELOAD, STATE_UNKNOWN
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue