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 gree water tank binary sensor #114435

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/gree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

_LOGGER = logging.getLogger(__name__)

PLATFORMS = [Platform.CLIMATE, Platform.SWITCH]
PLATFORMS = [Platform.CLIMATE, Platform.SWITCH, Platform.BINARY_SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down
83 changes: 83 additions & 0 deletions homeassistant/components/gree/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Support for interface with a Gree climate systems."""

from __future__ import annotations

import logging

from collections.abc import Callable
from dataclasses import dataclass

from greeclimate.device import Device

from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import COORDINATORS, DISPATCH_DEVICE_DISCOVERED, DOMAIN
from .entity import GreeEntity

_LOGGER = logging.getLogger(__name__)


@dataclass(kw_only=True, frozen=True)
class GreeBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes a Gree binary sensor entity."""

get_value_fn: Callable[[Device], bool]


GREE_BINARY_SENSORS: tuple[GreeBinarySensorEntityDescription, ...] = (
GreeBinarySensorEntityDescription(
key="Water Tank Full",
translation_key="water_full",
get_value_fn=lambda d: d.water_full,
),
)


async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Gree HVAC device from a config entry."""

@callback
def init_device(coordinator):
"""Register the device."""

async_add_entities(
GreeBinarySensor(coordinator=coordinator, description=description)
for description in GREE_BINARY_SENSORS
)

for coordinator in hass.data[DOMAIN][COORDINATORS]:
init_device(coordinator)

entry.async_on_unload(
async_dispatcher_connect(hass, DISPATCH_DEVICE_DISCOVERED, init_device)
)


class GreeBinarySensor(GreeEntity, BinarySensorEntity):
"""Generic Gree binary sensor entity."""

entity_description: GreeBinarySensorEntityDescription

def __init__(
self, coordinator, description: GreeBinarySensorEntityDescription
) -> None:
"""Initialize the Gree device."""
self.entity_description = description

super().__init__(coordinator, description.key)

@property
def is_on(self) -> bool:
"""Return if the state is turned on."""
return self.entity_description.get_value_fn(self.coordinator.device)
5 changes: 5 additions & 0 deletions homeassistant/components/gree/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"health_mode": {
"name": "Health mode"
}
},
"binary_sensor": {
"water_full": {
"name": "Water Tank Full"
}
}
}
}
52 changes: 52 additions & 0 deletions tests/components/gree/snapshots/test_binary_sensor.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# serializer version: 1
# name: test_entity_state
list([
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'binary_sensor',
'friendly_name': 'fake-device-1 Water Tank Full',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.fake_device_1_water_full',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
])
# ---
# name: test_registry_settings
list([
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.fake_device_1_water_full',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Water Tank Full',
'platform': 'gree',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'water_full',
'unique_id': 'aabbcc112233_Water Tank Full',
'unit_of_measurement': None,
}),
])
# ---
51 changes: 51 additions & 0 deletions tests/components/gree/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Tests for gree component."""

from unittest.mock import patch

import pytest
from syrupy.assertion import SnapshotAssertion

from homeassistant.components.gree.const import DOMAIN as GREE_DOMAIN
from homeassistant.components.binary_sensor import DOMAIN
from homeassistant.const import STATE_OFF
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component

from tests.common import MockConfigEntry

ENTITY_ID_WATER_FULL = f"{DOMAIN}.fake_device_1_water_full"


async def async_setup_gree(hass: HomeAssistant) -> MockConfigEntry:
"""Set up the gree binary_sensor platform."""
entry = MockConfigEntry(domain=GREE_DOMAIN)
entry.add_to_hass(hass)
await async_setup_component(hass, GREE_DOMAIN, {GREE_DOMAIN: {DOMAIN: {}}})
await hass.async_block_till_done()
return entry


@patch("homeassistant.components.gree.PLATFORMS", [DOMAIN])
async def test_registry_settings(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test for entity registry settings (disabled_by, unique_id)."""
entry = await async_setup_gree(hass)

state = er.async_entries_for_config_entry(entity_registry, entry.entry_id)
assert state == snapshot


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_entity_state(
hass: HomeAssistant,
) -> None:
"""Test for reading state of entity."""
await async_setup_gree(hass)

state = hass.states.get(ENTITY_ID_WATER_FULL)
assert state is not None
assert state.state == STATE_OFF