Always setup demo platforms with device support from config entry (#94586)

* Always setup demo platforms with device support from config entry

* Adjust test fixutres

* Update tests depending on the demo integration
This commit is contained in:
Erik Montnemery 2023-06-14 16:50:35 +02:00 committed by GitHub
parent 1b8c72e644
commit 9a3077d64a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
54 changed files with 491 additions and 351 deletions

View File

@ -63,9 +63,6 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the demo environment."""
if DOMAIN not in config:
return True
if not hass.config_entries.async_entries(DOMAIN):
hass.async_create_task(
hass.config_entries.flow.async_init(
@ -73,6 +70,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
)
)
if DOMAIN not in config:
return True
# Set up demo platforms
for platform in COMPONENTS_WITH_DEMO_PLATFORM:
hass.async_create_task(async_load_platform(hass, platform, DOMAIN, {}, config))

View File

@ -9,18 +9,16 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Demo binary sensor platform."""
"""Set up the demo binary sensor platform."""
async_add_entities(
[
DemoBinarySensor(
@ -36,42 +34,30 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoBinarySensor(BinarySensorEntity):
"""representation of a Demo binary sensor."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
state: bool,
device_class: BinarySensorDeviceClass,
) -> None:
"""Initialize the demo sensor."""
self._unique_id = unique_id
self._attr_name = name
self._attr_name = None
self._state = state
self._attr_device_class = device_class
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
self._attr_device_info = DeviceInfo(
identifiers={
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.unique_id)
},
name=self.name,
name=device_name,
)
@property

View File

@ -4,59 +4,47 @@ from __future__ import annotations
from homeassistant.components import persistent_notification
from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the demo Button entity."""
async_add_entities(
[
DemoButton(
unique_id="push",
name="Push",
icon="mdi:gesture-tap-button",
),
]
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
"""Set up the demo button platform."""
async_add_entities(
[
DemoButton(
unique_id="push",
device_name="Push",
icon="mdi:gesture-tap-button",
),
]
)
class DemoButton(ButtonEntity):
"""Representation of a demo button entity."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
icon: str,
) -> None:
"""Initialize the Demo button entity."""
self._attr_unique_id = unique_id
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_icon = icon
self._attr_device_info = {
"identifiers": {(DOMAIN, unique_id)},
"name": name,
"name": device_name,
}
async def async_press(self) -> None:

View File

@ -7,22 +7,6 @@ from homeassistant.components.camera import Camera, CameraEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Demo camera platform."""
async_add_entities(
[
DemoCamera("Demo camera", "image/jpg"),
DemoCamera("Demo camera png", "image/png"),
]
)
async def async_setup_entry(
@ -31,7 +15,12 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
async_add_entities(
[
DemoCamera("Demo camera", "image/jpg"),
DemoCamera("Demo camera png", "image/png"),
]
)
class DemoCamera(Camera):

View File

@ -14,27 +14,24 @@ from homeassistant.components.climate import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
SUPPORT_FLAGS = ClimateEntityFeature(0)
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Demo climate devices."""
"""Set up the demo climate platform."""
async_add_entities(
[
DemoClimate(
unique_id="climate_1",
name="HeatPump",
device_name="HeatPump",
target_temperature=68,
unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
preset=None,
@ -52,7 +49,7 @@ async def async_setup_platform(
),
DemoClimate(
unique_id="climate_2",
name="Hvac",
device_name="Hvac",
target_temperature=21,
unit_of_measurement=UnitOfTemperature.CELSIUS,
preset=None,
@ -70,7 +67,7 @@ async def async_setup_platform(
),
DemoClimate(
unique_id="climate_3",
name="Ecobee",
device_name="Ecobee",
target_temperature=None,
unit_of_measurement=UnitOfTemperature.CELSIUS,
preset="home",
@ -91,25 +88,17 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo climate devices config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoClimate(ClimateEntity):
"""Representation of a demo climate device."""
_attr_has_entity_name = True
_attr_should_poll = False
_attr_translation_key = "ubercool"
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
target_temperature: float | None,
unit_of_measurement: str,
preset: str | None,
@ -128,7 +117,6 @@ class DemoClimate(ClimateEntity):
) -> None:
"""Initialize the climate device."""
self._unique_id = unique_id
self._attr_name = name
self._attr_supported_features = SUPPORT_FLAGS
if target_temperature is not None:
self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE
@ -163,17 +151,10 @@ class DemoClimate(ClimateEntity):
self._swing_modes = ["auto", "1", "2", "3", "off"]
self._target_temperature_high = target_temp_high
self._target_temperature_low = target_temp_low
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
identifiers={
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.unique_id)
},
name=self.name,
)
self._attr_device_info = {
"identifiers": {(DOMAIN, unique_id)},
"name": device_name,
}
@property
def unique_id(self) -> str:

View File

@ -16,18 +16,16 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_utc_time_change
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Demo covers."""
"""Set up the demo cover platform."""
async_add_entities(
[
DemoCover(hass, "cover_1", "Kitchen Window"),
@ -56,25 +54,17 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoCover(CoverEntity):
"""Representation of a demo cover."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
hass: HomeAssistant,
unique_id: str,
name: str,
device_name: str,
position: int | None = None,
tilt_position: int | None = None,
device_class: CoverDeviceClass | None = None,
@ -83,7 +73,6 @@ class DemoCover(CoverEntity):
"""Initialize the cover."""
self.hass = hass
self._unique_id = unique_id
self._attr_name = name
self._position = position
self._attr_device_class = device_class
self._attr_supported_features = supported_features
@ -101,15 +90,12 @@ class DemoCover(CoverEntity):
else:
self._closed = position <= 0
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
self._attr_device_info = DeviceInfo(
identifiers={
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.unique_id)
},
name=self.name,
name=device_name,
)
@property

View File

@ -5,22 +5,19 @@ from datetime import date
from homeassistant.components.date import DateEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Demo date entity."""
"""Set up the demo date platform."""
async_add_entities(
[
DemoDate(
@ -34,24 +31,16 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoDate(DateEntity):
"""Representation of a Demo date entity."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
state: date,
icon: str,
assumed_state: bool,
@ -59,12 +48,11 @@ class DemoDate(DateEntity):
"""Initialize the Demo date entity."""
self._attr_assumed_state = assumed_state
self._attr_icon = icon
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_native_value = state
self._attr_unique_id = unique_id
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)}, name=self.name
identifiers={(DOMAIN, unique_id)}, name=device_name
)
async def async_set_value(self, value: date) -> None:

View File

@ -5,22 +5,19 @@ from datetime import datetime, timezone
from homeassistant.components.datetime import DateTimeEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Demo date/time entity."""
"""Set up the demo datetime platform."""
async_add_entities(
[
DemoDateTime(
@ -34,24 +31,16 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoDateTime(DateTimeEntity):
"""Representation of a Demo date/time entity."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
state: datetime,
icon: str,
assumed_state: bool,
@ -59,7 +48,6 @@ class DemoDateTime(DateTimeEntity):
"""Initialize the Demo date/time entity."""
self._attr_assumed_state = assumed_state
self._attr_icon = icon
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_native_value = state
self._attr_unique_id = unique_id
@ -68,7 +56,7 @@ class DemoDateTime(DateTimeEntity):
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, unique_id)
},
name=self.name,
name=device_name,
)
async def async_set_value(self, value: datetime) -> None:

View File

@ -20,7 +20,6 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
@ -34,11 +33,10 @@ SUPPORT_DEMO = {ColorMode.HS, ColorMode.COLOR_TEMP}
SUPPORT_DEMO_HS_WHITE = {ColorMode.HS, ColorMode.WHITE}
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the demo light platform."""
async_add_entities(
@ -47,28 +45,28 @@ async def async_setup_platform(
available=True,
effect_list=LIGHT_EFFECT_LIST,
effect=LIGHT_EFFECT_LIST[0],
name="Bed Light",
device_name="Bed Light",
state=False,
unique_id="light_1",
),
DemoLight(
available=True,
ct=LIGHT_TEMPS[1],
name="Ceiling Lights",
device_name="Ceiling Lights",
state=True,
unique_id="light_2",
),
DemoLight(
available=True,
hs_color=LIGHT_COLORS[1],
name="Kitchen Lights",
device_name="Kitchen Lights",
state=True,
unique_id="light_3",
),
DemoLight(
available=True,
ct=LIGHT_TEMPS[1],
name="Office RGBW Lights",
device_name="Office RGBW Lights",
rgbw_color=(255, 0, 0, 255),
state=True,
supported_color_modes={ColorMode.RGBW},
@ -76,7 +74,7 @@ async def async_setup_platform(
),
DemoLight(
available=True,
name="Living Room RGBWW Lights",
device_name="Living Room RGBWW Lights",
rgbww_color=(255, 0, 0, 255, 0),
state=True,
supported_color_modes={ColorMode.RGBWW},
@ -84,7 +82,7 @@ async def async_setup_platform(
),
DemoLight(
available=True,
name="Entrance Color + White Lights",
device_name="Entrance Color + White Lights",
hs_color=LIGHT_COLORS[1],
state=True,
supported_color_modes=SUPPORT_DEMO_HS_WHITE,
@ -94,24 +92,16 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoLight(LightEntity):
"""Representation of a demo light."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
state: bool,
available: bool = False,
brightness: int = 180,
@ -130,7 +120,6 @@ class DemoLight(LightEntity):
self._effect = effect
self._effect_list = effect_list
self._hs_color = hs_color
self._attr_name = name
self._rgbw_color = rgbw_color
self._rgbww_color = rgbww_color
self._state = state
@ -148,16 +137,12 @@ class DemoLight(LightEntity):
self._color_modes = supported_color_modes
if self._effect_list is not None:
self._attr_supported_features |= LightEntityFeature.EFFECT
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
self._attr_device_info = DeviceInfo(
identifiers={
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, self.unique_id)
},
name=self.name,
name=device_name,
)
@property

View File

@ -3,22 +3,20 @@ from __future__ import annotations
from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberMode
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_DEFAULT_NAME, UnitOfTemperature
from homeassistant.const import UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the demo Number entity."""
"""Set up the demo number platform."""
async_add_entities(
[
DemoNumber(
@ -77,24 +75,16 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoNumber(NumberEntity):
"""Representation of a demo Number entity."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
state: float,
icon: str,
assumed_state: bool,
@ -111,7 +101,6 @@ class DemoNumber(NumberEntity):
self._attr_device_class = device_class
self._attr_icon = icon
self._attr_mode = mode
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_native_value = state
self._attr_unique_id = unique_id
@ -128,7 +117,7 @@ class DemoNumber(NumberEntity):
# Serial numbers are unique identifiers within a specific domain
(DOMAIN, unique_id)
},
name=self.name,
name=device_name,
)
async def async_set_native_value(self, value: float) -> None:

View File

@ -3,27 +3,24 @@ from __future__ import annotations
from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DOMAIN
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the demo Select entity."""
"""Set up the demo select platform."""
async_add_entities(
[
DemoSelect(
unique_id="speed",
name="Speed",
device_name="Speed",
icon="mdi:speedometer",
current_option="ridiculous_speed",
options=[
@ -37,24 +34,16 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoSelect(SelectEntity):
"""Representation of a demo select entity."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
icon: str,
current_option: str | None,
options: list[str],
@ -62,14 +51,13 @@ class DemoSelect(SelectEntity):
) -> None:
"""Initialize the Demo select entity."""
self._attr_unique_id = unique_id
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_current_option = current_option
self._attr_icon = icon
self._attr_options = options
self._attr_translation_key = translation_key
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=name,
name=device_name,
)
async def async_select_option(self, option: str) -> None:

View File

@ -25,18 +25,17 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
from homeassistant.helpers.typing import StateType
from . import DOMAIN
async def async_setup_platform(
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigType,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Demo sensors."""
"""Set up the demo sensor platform."""
async_add_entities(
[
DemoSensor(
@ -126,7 +125,7 @@ async def async_setup_platform(
),
DemoSensor(
unique_id="sensor_10",
name=None,
device_name="Thermostat",
state="eco",
device_class=SensorDeviceClass.ENUM,
state_class=None,
@ -139,24 +138,16 @@ async def async_setup_platform(
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
await async_setup_platform(hass, {}, async_add_entities)
class DemoSensor(SensorEntity):
"""Representation of a Demo sensor."""
_attr_has_entity_name = True
_attr_should_poll = False
def __init__(
self,
unique_id: str,
name: str | None,
device_name: str | None,
state: StateType,
device_class: SensorDeviceClass,
state_class: SensorStateClass | None,
@ -167,10 +158,6 @@ class DemoSensor(SensorEntity):
) -> None:
"""Initialize the sensor."""
self._attr_device_class = device_class
if name is not None:
self._attr_name = name
else:
self._attr_has_entity_name = True
self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_native_value = state
self._attr_state_class = state_class
@ -180,7 +167,7 @@ class DemoSensor(SensorEntity):
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=name,
name=device_name,
)
if battery:
@ -196,7 +183,7 @@ class DemoSumSensor(RestoreSensor):
def __init__(
self,
unique_id: str,
name: str,
device_name: str,
five_minute_increase: float,
device_class: SensorDeviceClass,
state_class: SensorStateClass | None,
@ -207,7 +194,6 @@ class DemoSumSensor(RestoreSensor):
"""Initialize the sensor."""
self.entity_id = f"{SENSOR_DOMAIN}.{suggested_entity_id}"
self._attr_device_class = device_class
self._attr_name = name
self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_native_value = 0
self._attr_state_class = state_class
@ -216,7 +202,7 @@ class DemoSumSensor(RestoreSensor):
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=name,
name=device_name,
)
if battery:

View File

@ -55,7 +55,7 @@ def async_setup_legacy(
providers = hass.data[DATA_PROVIDERS] = {}
async def async_setup_platform(p_type, p_config=None, discovery_info=None):
"""Set up a TTS platform."""
"""Set up an STT platform."""
if p_config is None:
p_config = {}

View File

@ -1,11 +1,12 @@
"""The tests for calendar recorder."""
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -19,6 +20,16 @@ async def setup_homeassistant():
"""Override the fixture in calendar.conftest."""
@pytest.fixture(autouse=True)
async def calendar_only() -> None:
"""Enable only the calendar platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.CALENDAR],
):
yield
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
"""Test sensor attributes to be excluded."""
now = dt_util.utcnow()

View File

@ -5,6 +5,7 @@ import pytest
from homeassistant.components import camera
from homeassistant.components.camera.const import StreamType
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@ -17,6 +18,16 @@ async def setup_homeassistant(hass: HomeAssistant):
await async_setup_component(hass, "homeassistant", {})
@pytest.fixture(autouse=True)
async def camera_only() -> None:
"""Enable only the camera platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.CAMERA],
):
yield
@pytest.fixture(name="mock_camera")
async def mock_camera_fixture(hass):
"""Initialize a demo camera platform."""

View File

@ -2,6 +2,9 @@
from __future__ import annotations
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components import climate
from homeassistant.components.climate import (
@ -17,7 +20,7 @@ from homeassistant.components.climate import (
)
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -26,6 +29,16 @@ from tests.common import async_fire_time_changed
from tests.components.recorder.common import async_wait_recording_done
@pytest.fixture(autouse=True)
async def climate_only() -> None:
"""Enable only the climate platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.CLIMATE],
):
yield
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
"""Test climate registered attributes to be excluded."""
now = dt_util.utcnow()

View File

@ -976,7 +976,7 @@ async def test_get_single(
assert await async_setup_component(hass, "config", {})
ws_client = await hass_ws_client(hass)
entry = MockConfigEntry(domain="demo", state=core_ce.ConfigEntryState.LOADED)
entry = MockConfigEntry(domain="test", state=core_ce.ConfigEntryState.LOADED)
entry.add_to_hass(hass)
assert entry.pref_disable_new_entities is False
@ -993,7 +993,7 @@ async def test_get_single(
assert response["success"]
assert response["result"]["config_entry"] == {
"disabled_by": None,
"domain": "demo",
"domain": "test",
"entry_id": entry.entry_id,
"pref_disable_new_entities": False,
"pref_disable_polling": False,

View File

@ -1,4 +1,6 @@
"""demo conftest."""
from unittest.mock import patch
import pytest
from homeassistant.core import HomeAssistant
@ -16,3 +18,13 @@ def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None:
async def setup_homeassistant(hass: HomeAssistant):
"""Set up the homeassistant integration."""
await async_setup_component(hass, "homeassistant", {})
@pytest.fixture
async def disable_platforms(hass: HomeAssistant) -> None:
"""Disable platforms to speed up tests."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[],
):
yield

View File

@ -5,7 +5,7 @@ from unittest.mock import patch
import pytest
from homeassistant.components.button import DOMAIN, SERVICE_PRESS
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -13,8 +13,18 @@ from homeassistant.util import dt as dt_util
ENTITY_PUSH = "button.push"
@pytest.fixture
async def button_only() -> None:
"""Enable only the button platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.BUTTON],
):
yield
@pytest.fixture(autouse=True)
async def setup_demo_button(hass: HomeAssistant) -> None:
async def setup_demo_button(hass: HomeAssistant, button_only) -> None:
"""Initialize setup demo button entity."""
assert await async_setup_component(hass, DOMAIN, {"button": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -14,7 +14,7 @@ from homeassistant.components.camera import (
async_get_image,
)
from homeassistant.components.demo import DOMAIN
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component
@ -22,8 +22,18 @@ from homeassistant.setup import async_setup_component
ENTITY_CAMERA = "camera.demo_camera"
@pytest.fixture
async def camera_only() -> None:
"""Enable only the button platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.CAMERA],
):
yield
@pytest.fixture(autouse=True)
async def demo_camera(hass):
async def demo_camera(hass, camera_only):
"""Initialize a demo camera platform."""
assert await async_setup_component(
hass, CAMERA_DOMAIN, {CAMERA_DOMAIN: {"platform": DOMAIN}}

View File

@ -1,4 +1,5 @@
"""The tests for the demo climate component."""
from unittest.mock import patch
import pytest
import voluptuous as vol
@ -40,6 +41,7 @@ from homeassistant.const import (
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@ -50,8 +52,18 @@ ENTITY_ECOBEE = "climate.ecobee"
ENTITY_HEATPUMP = "climate.heatpump"
@pytest.fixture
async def climate_only() -> None:
"""Enable only the climate platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.CLIMATE],
):
yield
@pytest.fixture(autouse=True)
async def setup_demo_climate(hass):
async def setup_demo_climate(hass, climate_only):
"""Initialize setup demo climate."""
hass.config.units = METRIC_SYSTEM
assert await async_setup_component(hass, DOMAIN, {"climate": {"platform": "demo"}})

View File

@ -1,5 +1,6 @@
"""The tests for the Demo cover platform."""
from datetime import timedelta
from unittest.mock import patch
import pytest
@ -27,6 +28,7 @@ from homeassistant.const import (
STATE_CLOSING,
STATE_OPEN,
STATE_OPENING,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@ -39,7 +41,17 @@ ENTITY_COVER = "cover.living_room_window"
@pytest.fixture
async def setup_comp(hass):
async def cover_only() -> None:
"""Enable only the climate platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.COVER],
):
yield
@pytest.fixture
async def setup_comp(hass, cover_only):
"""Set up demo cover component."""
with assert_setup_component(1, DOMAIN):
await async_setup_component(hass, DOMAIN, CONFIG)

View File

@ -1,16 +1,28 @@
"""The tests for the demo date component."""
from unittest.mock import patch
import pytest
from homeassistant.components.date import ATTR_DATE, DOMAIN, SERVICE_SET_VALUE
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
ENTITY_DATE = "date.date"
@pytest.fixture
async def date_only() -> None:
"""Enable only the date platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.DATE],
):
yield
@pytest.fixture(autouse=True)
async def setup_demo_date(hass: HomeAssistant) -> None:
async def setup_demo_date(hass: HomeAssistant, date_only) -> None:
"""Initialize setup demo date."""
assert await async_setup_component(hass, DOMAIN, {"date": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -1,16 +1,28 @@
"""The tests for the demo datetime component."""
from unittest.mock import patch
import pytest
from homeassistant.components.datetime import ATTR_DATETIME, DOMAIN, SERVICE_SET_VALUE
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
ENTITY_DATETIME = "datetime.date_and_time"
@pytest.fixture
async def datetime_only() -> None:
"""Enable only the datetime platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.DATETIME],
):
yield
@pytest.fixture(autouse=True)
async def setup_demo_datetime(hass: HomeAssistant) -> None:
async def setup_demo_datetime(hass: HomeAssistant, datetime_only) -> None:
"""Initialize setup demo datetime."""
assert await async_setup_component(hass, DOMAIN, {"datetime": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -32,7 +32,7 @@ PERCENTAGE_MODEL_FANS = ["fan.percentage_full_fan", "fan.percentage_limited_fan"
@pytest.fixture(autouse=True)
async def setup_comp(hass):
async def setup_comp(hass, disable_platforms):
"""Initialize components."""
assert await async_setup_component(hass, fan.DOMAIN, {"fan": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -21,7 +21,7 @@ from tests.common import assert_setup_component, async_fire_time_changed
CONFIG = {geo_location.DOMAIN: [{"platform": "demo"}]}
async def test_setup_platform(hass: HomeAssistant) -> None:
async def test_setup_platform(hass: HomeAssistant, disable_platforms) -> None:
"""Test setup of demo platform via configuration."""
utcnow = dt_util.utcnow()
# Patching 'utcnow' to gain more control over the timed update.

View File

@ -30,7 +30,7 @@ ENTITY_HUMIDIFIER = "humidifier.humidifier"
@pytest.fixture(autouse=True)
async def setup_demo_humidifier(hass):
async def setup_demo_humidifier(hass, disable_platforms):
"""Initialize setup demo humidifier."""
assert await async_setup_component(
hass, DOMAIN, {"humidifier": {"platform": "demo"}}

View File

@ -1,4 +1,6 @@
"""The tests for the demo light component."""
from unittest.mock import patch
import pytest
from homeassistant.components.demo import DOMAIN
@ -16,15 +18,25 @@ from homeassistant.components.light import (
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
ENTITY_LIGHT = "light.bed_light"
@pytest.fixture
async def light_only() -> None:
"""Enable only the light platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.LIGHT],
):
yield
@pytest.fixture(autouse=True)
async def setup_comp(hass):
async def setup_comp(hass, light_only):
"""Set up demo component."""
assert await async_setup_component(
hass, LIGHT_DOMAIN, {LIGHT_DOMAIN: {"platform": DOMAIN}}

View File

@ -28,7 +28,7 @@ OPENABLE_LOCK = "lock.openable_lock"
@pytest.fixture(autouse=True)
async def setup_comp(hass):
async def setup_comp(hass, disable_platforms):
"""Set up demo component."""
assert await async_setup_component(
hass, LOCK_DOMAIN, {LOCK_DOMAIN: {"platform": DOMAIN}}

View File

@ -23,6 +23,11 @@ from tests.typing import ClientSessionGenerator
TEST_ENTITY_ID = "media_player.walkman"
@pytest.fixture(autouse=True)
def autouse_disable_platforms(disable_platforms):
"""Auto use the disable_platforms fixture."""
@pytest.fixture(name="mock_media_seek")
def media_player_media_seek_fixture():
"""Mock demo YouTube player media seek."""

View File

@ -16,6 +16,11 @@ from tests.common import assert_setup_component, async_capture_events
CONFIG = {notify.DOMAIN: {"platform": "demo"}}
@pytest.fixture(autouse=True)
def autouse_disable_platforms(disable_platforms):
"""Auto use the disable_platforms fixture."""
@pytest.fixture
def events(hass):
"""Fixture that catches notify events."""

View File

@ -1,4 +1,5 @@
"""The tests for the demo number component."""
from unittest.mock import patch
import pytest
import voluptuous as vol
@ -12,7 +13,7 @@ from homeassistant.components.number import (
SERVICE_SET_VALUE,
NumberMode,
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_MODE
from homeassistant.const import ATTR_ENTITY_ID, ATTR_MODE, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@ -22,8 +23,18 @@ ENTITY_LARGE_RANGE = "number.large_range"
ENTITY_SMALL_RANGE = "number.small_range"
@pytest.fixture
async def number_only() -> None:
"""Enable only the number platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.NUMBER],
):
yield
@pytest.fixture(autouse=True)
async def setup_demo_number(hass):
async def setup_demo_number(hass, number_only):
"""Initialize setup demo Number entity."""
assert await async_setup_component(hass, DOMAIN, {"number": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -18,7 +18,7 @@ SERVICE_SEND_COMMAND = "send_command"
@pytest.fixture(autouse=True)
async def setup_component(hass):
async def setup_component(hass, disable_platforms):
"""Initialize components."""
assert await async_setup_component(
hass, remote.DOMAIN, {"remote": {"platform": "demo"}}

View File

@ -1,4 +1,5 @@
"""The tests for the demo select component."""
from unittest.mock import patch
import pytest
@ -8,15 +9,25 @@ from homeassistant.components.select import (
DOMAIN,
SERVICE_SELECT_OPTION,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
ENTITY_SPEED = "select.speed"
@pytest.fixture
async def select_only() -> None:
"""Enable only the select platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.SELECT],
):
yield
@pytest.fixture(autouse=True)
async def setup_demo_select(hass: HomeAssistant) -> None:
async def setup_demo_select(hass: HomeAssistant, select_only) -> None:
"""Initialize setup demo select entity."""
assert await async_setup_component(hass, DOMAIN, {"select": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -1,5 +1,6 @@
"""The tests for the demo sensor component."""
from datetime import timedelta
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest
@ -7,12 +8,23 @@ import pytest
from homeassistant import core as ha
from homeassistant.components.demo import DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import mock_restore_cache_with_extra_data
@pytest.fixture(autouse=True)
async def sensor_only() -> None:
"""Enable only the sensor platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.SENSOR],
):
yield
@pytest.mark.parametrize(("entity_id", "delta"), (("sensor.total_energy_kwh", 0.5),))
async def test_energy_sensor(
hass: HomeAssistant, entity_id, delta, freezer: FrozenDateTimeFactory

View File

@ -25,7 +25,7 @@ ENTITY_SIREN_WITH_ALL_FEATURES = "siren.siren_with_all_features"
@pytest.fixture(autouse=True)
async def setup_demo_siren(hass):
async def setup_demo_siren(hass, disable_platforms):
"""Initialize setup demo siren."""
assert await async_setup_component(hass, DOMAIN, {"siren": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -15,7 +15,7 @@ SWITCH_ENTITY_IDS = ["switch.decorative_lights", "switch.ac"]
@pytest.fixture(autouse=True)
async def setup_comp(hass):
async def setup_comp(hass, disable_platforms):
"""Set up demo component."""
assert await async_setup_component(
hass, SWITCH_DOMAIN, {SWITCH_DOMAIN: {"platform": DOMAIN}}

View File

@ -17,7 +17,7 @@ ENTITY_TEXT = "text.text"
@pytest.fixture(autouse=True)
async def setup_demo_text(hass):
async def setup_demo_text(hass, disable_platforms):
"""Initialize setup demo text."""
assert await async_setup_component(hass, DOMAIN, {"text": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -10,7 +10,7 @@ ENTITY_TIME = "time.time"
@pytest.fixture(autouse=True)
async def setup_demo_datetime(hass: HomeAssistant) -> None:
async def setup_demo_datetime(hass: HomeAssistant, disable_platforms) -> None:
"""Initialize setup demo time."""
assert await async_setup_component(hass, DOMAIN, {"time": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -27,7 +27,7 @@ from homeassistant.setup import async_setup_component
@pytest.fixture(autouse=True)
async def setup_demo_update(hass: HomeAssistant) -> None:
async def setup_demo_update(hass: HomeAssistant, disable_platforms) -> None:
"""Initialize setup demo update entity."""
assert await async_setup_component(hass, DOMAIN, {"update": {"platform": "demo"}})
await hass.async_block_till_done()

View File

@ -52,7 +52,7 @@ ENTITY_VACUUM_STATE = f"{DOMAIN}.{DEMO_VACUUM_STATE}".lower()
@pytest.fixture(autouse=True)
async def setup_demo_vacuum(hass):
async def setup_demo_vacuum(hass, disable_platforms):
"""Initialize setup demo vacuum."""
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "demo"}})
await hass.async_block_till_done()

View File

@ -14,7 +14,7 @@ ENTITY_WATER_HEATER_CELSIUS = "water_heater.demo_water_heater_celsius"
@pytest.fixture(autouse=True)
async def setup_comp(hass):
async def setup_comp(hass, disable_platforms):
"""Set up demo component."""
hass.config.units = US_CUSTOMARY_SYSTEM
assert await async_setup_component(

View File

@ -20,7 +20,7 @@ from homeassistant.setup import async_setup_component
from homeassistant.util.unit_system import METRIC_SYSTEM
async def test_attributes(hass: HomeAssistant) -> None:
async def test_attributes(hass: HomeAssistant, disable_platforms) -> None:
"""Test weather attributes."""
assert await async_setup_component(
hass, weather.DOMAIN, {"weather": {"platform": "demo"}}

View File

@ -135,8 +135,25 @@ async def base_setup(hass):
)
@pytest.fixture(autouse=True)
async def wanted_platforms_only() -> None:
"""Enable only the wanted demo platforms."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[
const.Platform.CLIMATE,
const.Platform.COVER,
const.Platform.FAN,
const.Platform.HUMIDIFIER,
const.Platform.LIGHT,
const.Platform.MEDIA_PLAYER,
],
):
yield
@pytest.fixture
async def demo_setup(hass):
async def demo_setup(hass, wanted_platforms_only):
"""Fixture to setup demo platforms."""
# We need to do this to get access to homeassistant/turn_(on,off)
setups = [
@ -144,12 +161,7 @@ async def demo_setup(hass):
setup.async_setup_component(
hass, http.DOMAIN, {http.DOMAIN: {http.CONF_SERVER_PORT: HTTP_SERVER_PORT}}
),
*[
setup.async_setup_component(
hass, comp.DOMAIN, {comp.DOMAIN: [{"platform": "demo"}]}
)
for comp in (light, climate, humidifier, media_player, fan, cover)
],
setup.async_setup_component(hass, "demo", {}),
setup.async_setup_component(
hass,
script.DOMAIN,

View File

@ -2,12 +2,15 @@
from __future__ import annotations
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components import fan
from homeassistant.components.fan import ATTR_PRESET_MODES
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -16,6 +19,16 @@ from tests.common import async_fire_time_changed
from tests.components.recorder.common import async_wait_recording_done
@pytest.fixture(autouse=True)
async def fan_only() -> None:
"""Enable only the fan platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.FAN],
):
yield
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
"""Test fan registered attributes to be excluded."""
now = dt_util.utcnow()

View File

@ -1,8 +1,11 @@
"""Test diagnostics."""
from unittest.mock import ANY
from unittest.mock import ANY, patch
import pytest
from homeassistant import setup
from homeassistant.components import google_assistant as ga, switch
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@ -12,6 +15,16 @@ from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
@pytest.fixture(autouse=True)
async def switch_only() -> None:
"""Enable only the switch platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.SWITCH],
):
yield
async def test_diagnostics(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None:

View File

@ -1,26 +1,22 @@
"""The tests for the Google Assistant component."""
from http import HTTPStatus
import json
from unittest.mock import patch
from aiohttp.hdrs import AUTHORIZATION
import pytest
from homeassistant import const, core, setup
from homeassistant.components import (
alarm_control_panel,
climate,
cover,
fan,
google_assistant as ga,
humidifier,
light,
lock,
media_player,
switch,
)
from homeassistant.const import (
CLOUD_NEVER_EXPOSED_ENTITIES,
EntityCategory,
Platform,
UnitOfTemperature,
)
from homeassistant.helpers import entity_registry as er
@ -65,6 +61,26 @@ def assistant_client(event_loop, hass, hass_client_no_auth):
return loop.run_until_complete(hass_client_no_auth())
@pytest.fixture(autouse=True)
async def wanted_platforms_only() -> None:
"""Enable only the wanted demo platforms."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[
Platform.ALARM_CONTROL_PANEL,
Platform.CLIMATE,
Platform.COVER,
Platform.FAN,
Platform.HUMIDIFIER,
Platform.LIGHT,
Platform.LOCK,
Platform.MEDIA_PLAYER,
Platform.SWITCH,
],
):
yield
@pytest.fixture
def hass_fixture(event_loop, hass):
"""Set up a Home Assistant instance for these tests."""
@ -73,55 +89,7 @@ def hass_fixture(event_loop, hass):
# We need to do this to get access to homeassistant/turn_(on,off)
loop.run_until_complete(setup.async_setup_component(hass, core.DOMAIN, {}))
loop.run_until_complete(
setup.async_setup_component(
hass, light.DOMAIN, {"light": [{"platform": "demo"}]}
)
)
loop.run_until_complete(
setup.async_setup_component(
hass, switch.DOMAIN, {"switch": [{"platform": "demo"}]}
)
)
loop.run_until_complete(
setup.async_setup_component(
hass, cover.DOMAIN, {"cover": [{"platform": "demo"}]}
)
)
loop.run_until_complete(
setup.async_setup_component(
hass, media_player.DOMAIN, {"media_player": [{"platform": "demo"}]}
)
)
loop.run_until_complete(
setup.async_setup_component(hass, fan.DOMAIN, {"fan": [{"platform": "demo"}]})
)
loop.run_until_complete(
setup.async_setup_component(
hass, climate.DOMAIN, {"climate": [{"platform": "demo"}]}
)
)
loop.run_until_complete(
setup.async_setup_component(
hass, humidifier.DOMAIN, {"humidifier": [{"platform": "demo"}]}
)
)
loop.run_until_complete(
setup.async_setup_component(hass, lock.DOMAIN, {"lock": [{"platform": "demo"}]})
)
loop.run_until_complete(
setup.async_setup_component(
hass,
alarm_control_panel.DOMAIN,
{"alarm_control_panel": [{"platform": "demo"}]},
)
)
loop.run_until_complete(setup.async_setup_component(hass, "demo", {}))
return hass

View File

@ -22,7 +22,12 @@ from homeassistant.components.google_assistant import (
trait,
)
from homeassistant.config import async_process_ha_core_config
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, UnitOfTemperature, __version__
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
Platform,
UnitOfTemperature,
__version__,
)
from homeassistant.core import EVENT_CALL_SERVICE, HomeAssistant, State
from homeassistant.helpers import (
area_registry as ar,
@ -39,6 +44,16 @@ from tests.common import async_capture_events
REQ_ID = "ff36a3cc-ec34-11e6-b1a0-64510650abcf"
@pytest.fixture
async def light_only() -> None:
"""Enable only the light platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.LIGHT],
):
yield
@pytest.fixture
def registries(
entity_registry: er.EntityRegistry,
@ -128,6 +143,8 @@ async def test_sync_message(hass: HomeAssistant, registries) -> None:
)
light.hass = hass
light.entity_id = "light.demo_light"
light._attr_device_info = None
light._attr_name = "Demo Light"
light.async_write_ha_state()
# This should not show up in the sync request
@ -268,6 +285,8 @@ async def test_sync_in_area(area_on_device, hass: HomeAssistant, registries) ->
)
light.hass = hass
light.entity_id = entity.entity_id
light._attr_device_info = None
light._attr_name = "Demo Light"
light.async_write_ha_state()
config = MockConfig(should_expose=lambda _: True, entity_config={})
@ -360,6 +379,8 @@ async def test_query_message(hass: HomeAssistant) -> None:
)
light.hass = hass
light.entity_id = "light.demo_light"
light._attr_device_info = None
light._attr_name = "Demo Light"
light.async_write_ha_state()
light2 = DemoLight(
@ -367,11 +388,15 @@ async def test_query_message(hass: HomeAssistant) -> None:
)
light2.hass = hass
light2.entity_id = "light.another_light"
light2._attr_device_info = None
light2._attr_name = "Another Light"
light2.async_write_ha_state()
light3 = DemoLight(None, "Color temp Light", state=True, ct=400, brightness=200)
light3.hass = hass
light3.entity_id = "light.color_temp_light"
light3._attr_device_info = None
light3._attr_name = "Color temp Light"
light3.async_write_ha_state()
events = async_capture_events(hass, EVENT_QUERY_RECEIVED)
@ -448,7 +473,7 @@ async def test_query_message(hass: HomeAssistant) -> None:
[(False, True, 20, 0.2), (True, ANY, ANY, ANY)],
)
async def test_execute(
hass: HomeAssistant, report_state, on, brightness, value
hass: HomeAssistant, light_only, report_state, on, brightness, value
) -> None:
"""Test an execute command."""
await async_setup_component(hass, "homeassistant", {})
@ -630,7 +655,7 @@ async def test_execute(
("report_state", "on", "brightness", "value"), [(False, False, ANY, ANY)]
)
async def test_execute_times_out(
hass: HomeAssistant, report_state, on, brightness, value
hass: HomeAssistant, light_only, report_state, on, brightness, value
) -> None:
"""Test an execute command which times out."""
orig_execute_limit = sh.EXECUTE_LIMIT
@ -933,6 +958,8 @@ async def test_unavailable_state_does_sync(hass: HomeAssistant) -> None:
light.hass = hass
light.entity_id = "light.demo_light"
light._available = False
light._attr_device_info = None
light._attr_name = "Demo Light"
light.async_write_ha_state()
events = async_capture_events(hass, EVENT_SYNC_RECEIVED)
@ -1074,6 +1101,8 @@ async def test_device_class_binary_sensor(
)
sensor.hass = hass
sensor.entity_id = "binary_sensor.demo_sensor"
sensor._attr_device_info = None
sensor._attr_name = "Demo Sensor"
sensor.async_write_ha_state()
result = await sh.async_handle_message(
@ -1125,6 +1154,8 @@ async def test_device_class_cover(
sensor = DemoCover(None, hass, "Demo Sensor", device_class=device_class)
sensor.hass = hass
sensor.entity_id = "cover.demo_sensor"
sensor._attr_device_info = None
sensor._attr_name = "Demo Sensor"
sensor.async_write_ha_state()
result = await sh.async_handle_message(
@ -1456,6 +1487,8 @@ async def test_sync_message_recovery(
)
light.hass = hass
light.entity_id = "light.demo_light"
light._attr_device_info = None
light._attr_name = "Demo Light"
light.async_write_ha_state()
hass.states.async_set(

View File

@ -2,6 +2,9 @@
from __future__ import annotations
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components import light
from homeassistant.components.light import (
@ -14,7 +17,7 @@ from homeassistant.components.light import (
)
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -23,6 +26,16 @@ from tests.common import async_fire_time_changed
from tests.components.recorder.common import async_wait_recording_done
@pytest.fixture(autouse=True)
async def light_only() -> None:
"""Enable only the light platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.LIGHT],
):
yield
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
"""Test light registered attributes to be excluded."""
now = dt_util.utcnow()

View File

@ -2,6 +2,9 @@
from __future__ import annotations
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components import media_player
from homeassistant.components.media_player import (
@ -13,7 +16,7 @@ from homeassistant.components.media_player import (
)
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.const import ATTR_ENTITY_PICTURE, ATTR_FRIENDLY_NAME
from homeassistant.const import ATTR_ENTITY_PICTURE, ATTR_FRIENDLY_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -22,6 +25,16 @@ from tests.common import async_fire_time_changed
from tests.components.recorder.common import async_wait_recording_done
@pytest.fixture(autouse=True)
async def media_player_only() -> None:
"""Enable only the media_player platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.MEDIA_PLAYER],
):
yield
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
"""Test media_player registered attributes to be excluded."""
now = dt_util.utcnow()

View File

@ -2,12 +2,15 @@
from __future__ import annotations
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components import number
from homeassistant.components.number import ATTR_MAX, ATTR_MIN, ATTR_MODE, ATTR_STEP
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -16,6 +19,16 @@ from tests.common import async_fire_time_changed
from tests.components.recorder.common import async_wait_recording_done
@pytest.fixture(autouse=True)
async def number_only() -> None:
"""Enable only the number platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.NUMBER],
):
yield
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
"""Test number registered attributes to be excluded."""
assert await async_setup_component(hass, "homeassistant", {})

View File

@ -2,12 +2,15 @@
from __future__ import annotations
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components import select
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.components.select import ATTR_OPTIONS
from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -16,6 +19,16 @@ from tests.common import async_fire_time_changed
from tests.components.recorder.common import async_wait_recording_done
@pytest.fixture(autouse=True)
async def select_only() -> None:
"""Enable only the select platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.SELECT],
):
yield
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
"""Test select registered attributes to be excluded."""
now = dt_util.utcnow()

View File

@ -2,12 +2,15 @@
from __future__ import annotations
from datetime import timedelta
from unittest.mock import patch
import pytest
from homeassistant.components import text
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.components.text import ATTR_MAX, ATTR_MIN, ATTR_MODE, ATTR_PATTERN
from homeassistant.const import ATTR_FRIENDLY_NAME
from homeassistant.const import ATTR_FRIENDLY_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@ -16,6 +19,16 @@ from tests.common import async_fire_time_changed
from tests.components.recorder.common import async_wait_recording_done
@pytest.fixture(autouse=True)
async def text_only() -> None:
"""Enable only the text platform."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[Platform.TEXT],
):
yield
async def test_exclude_attributes(recorder_mock: Recorder, hass: HomeAssistant) -> None:
"""Test siren registered attributes to be excluded."""
now = dt_util.utcnow()

View File

@ -1,7 +1,9 @@
"""The tests for the TTS component."""
from unittest.mock import patch
import pytest
from homeassistant.components import media_player, notify, tts
from homeassistant.components import notify, tts
from homeassistant.components.media_player import (
DOMAIN as DOMAIN_MP,
SERVICE_PLAY_MEDIA,
@ -24,6 +26,16 @@ async def internal_url_mock(hass: HomeAssistant) -> None:
)
@pytest.fixture(autouse=True)
async def disable_platforms() -> None:
"""Disable demo platforms."""
with patch(
"homeassistant.components.demo.COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM",
[],
):
yield
async def test_setup_legacy_platform(hass: HomeAssistant) -> None:
"""Set up the tts notify platform ."""
config = {
@ -62,7 +74,6 @@ async def test_setup_legacy_service(hass: HomeAssistant) -> None:
config = {
tts.DOMAIN: {"platform": "demo"},
media_player.DOMAIN: {"platform": "demo"},
notify.DOMAIN: {
"platform": "tts",
"name": "tts_test",