Add switch platform to Nice G.O. (#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
pull/124253/head
IceBotYT 2024-08-19 15:25:11 -04:00 committed by GitHub
parent 69652ca2ca
commit 20f7af25e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 114 additions and 3 deletions

View File

@ -11,7 +11,7 @@ from homeassistant.core import HomeAssistant
from .coordinator import NiceGOUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.COVER, Platform.LIGHT]
PLATFORMS: list[Platform] = [Platform.COVER, Platform.LIGHT, Platform.SWITCH]
type NiceGOConfigEntry = ConfigEntry[NiceGOUpdateCoordinator]

View File

@ -46,6 +46,7 @@ class NiceGODevice:
light_status: bool
fw_version: str
connected: bool
vacation_mode: bool
class NiceGOUpdateCoordinator(DataUpdateCoordinator[dict[str, NiceGODevice]]):
@ -105,6 +106,7 @@ class NiceGOUpdateCoordinator(DataUpdateCoordinator[dict[str, NiceGODevice]]):
connected = barrier_state.connectionState.connected
else:
connected = False
vacation_mode = barrier_state.reported["vcnMode"]
return NiceGODevice(
id=device_id,
@ -113,6 +115,7 @@ class NiceGOUpdateCoordinator(DataUpdateCoordinator[dict[str, NiceGODevice]]):
light_status=light_status,
fw_version=fw_version,
connected=connected,
vacation_mode=vacation_mode,
)
async def _async_update_data(self) -> dict[str, NiceGODevice]:

View File

@ -0,0 +1,9 @@
{
"entity": {
"switch": {
"vacation_mode": {
"default": "mdi:beach"
}
}
}
}

View File

@ -22,6 +22,11 @@
"light": {
"name": "[%key:component::light::title%]"
}
},
"switch": {
"vacation_mode": {
"name": "Vacation mode"
}
}
},
"issues": {

View File

@ -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)

View File

@ -49,7 +49,7 @@
"migrationStatus": "DONE",
"deviceId": "2",
"lightStatus": "0,100",
"vcnMode": false,
"vcnMode": true,
"deviceFwVersion": "1.2.3.4.5.6",
"barrierStatus": "1,100,0,0,-1,0,3,0"
},

View File

@ -9,6 +9,7 @@
'id': '1',
'light_status': True,
'name': 'Test Garage 1',
'vacation_mode': False,
}),
'2': dict({
'barrier_status': 'open',
@ -17,6 +18,7 @@
'id': '2',
'light_status': False,
'name': 'Test Garage 2',
'vacation_mode': True,
}),
}),
'entry': dict({

View File

@ -275,7 +275,7 @@ async def test_no_connection_state(
"item": {
"deviceId": "1",
"desired": '{"key": "value"}',
"reported": '{"displayName":"Test Garage 1", "migrationStatus":"DONE", "barrierStatus": "1,100,0", "deviceFwVersion": "1.0.0", "lightStatus": "1,100"}',
"reported": '{"displayName":"Test Garage 1", "migrationStatus":"DONE", "barrierStatus": "1,100,0", "deviceFwVersion": "1.0.0", "lightStatus": "1,100", "vcnMode": false}',
"connectionState": None,
"version": None,
"timestamp": None,

View File

@ -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")