From 3d3aa824b339553fd696663bee989ebcffc97219 Mon Sep 17 00:00:00 2001 From: Jack Boswell Date: Wed, 28 Sep 2022 19:46:13 +1300 Subject: [PATCH] Refactor Trend to use `async_setup_platform` (#78216) * Move trend constants to const.py * Migrate to async_setup_platform * Fix test * Reorder attrs --- homeassistant/components/trend/__init__.py | 1 - .../components/trend/binary_sensor.py | 37 ++++++++++--------- homeassistant/components/trend/const.py | 14 +++++++ tests/components/trend/test_binary_sensor.py | 2 +- 4 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 homeassistant/components/trend/const.py diff --git a/homeassistant/components/trend/__init__.py b/homeassistant/components/trend/__init__.py index a56bb00d97b..b583f424da1 100644 --- a/homeassistant/components/trend/__init__.py +++ b/homeassistant/components/trend/__init__.py @@ -2,5 +2,4 @@ from homeassistant.const import Platform -DOMAIN = "trend" PLATFORMS = [Platform.BINARY_SENSOR] diff --git a/homeassistant/components/trend/binary_sensor.py b/homeassistant/components/trend/binary_sensor.py index b98d904cabd..e43032a580f 100644 --- a/homeassistant/components/trend/binary_sensor.py +++ b/homeassistant/components/trend/binary_sensor.py @@ -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): diff --git a/homeassistant/components/trend/const.py b/homeassistant/components/trend/const.py new file mode 100644 index 00000000000..6787dc08445 --- /dev/null +++ b/homeassistant/components/trend/const.py @@ -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" diff --git a/tests/components/trend/test_binary_sensor.py b/tests/components/trend/test_binary_sensor.py index f7ccee97b29..1439ca80e9f 100644 --- a/tests/components/trend/test_binary_sensor.py +++ b/tests/components/trend/test_binary_sensor.py @@ -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