diff --git a/homeassistant/components/demo/sensor.py b/homeassistant/components/demo/sensor.py index 817cbd435d1..488c34be983 100644 --- a/homeassistant/components/demo/sensor.py +++ b/homeassistant/components/demo/sensor.py @@ -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, + ), ] ) diff --git a/homeassistant/components/demo/switch.py b/homeassistant/components/demo/switch.py index 13853959a12..84554bf0db1 100644 --- a/homeassistant/components/demo/switch.py +++ b/homeassistant/components/demo/switch.py @@ -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() diff --git a/tests/components/demo/test_switch.py b/tests/components/demo/test_switch.py new file mode 100644 index 00000000000..f35bc14db34 --- /dev/null +++ b/tests/components/demo/test_switch.py @@ -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 diff --git a/tests/components/emulated_kasa/test_init.py b/tests/components/emulated_kasa/test_init.py index 60f4f5be1db..d68221d84fd 100644 --- a/tests/components/emulated_kasa/test_init.py +++ b/tests/components/emulated_kasa/test_init.py @@ -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]