Skip to content

Commit

Permalink
Add switch platform to Nice G.O.
Browse files Browse the repository at this point in the history
  • Loading branch information
IceBotYT committed Aug 19, 2024
1 parent 50f3c89 commit b6dca76
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 6 deletions.
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
2 changes: 1 addition & 1 deletion homeassistant/components/nice_go/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"documentation": "https://www.home-assistant.io/integrations/nice_go",
"iot_class": "cloud_push",
"loggers": ["nice-go"],
"requirements": ["nice-go==0.1.6"]
"requirements": ["nice-go==0.2.0"]
}
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
50 changes: 50 additions & 0 deletions homeassistant/components/nice_go/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""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. cover."""
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_icon = "mdi:beach"
_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 requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ nextdns==3.1.0
nibe==2.11.0

# homeassistant.components.nice_go
nice-go==0.1.6
nice-go==0.2.0

# homeassistant.components.niko_home_control
niko-home-control==0.2.1
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ nextdns==3.1.0
nibe==2.11.0

# homeassistant.components.nice_go
nice-go==0.1.6
nice-go==0.2.0

# homeassistant.components.nfandroidtv
notifications-android-tv==0.1.5
Expand Down
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: 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
37 changes: 37 additions & 0 deletions tests/components/nice_go/test_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Nice G.O. switch tests."""

from unittest.mock import AsyncMock

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, ["switch"])
await hass.services.async_call(
"switch",
"turn_on",
{"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, ["switch"])
await hass.services.async_call(
"switch",
"turn_off",
{"entity_id": "switch.test_garage_2_vacation_mode"},
blocking=True,
)
mock_nice_go.vacation_mode_off.assert_called_once_with("2")

0 comments on commit b6dca76

Please sign in to comment.