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:
Jack Boswell 2022-09-28 19:46:13 +13:00 committed by GitHub
parent c38b1e7727
commit 3d3aa824b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 20 deletions

View file

@ -2,5 +2,4 @@
from homeassistant.const import Platform from homeassistant.const import Platform
DOMAIN = "trend"
PLATFORMS = [Platform.BINARY_SENSOR] PLATFORMS = [Platform.BINARY_SENSOR]

View file

@ -30,26 +30,26 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import generate_entity_id from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_state_change_event 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.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.dt import utcnow 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__) _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( SENSOR_SCHEMA = vol.Schema(
{ {
vol.Required(CONF_ENTITY_ID): cv.entity_id, 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, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the trend sensors.""" """Set up the trend sensors."""
setup_reload_service(hass, DOMAIN, PLATFORMS) await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
sensors = [] sensors = []
@ -106,7 +106,8 @@ def setup_platform(
if not sensors: if not sensors:
_LOGGER.error("No sensors added") _LOGGER.error("No sensors added")
return return
add_entities(sensors)
async_add_entities(sensors)
class SensorTrend(BinarySensorEntity): class SensorTrend(BinarySensorEntity):

View 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"

View file

@ -3,7 +3,7 @@ from datetime import timedelta
from unittest.mock import patch from unittest.mock import patch
from homeassistant import config as hass_config, setup 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 from homeassistant.const import SERVICE_RELOAD, STATE_UNKNOWN
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util