Demo: remove deprecated switch entity properties (#52424)

* Demo: deprecate switch entity properties

* Fix emulated_kasa test
This commit is contained in:
Shay Levy 2021-07-02 18:50:57 +03:00 committed by GitHub
parent 5cd4471c67
commit 1eb27f7ccc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 116 additions and 3 deletions

View file

@ -10,9 +10,13 @@ from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
DEVICE_CLASS_CO,
DEVICE_CLASS_CO2,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_TEMPERATURE,
ENERGY_KILO_WATT_HOUR,
PERCENTAGE,
POWER_WATT,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
@ -67,6 +71,24 @@ async def async_setup_platform(
CONCENTRATION_PARTS_PER_MILLION,
14,
),
DemoSensor(
"sensor_5",
"Power consumption",
100,
DEVICE_CLASS_POWER,
STATE_CLASS_MEASUREMENT,
POWER_WATT,
None,
),
DemoSensor(
"sensor_6",
"Today energy",
15,
DEVICE_CLASS_ENERGY,
STATE_CLASS_MEASUREMENT,
ENERGY_KILO_WATT_HOUR,
None,
),
]
)

View file

@ -49,7 +49,6 @@ class DemoSwitch(SwitchEntity):
self._attr_icon = icon
self._attr_is_on = state
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_today_energy_kwh = 15
self._attr_unique_id = unique_id
@property
@ -63,11 +62,9 @@ class DemoSwitch(SwitchEntity):
def turn_on(self, **kwargs):
"""Turn the switch on."""
self._attr_is_on = True
self._attr_current_power_w = 100
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
"""Turn the device off."""
self._attr_is_on = False
self._attr_current_power_w = 0
self.schedule_update_ha_state()

View file

@ -0,0 +1,88 @@
"""The tests for the demo switch component."""
import pytest
from homeassistant.components.demo import DOMAIN
from homeassistant.components.switch import (
DOMAIN as SWITCH_DOMAIN,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.setup import async_setup_component
SWITCH_ENTITY_IDS = ["switch.decorative_lights", "switch.ac"]
@pytest.fixture(autouse=True)
async def setup_comp(hass):
"""Set up demo component."""
assert await async_setup_component(
hass, SWITCH_DOMAIN, {SWITCH_DOMAIN: {"platform": DOMAIN}}
)
await hass.async_block_till_done()
@pytest.mark.parametrize("switch_entity_id", SWITCH_ENTITY_IDS)
async def test_turn_on(hass, switch_entity_id):
"""Test switch turn on method."""
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: switch_entity_id},
blocking=True,
)
state = hass.states.get(switch_entity_id)
assert state.state == STATE_OFF
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: switch_entity_id},
blocking=True,
)
state = hass.states.get(switch_entity_id)
assert state.state == STATE_ON
@pytest.mark.parametrize("switch_entity_id", SWITCH_ENTITY_IDS)
async def test_turn_off(hass, switch_entity_id):
"""Test switch turn off method."""
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: switch_entity_id},
blocking=True,
)
state = hass.states.get(switch_entity_id)
assert state.state == STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: switch_entity_id},
blocking=True,
)
state = hass.states.get(switch_entity_id)
assert state.state == STATE_OFF
@pytest.mark.parametrize("switch_entity_id", SWITCH_ENTITY_IDS)
async def test_turn_off_without_entity_id(hass, switch_entity_id):
"""Test switch turn off all switches."""
await hass.services.async_call(
SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: "all"}, blocking=True
)
state = hass.states.get(switch_entity_id)
assert state.state == STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
)
state = hass.states.get(switch_entity_id)
assert state.state == STATE_OFF

View file

@ -217,6 +217,12 @@ async def test_switch_power(hass):
SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_SWITCH}, blocking=True
)
hass.states.async_set(
ENTITY_SWITCH,
STATE_ON,
attributes={ATTR_CURRENT_POWER_W: 100, ATTR_FRIENDLY_NAME: "AC"},
)
switch = hass.states.get(ENTITY_SWITCH)
assert switch.state == STATE_ON
power = switch.attributes[ATTR_CURRENT_POWER_W]