forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add switch platform to Nice G.O. (home-assistant#124237)
* Add switch platform to Nice G.O. * Replace cover with switch in switch.py * Use icon translations * Fix tests * Use constants in test_switch.py * Use ATTR_ENTITY_ID
- Loading branch information
Showing
9 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"entity": { | ||
"switch": { | ||
"vacation_mode": { | ||
"default": "mdi:beach" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Nice G.O. switch platform.""" | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
from typing import Any | ||
|
||
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import NiceGOConfigEntry | ||
from .entity import NiceGOEntity | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: NiceGOConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up Nice G.O. switch.""" | ||
coordinator = config_entry.runtime_data | ||
|
||
async_add_entities( | ||
NiceGOSwitchEntity(coordinator, device_id, device_data.name, "switch") | ||
for device_id, device_data in coordinator.data.items() | ||
) | ||
|
||
|
||
class NiceGOSwitchEntity(NiceGOEntity, SwitchEntity): | ||
"""Representation of a Nice G.O. switch.""" | ||
|
||
_attr_device_class = SwitchDeviceClass.SWITCH | ||
_attr_translation_key = "vacation_mode" | ||
|
||
@property | ||
def is_on(self) -> bool: | ||
"""Return if switch is on.""" | ||
return self.data.vacation_mode | ||
|
||
async def async_turn_on(self, **kwargs: Any) -> None: | ||
"""Turn the switch on.""" | ||
await self.coordinator.api.vacation_mode_on(self.data.id) | ||
|
||
async def async_turn_off(self, **kwargs: Any) -> None: | ||
"""Turn the switch off.""" | ||
await self.coordinator.api.vacation_mode_off(self.data.id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Nice G.O. switch tests.""" | ||
|
||
from unittest.mock import AsyncMock | ||
|
||
from homeassistant.components.switch import ( | ||
DOMAIN as SWITCH_DOMAIN, | ||
SERVICE_TURN_OFF, | ||
SERVICE_TURN_ON, | ||
) | ||
from homeassistant.const import ATTR_ENTITY_ID, Platform | ||
from homeassistant.core import HomeAssistant | ||
|
||
from . import setup_integration | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
async def test_turn_on( | ||
hass: HomeAssistant, mock_nice_go: AsyncMock, mock_config_entry: MockConfigEntry | ||
) -> None: | ||
"""Test turn on switch.""" | ||
await setup_integration(hass, mock_config_entry, [Platform.SWITCH]) | ||
await hass.services.async_call( | ||
SWITCH_DOMAIN, | ||
SERVICE_TURN_ON, | ||
{ATTR_ENTITY_ID: "switch.test_garage_1_vacation_mode"}, | ||
blocking=True, | ||
) | ||
mock_nice_go.vacation_mode_on.assert_called_once_with("1") | ||
|
||
|
||
async def test_turn_off( | ||
hass: HomeAssistant, mock_nice_go: AsyncMock, mock_config_entry: MockConfigEntry | ||
) -> None: | ||
"""Test turn off switch.""" | ||
await setup_integration(hass, mock_config_entry, [Platform.SWITCH]) | ||
await hass.services.async_call( | ||
SWITCH_DOMAIN, | ||
SERVICE_TURN_OFF, | ||
{ATTR_ENTITY_ID: "switch.test_garage_2_vacation_mode"}, | ||
blocking=True, | ||
) | ||
mock_nice_go.vacation_mode_off.assert_called_once_with("2") |