Add Signal Strength sensor to WiZ (#67411)

This commit is contained in:
J. Nick Koston 2022-02-28 18:50:26 -10:00 committed by GitHub
parent ac031cb817
commit fc4cb743bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 110 additions and 2 deletions

View File

@ -30,7 +30,13 @@ from .models import WizData
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.BINARY_SENSOR, Platform.LIGHT, Platform.NUMBER, Platform.SWITCH]
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.LIGHT,
Platform.NUMBER,
Platform.SENSOR,
Platform.SWITCH,
]
REQUEST_REFRESH_DELAY = 0.35

View File

@ -0,0 +1,65 @@
"""Support for WiZ sensors."""
from __future__ import annotations
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import SIGNAL_STRENGTH_DECIBELS_MILLIWATT
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .entity import WizEntity
from .models import WizData
SENSORS: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="rssi",
name="Signal Strength",
entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the wiz sensor."""
wiz_data: WizData = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
WizSensor(wiz_data, entry.title, description) for description in SENSORS
)
class WizSensor(WizEntity, SensorEntity):
"""Defines a WiZ sensor."""
entity_description: SensorEntityDescription
def __init__(
self, wiz_data: WizData, name: str, description: SensorEntityDescription
) -> None:
"""Initialize an WiZ sensor."""
super().__init__(wiz_data, name)
self.entity_description = description
self._attr_unique_id = f"{self._device.mac}_{description.key}"
self._attr_name = f"{name} {description.name}"
self._async_update_attrs()
@callback
def _async_update_attrs(self) -> None:
"""Handle updating _attr values."""
self._attr_native_value = self._device.state.pilotResult.get(
self.entity_description.key
)

View File

@ -199,7 +199,7 @@ def _mocked_wizlight(device, extended_white_range, bulb_type) -> wizlight:
bulb.turn_on = AsyncMock()
bulb.turn_off = AsyncMock()
bulb.updateState = AsyncMock(return_value=FAKE_STATE)
bulb.getSupportedScenes = AsyncMock(return_value=list(SCENES))
bulb.getSupportedScenes = AsyncMock(return_value=list(SCENES.values()))
bulb.start_push = AsyncMock(side_effect=_save_setup_callback)
bulb.async_close = AsyncMock()
bulb.set_speed = AsyncMock()

View File

@ -0,0 +1,37 @@
"""Tests for the sensor platform."""
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import (
FAKE_DUAL_HEAD_RGBWW_BULB,
FAKE_MAC,
_patch_discovery,
_patch_wizlight,
async_push_update,
async_setup_integration,
)
async def test_signal_strength(hass: HomeAssistant) -> None:
"""Test signal strength."""
bulb, entry = await async_setup_integration(
hass, bulb_type=FAKE_DUAL_HEAD_RGBWW_BULB
)
entity_id = "sensor.mock_title_signal_strength"
entity_registry = er.async_get(hass)
reg_entry = entity_registry.async_get(entity_id)
assert reg_entry.unique_id == f"{FAKE_MAC}_rssi"
updated_entity = entity_registry.async_update_entity(
entity_id=entity_id, disabled_by=None
)
assert not updated_entity.disabled
with _patch_discovery(), _patch_wizlight(device=bulb):
await hass.config_entries.async_reload(entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == "-55"
await async_push_update(hass, bulb, {"mac": FAKE_MAC, "rssi": -50})
assert hass.states.get(entity_id).state == "-50"