Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add switch platform to Nice G.O. #124237

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/nice_go/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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]

Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/nice_go/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class NiceGODevice:
light_status: bool
fw_version: str
connected: bool
vacation_mode: bool


class NiceGOUpdateCoordinator(DataUpdateCoordinator[dict[str, NiceGODevice]]):
Expand Down Expand Up @@ -105,6 +106,7 @@ async def _parse_barrier(self, barrier_state: BarrierState) -> NiceGODevice | No
connected = barrier_state.connectionState.connected
else:
connected = False
vacation_mode = barrier_state.reported["vcnMode"]

return NiceGODevice(
id=device_id,
Expand All @@ -113,6 +115,7 @@ async def _parse_barrier(self, barrier_state: BarrierState) -> NiceGODevice | No
light_status=light_status,
fw_version=fw_version,
connected=connected,
vacation_mode=vacation_mode,
)

async def _async_update_data(self) -> dict[str, NiceGODevice]:
Expand Down
9 changes: 9 additions & 0 deletions homeassistant/components/nice_go/icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"entity": {
"switch": {
"vacation_mode": {
"default": "mdi:beach"
}
}
}
}
5 changes: 5 additions & 0 deletions homeassistant/components/nice_go/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"light": {
"name": "[%key:component::light::title%]"
}
},
"switch": {
"vacation_mode": {
"name": "Vacation mode"
}
}
},
"issues": {
Expand Down
49 changes: 49 additions & 0 deletions homeassistant/components/nice_go/switch.py
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")
IceBotYT marked this conversation as resolved.
Show resolved Hide resolved
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)
2 changes: 1 addition & 1 deletion tests/components/nice_go/fixtures/get_all_barriers.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 2 additions & 0 deletions tests/components/nice_go/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'id': '1',
'light_status': True,
'name': 'Test Garage 1',
'vacation_mode': False,
}),
'2': dict({
'barrier_status': 'open',
Expand All @@ -17,6 +18,7 @@
'id': '2',
'light_status': False,
'name': 'Test Garage 2',
'vacation_mode': True,
}),
}),
'entry': dict({
Expand Down
2 changes: 1 addition & 1 deletion tests/components/nice_go/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions tests/components/nice_go/test_switch.py
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")