From eb85f469e955b83143cd25899b8e9426cade3700 Mon Sep 17 00:00:00 2001 From: Massimo Savazzi Date: Thu, 25 Jan 2024 22:49:03 +0100 Subject: [PATCH] Add binary sensor platform to JVC Projector (#108668) * JVC Projector Binary Sensor * Fixed PR as per request, removed Name, removed Read_Only. * Fixed as per Joostlek suggestions * Update homeassistant/components/jvc_projector/coordinator.py Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/jvc_projector/binary_sensor.py Co-authored-by: Joost Lekkerkerker * Added changes as per requests * fixed docstring * Update homeassistant/components/jvc_projector/strings.json Co-authored-by: Joost Lekkerkerker * Update homeassistant/components/jvc_projector/binary_sensor.py Co-authored-by: Joost Lekkerkerker * Added icon property to binary sensor. Removed icons.json file as not used anymore * Fixed tests * Added icons file * Update homeassistant/components/jvc_projector/icons.json Co-authored-by: Joost Lekkerkerker * Update test_binary_sensor.py --------- Co-authored-by: Joost Lekkerkerker --- CODEOWNERS | 4 +- .../components/jvc_projector/__init__.py | 2 +- .../components/jvc_projector/binary_sensor.py | 44 +++++++++++++++++++ .../components/jvc_projector/icons.json | 12 +++++ .../components/jvc_projector/manifest.json | 2 +- .../components/jvc_projector/strings.json | 7 +++ .../jvc_projector/test_binary_sensor.py | 22 ++++++++++ 7 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 homeassistant/components/jvc_projector/binary_sensor.py create mode 100644 homeassistant/components/jvc_projector/icons.json create mode 100644 tests/components/jvc_projector/test_binary_sensor.py diff --git a/CODEOWNERS b/CODEOWNERS index 89e689b4325aff..56148d9e1be63d 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -664,8 +664,8 @@ build.json @home-assistant/supervisor /tests/components/juicenet/ @jesserockz /homeassistant/components/justnimbus/ @kvanzuijlen /tests/components/justnimbus/ @kvanzuijlen -/homeassistant/components/jvc_projector/ @SteveEasley -/tests/components/jvc_projector/ @SteveEasley +/homeassistant/components/jvc_projector/ @SteveEasley @msavazzi +/tests/components/jvc_projector/ @SteveEasley @msavazzi /homeassistant/components/kaiterra/ @Michsior14 /homeassistant/components/kaleidescape/ @SteveEasley /tests/components/kaleidescape/ @SteveEasley diff --git a/homeassistant/components/jvc_projector/__init__.py b/homeassistant/components/jvc_projector/__init__.py index 996d745a1d5bfc..33af1d315f78f4 100644 --- a/homeassistant/components/jvc_projector/__init__.py +++ b/homeassistant/components/jvc_projector/__init__.py @@ -18,7 +18,7 @@ from .const import DOMAIN from .coordinator import JvcProjectorDataUpdateCoordinator -PLATFORMS = [Platform.REMOTE] +PLATFORMS = [Platform.BINARY_SENSOR, Platform.REMOTE] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/jvc_projector/binary_sensor.py b/homeassistant/components/jvc_projector/binary_sensor.py new file mode 100644 index 00000000000000..7e8788aa0a63ab --- /dev/null +++ b/homeassistant/components/jvc_projector/binary_sensor.py @@ -0,0 +1,44 @@ +"""Binary Sensor platform for JVC Projector integration.""" + +from __future__ import annotations + +from jvcprojector import const + +from homeassistant.components.binary_sensor import BinarySensorEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import JvcProjectorDataUpdateCoordinator +from .const import DOMAIN +from .entity import JvcProjectorEntity + +ON_STATUS = (const.ON, const.WARMING) + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up the JVC Projector platform from a config entry.""" + coordinator: JvcProjectorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + + async_add_entities([JvcBinarySensor(coordinator)]) + + +class JvcBinarySensor(JvcProjectorEntity, BinarySensorEntity): + """The entity class for JVC Projector Binary Sensor.""" + + _attr_translation_key = "jvc_power" + + def __init__( + self, + coordinator: JvcProjectorDataUpdateCoordinator, + ) -> None: + """Initialize the JVC Projector sensor.""" + super().__init__(coordinator) + self._attr_unique_id = f"{coordinator.device.mac}_power" + + @property + def is_on(self) -> bool: + """Return true if the JVC is on.""" + return self.coordinator.data["power"] in ON_STATUS diff --git a/homeassistant/components/jvc_projector/icons.json b/homeassistant/components/jvc_projector/icons.json new file mode 100644 index 00000000000000..94e2ec41cf6275 --- /dev/null +++ b/homeassistant/components/jvc_projector/icons.json @@ -0,0 +1,12 @@ +{ + "entity": { + "binary_sensor": { + "jvc_power": { + "default": "mdi:projector-off", + "state": { + "on": "mdi:projector" + } + } + } + } +} diff --git a/homeassistant/components/jvc_projector/manifest.json b/homeassistant/components/jvc_projector/manifest.json index a7c08bb9f512e9..de7e77197f26c4 100644 --- a/homeassistant/components/jvc_projector/manifest.json +++ b/homeassistant/components/jvc_projector/manifest.json @@ -1,7 +1,7 @@ { "domain": "jvc_projector", "name": "JVC Projector", - "codeowners": ["@SteveEasley"], + "codeowners": ["@SteveEasley", "@msavazzi"], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/jvc_projector", "integration_type": "device", diff --git a/homeassistant/components/jvc_projector/strings.json b/homeassistant/components/jvc_projector/strings.json index 6fdc5b4d12f560..06efdc8f9aa740 100644 --- a/homeassistant/components/jvc_projector/strings.json +++ b/homeassistant/components/jvc_projector/strings.json @@ -31,5 +31,12 @@ "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]" } + }, + "entity": { + "binary_sensor": { + "jvc_power": { + "name": "[%key:component::sensor::entity_component::power::name%]" + } + } } } diff --git a/tests/components/jvc_projector/test_binary_sensor.py b/tests/components/jvc_projector/test_binary_sensor.py new file mode 100644 index 00000000000000..b327538991ca3c --- /dev/null +++ b/tests/components/jvc_projector/test_binary_sensor.py @@ -0,0 +1,22 @@ +"""Tests for the JVC Projector binary sensor device.""" + +from unittest.mock import MagicMock + +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from tests.common import MockConfigEntry + +ENTITY_ID = "binary_sensor.jvc_projector_power" + + +async def test_entity_state( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_device: MagicMock, + mock_integration: MockConfigEntry, +) -> None: + """Tests entity state is registered.""" + entity = hass.states.get(ENTITY_ID) + assert entity + assert entity_registry.async_get(entity.entity_id)