From 8bc4fcc342657f5b2c9e1be014b460c220b336d1 Mon Sep 17 00:00:00 2001 From: Steven Stallion Date: Mon, 13 Jan 2025 14:41:09 -0600 Subject: [PATCH] Add SensorPush Cloud integration --- .strict-typing | 1 + CODEOWNERS | 2 + homeassistant/brands/sensorpush.json | 5 + .../components/sensorpush_cloud/__init__.py | 28 + .../sensorpush_cloud/config_flow.py | 65 + .../components/sensorpush_cloud/const.py | 12 + .../sensorpush_cloud/coordinator.py | 45 + .../components/sensorpush_cloud/manifest.json | 11 + .../sensorpush_cloud/quality_scale.yaml | 68 + .../components/sensorpush_cloud/sensor.py | 157 +++ .../components/sensorpush_cloud/strings.json | 40 + homeassistant/generated/config_flows.py | 1 + homeassistant/generated/integrations.json | 17 +- mypy.ini | 10 + requirements_all.txt | 6 + requirements_test_all.txt | 6 + tests/components/sensorpush_cloud/__init__.py | 1 + tests/components/sensorpush_cloud/conftest.py | 62 + tests/components/sensorpush_cloud/const.py | 32 + .../snapshots/test_sensor.ambr | 1243 +++++++++++++++++ .../sensorpush_cloud/test_config_flow.py | 95 ++ .../sensorpush_cloud/test_sensor.py | 29 + 22 files changed, 1933 insertions(+), 3 deletions(-) create mode 100644 homeassistant/brands/sensorpush.json create mode 100644 homeassistant/components/sensorpush_cloud/__init__.py create mode 100644 homeassistant/components/sensorpush_cloud/config_flow.py create mode 100644 homeassistant/components/sensorpush_cloud/const.py create mode 100644 homeassistant/components/sensorpush_cloud/coordinator.py create mode 100644 homeassistant/components/sensorpush_cloud/manifest.json create mode 100644 homeassistant/components/sensorpush_cloud/quality_scale.yaml create mode 100644 homeassistant/components/sensorpush_cloud/sensor.py create mode 100644 homeassistant/components/sensorpush_cloud/strings.json create mode 100644 tests/components/sensorpush_cloud/__init__.py create mode 100644 tests/components/sensorpush_cloud/conftest.py create mode 100644 tests/components/sensorpush_cloud/const.py create mode 100644 tests/components/sensorpush_cloud/snapshots/test_sensor.ambr create mode 100644 tests/components/sensorpush_cloud/test_config_flow.py create mode 100644 tests/components/sensorpush_cloud/test_sensor.py diff --git a/.strict-typing b/.strict-typing index b83bb0f6a95f8c..2f1c2e96b39a6e 100644 --- a/.strict-typing +++ b/.strict-typing @@ -428,6 +428,7 @@ homeassistant.components.select.* homeassistant.components.sensibo.* homeassistant.components.sensirion_ble.* homeassistant.components.sensor.* +homeassistant.components.sensorpush_cloud.* homeassistant.components.sensoterra.* homeassistant.components.senz.* homeassistant.components.sfr_box.* diff --git a/CODEOWNERS b/CODEOWNERS index bf8b1502a107f7..8a6f9ddf1e400a 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1333,6 +1333,8 @@ build.json @home-assistant/supervisor /tests/components/sensorpro/ @bdraco /homeassistant/components/sensorpush/ @bdraco /tests/components/sensorpush/ @bdraco +/homeassistant/components/sensorpush_cloud/ @sstallion +/tests/components/sensorpush_cloud/ @sstallion /homeassistant/components/sensoterra/ @markruys /tests/components/sensoterra/ @markruys /homeassistant/components/sentry/ @dcramer @frenck diff --git a/homeassistant/brands/sensorpush.json b/homeassistant/brands/sensorpush.json new file mode 100644 index 00000000000000..b7e528948f86c9 --- /dev/null +++ b/homeassistant/brands/sensorpush.json @@ -0,0 +1,5 @@ +{ + "domain": "sensorpush", + "name": "SensorPush", + "integrations": ["sensorpush", "sensorpush_cloud"] +} diff --git a/homeassistant/components/sensorpush_cloud/__init__.py b/homeassistant/components/sensorpush_cloud/__init__.py new file mode 100644 index 00000000000000..2d9d299c132c28 --- /dev/null +++ b/homeassistant/components/sensorpush_cloud/__init__.py @@ -0,0 +1,28 @@ +"""The SensorPush Cloud integration.""" + +from __future__ import annotations + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + +from .coordinator import SensorPushCloudConfigEntry, SensorPushCloudCoordinator + +PLATFORMS: list[Platform] = [Platform.SENSOR] + + +async def async_setup_entry( + hass: HomeAssistant, entry: SensorPushCloudConfigEntry +) -> bool: + """Set up SensorPush Cloud from a config entry.""" + coordinator = SensorPushCloudCoordinator(hass, entry) + entry.runtime_data = coordinator + await coordinator.async_config_entry_first_refresh() + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + return True + + +async def async_unload_entry( + hass: HomeAssistant, entry: SensorPushCloudConfigEntry +) -> bool: + """Unload a config entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/sensorpush_cloud/config_flow.py b/homeassistant/components/sensorpush_cloud/config_flow.py new file mode 100644 index 00000000000000..6603ec0504d2fb --- /dev/null +++ b/homeassistant/components/sensorpush_cloud/config_flow.py @@ -0,0 +1,65 @@ +"""Config flow for the SensorPush Cloud integration.""" + +from __future__ import annotations + +from typing import Any + +from sensorpush_ha import SensorPushCloudApi, SensorPushCloudError +import voluptuous as vol + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import CONF_EMAIL, CONF_PASSWORD +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.selector import ( + TextSelector, + TextSelectorConfig, + TextSelectorType, +) + +from .const import DOMAIN, LOGGER + + +class SensorPushCloudConfigFlow(ConfigFlow, domain=DOMAIN): + """Handle a config flow for SensorPush Cloud.""" + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the initial step.""" + errors: dict[str, str] = {} + if user_input is not None: + email, password = user_input[CONF_EMAIL], user_input[CONF_PASSWORD] + await self.async_set_unique_id(email) + self._abort_if_unique_id_configured() + clientsession = async_get_clientsession(self.hass) + api = SensorPushCloudApi(email, password, clientsession) + try: + await api.async_authorize() + except SensorPushCloudError: + LOGGER.exception("Invalid authentication") + errors["base"] = "invalid_auth" + except Exception: # noqa: BLE001 + LOGGER.exception("Unexpected error") + errors["base"] = "unknown" + else: + return self.async_create_entry(title=email, data=user_input) + + return self.async_show_form( + step_id="user", + data_schema=vol.Schema( + { + vol.Required(CONF_EMAIL): TextSelector( + TextSelectorConfig( + type=TextSelectorType.EMAIL, autocomplete="username" + ) + ), + vol.Required(CONF_PASSWORD): TextSelector( + TextSelectorConfig( + type=TextSelectorType.PASSWORD, + autocomplete="current-password", + ) + ), + } + ), + errors=errors, + ) diff --git a/homeassistant/components/sensorpush_cloud/const.py b/homeassistant/components/sensorpush_cloud/const.py new file mode 100644 index 00000000000000..9e66dacfabad1f --- /dev/null +++ b/homeassistant/components/sensorpush_cloud/const.py @@ -0,0 +1,12 @@ +"""Constants for the SensorPush Cloud integration.""" + +from datetime import timedelta +import logging +from typing import Final + +LOGGER = logging.getLogger(__package__) + +DOMAIN: Final = "sensorpush_cloud" + +UPDATE_INTERVAL: Final = timedelta(seconds=60) +MAX_TIME_BETWEEN_UPDATES: Final = UPDATE_INTERVAL * 60 diff --git a/homeassistant/components/sensorpush_cloud/coordinator.py b/homeassistant/components/sensorpush_cloud/coordinator.py new file mode 100644 index 00000000000000..9885538b55a95d --- /dev/null +++ b/homeassistant/components/sensorpush_cloud/coordinator.py @@ -0,0 +1,45 @@ +"""Coordinator for the SensorPush Cloud integration.""" + +from __future__ import annotations + +from sensorpush_ha import ( + SensorPushCloudApi, + SensorPushCloudData, + SensorPushCloudError, + SensorPushCloudHelper, +) + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_EMAIL, CONF_PASSWORD +from homeassistant.core import HomeAssistant +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +from .const import LOGGER, UPDATE_INTERVAL + +type SensorPushCloudConfigEntry = ConfigEntry[SensorPushCloudCoordinator] + + +class SensorPushCloudCoordinator(DataUpdateCoordinator[dict[str, SensorPushCloudData]]): + """SensorPush Cloud coordinator.""" + + def __init__(self, hass: HomeAssistant, entry: SensorPushCloudConfigEntry) -> None: + """Initialize the coordinator.""" + super().__init__( + hass, + LOGGER, + name=entry.title, + update_interval=UPDATE_INTERVAL, + config_entry=entry, + ) + email, password = entry.data[CONF_EMAIL], entry.data[CONF_PASSWORD] + clientsession = async_get_clientsession(hass) + api = SensorPushCloudApi(email, password, clientsession) + self.helper = SensorPushCloudHelper(api) + + async def _async_update_data(self) -> dict[str, SensorPushCloudData]: + """Fetch data from API endpoints.""" + try: + return await self.helper.async_get_data() + except SensorPushCloudError as e: + raise UpdateFailed(e) from e diff --git a/homeassistant/components/sensorpush_cloud/manifest.json b/homeassistant/components/sensorpush_cloud/manifest.json new file mode 100644 index 00000000000000..2e1f2e3bcf5524 --- /dev/null +++ b/homeassistant/components/sensorpush_cloud/manifest.json @@ -0,0 +1,11 @@ +{ + "domain": "sensorpush_cloud", + "name": "SensorPush Cloud", + "codeowners": ["@sstallion"], + "config_flow": true, + "documentation": "https://www.home-assistant.io/integrations/sensorpush_cloud", + "iot_class": "cloud_polling", + "loggers": ["sensorpush_api", "sensorpush_ha"], + "quality_scale": "bronze", + "requirements": ["sensorpush-api==2.1.1", "sensorpush-ha==1.2.0"] +} diff --git a/homeassistant/components/sensorpush_cloud/quality_scale.yaml b/homeassistant/components/sensorpush_cloud/quality_scale.yaml new file mode 100644 index 00000000000000..d05bb2440f9072 --- /dev/null +++ b/homeassistant/components/sensorpush_cloud/quality_scale.yaml @@ -0,0 +1,68 @@ +rules: + # Bronze + action-setup: + status: exempt + comment: Integration does not register custom actions. + appropriate-polling: done + brands: done + common-modules: done + config-flow-test-coverage: done + config-flow: done + dependency-transparency: done + docs-actions: + status: exempt + comment: Integration does not register custom actions. + docs-high-level-description: done + docs-installation-instructions: done + docs-removal-instructions: done + entity-event-setup: done + entity-unique-id: done + has-entity-name: done + runtime-data: done + test-before-configure: done + test-before-setup: done + unique-config-entry: done + + # Silver + action-exceptions: + status: exempt + comment: Integration does not register custom actions. + config-entry-unloading: done + docs-configuration-parameters: + status: exempt + comment: Integration does not support options flow. + docs-installation-parameters: todo + entity-unavailable: done + integration-owner: done + log-when-unavailable: done + parallel-updates: done + reauthentication-flow: todo + test-coverage: todo + + # Gold + devices: todo + diagnostics: todo + discovery-update-info: todo + discovery: todo + docs-data-update: todo + docs-examples: todo + docs-known-limitations: done + docs-supported-devices: done + docs-supported-functions: done + docs-troubleshooting: todo + docs-use-cases: todo + dynamic-devices: todo + entity-category: todo + entity-device-class: done + entity-disabled-by-default: done + entity-translations: todo + exception-translations: todo + icon-translations: todo + reconfiguration-flow: todo + repair-issues: todo + stale-devices: todo + + # Platinum + async-dependency: done + inject-websession: done + strict-typing: done diff --git a/homeassistant/components/sensorpush_cloud/sensor.py b/homeassistant/components/sensorpush_cloud/sensor.py new file mode 100644 index 00000000000000..b7b1c72a82f222 --- /dev/null +++ b/homeassistant/components/sensorpush_cloud/sensor.py @@ -0,0 +1,157 @@ +"""Support for SensorPush Cloud sensors.""" + +from __future__ import annotations + +from typing import Final + +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) +from homeassistant.const import ( + ATTR_TEMPERATURE, + PERCENTAGE, + SIGNAL_STRENGTH_DECIBELS_MILLIWATT, + UnitOfElectricPotential, + UnitOfLength, + UnitOfPressure, + UnitOfTemperature, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.typing import StateType +from homeassistant.helpers.update_coordinator import CoordinatorEntity +from homeassistant.util import dt as dt_util + +from .const import DOMAIN, MAX_TIME_BETWEEN_UPDATES +from .coordinator import SensorPushCloudConfigEntry, SensorPushCloudCoordinator + +ATTR_ALTITUDE: Final = "altitude" +ATTR_ATMOSPHERIC_PRESSURE: Final = "atmospheric_pressure" +ATTR_BATTERY_VOLTAGE: Final = "battery_voltage" +ATTR_DEWPOINT: Final = "dewpoint" +ATTR_HUMIDITY: Final = "humidity" +ATTR_SIGNAL_STRENGTH: Final = "signal_strength" +ATTR_VAPOR_PRESSURE: Final = "vapor_pressure" + +# Coordinator is used to centralize the data updates +PARALLEL_UPDATES = 0 + +SENSORS: Final[tuple[SensorEntityDescription, ...]] = ( + SensorEntityDescription( + key=ATTR_ALTITUDE, + device_class=SensorDeviceClass.DISTANCE, + entity_registry_enabled_default=False, + translation_key="altitude", + native_unit_of_measurement=UnitOfLength.FEET, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_ATMOSPHERIC_PRESSURE, + device_class=SensorDeviceClass.ATMOSPHERIC_PRESSURE, + entity_registry_enabled_default=False, + native_unit_of_measurement=UnitOfPressure.INHG, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_BATTERY_VOLTAGE, + device_class=SensorDeviceClass.VOLTAGE, + entity_registry_enabled_default=False, + translation_key="battery_voltage", + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_DEWPOINT, + device_class=SensorDeviceClass.TEMPERATURE, + entity_registry_enabled_default=False, + translation_key="dewpoint", + native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_HUMIDITY, + device_class=SensorDeviceClass.HUMIDITY, + native_unit_of_measurement=PERCENTAGE, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_SIGNAL_STRENGTH, + device_class=SensorDeviceClass.SIGNAL_STRENGTH, + entity_registry_enabled_default=False, + native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_TEMPERATURE, + device_class=SensorDeviceClass.TEMPERATURE, + native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT, + state_class=SensorStateClass.MEASUREMENT, + ), + SensorEntityDescription( + key=ATTR_VAPOR_PRESSURE, + device_class=SensorDeviceClass.PRESSURE, + entity_registry_enabled_default=False, + translation_key="vapor_pressure", + native_unit_of_measurement=UnitOfPressure.KPA, + state_class=SensorStateClass.MEASUREMENT, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: SensorPushCloudConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up SensorPush Cloud sensors.""" + coordinator = entry.runtime_data + async_add_entities( + SensorPushCloudSensor(coordinator, entity_description, device_id) + for entity_description in SENSORS + for device_id in coordinator.data + ) + + +class SensorPushCloudSensor( + CoordinatorEntity[SensorPushCloudCoordinator], SensorEntity +): + """SensorPush Cloud sensor.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: SensorPushCloudCoordinator, + entity_description: SensorEntityDescription, + device_id: str, + ) -> None: + """Initialize the sensor.""" + super().__init__(coordinator) + self.entity_description = entity_description + self.device_id = device_id + + device = coordinator.data[device_id] + self._attr_unique_id = f"{device.device_id}_{entity_description.key}" + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, device.device_id)}, + manufacturer=device.manufacturer, + model=device.model, + name=device.name, + ) + + @property + def available(self) -> bool: + """Return true if entity is available.""" + if self.device_id in self.coordinator.data: + last_update = self.coordinator.data[self.device_id].last_update + return dt_util.utcnow() < (last_update + MAX_TIME_BETWEEN_UPDATES) + return super().available + + @property + def native_value(self) -> StateType: + """Return the value reported by the sensor.""" + return self.coordinator.data[self.device_id][self.entity_description.key] diff --git a/homeassistant/components/sensorpush_cloud/strings.json b/homeassistant/components/sensorpush_cloud/strings.json new file mode 100644 index 00000000000000..8467a123b6f806 --- /dev/null +++ b/homeassistant/components/sensorpush_cloud/strings.json @@ -0,0 +1,40 @@ +{ + "config": { + "step": { + "user": { + "description": "To activate API access, log in to the [Gateway Cloud Dashboard](https://dashboard.sensorpush.com/) and agree to the terms of service. Devices are not available until activated with the SensorPush app on iOS or Android.", + "data": { + "email": "[%key:common::config_flow::data::email%]", + "password": "[%key:common::config_flow::data::password%]" + }, + "data_description": { + "email": "The email address used to log in to the SensorPush Gateway Cloud Dashboard", + "password": "The password used to log in to the SensorPush Gateway Cloud Dashboard" + } + } + }, + "error": { + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "unknown": "[%key:common::config_flow::error::unknown%]" + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_account%]" + } + }, + "entity": { + "sensor": { + "altitude": { + "name": "Altitude" + }, + "battery_voltage": { + "name": "Battery voltage" + }, + "dewpoint": { + "name": "Dew point" + }, + "vapor_pressure": { + "name": "Vapor pressure" + } + } + } +} diff --git a/homeassistant/generated/config_flows.py b/homeassistant/generated/config_flows.py index b393e5c885195b..e0a3eaf3b9f97a 100644 --- a/homeassistant/generated/config_flows.py +++ b/homeassistant/generated/config_flows.py @@ -540,6 +540,7 @@ "sensirion_ble", "sensorpro", "sensorpush", + "sensorpush_cloud", "sensoterra", "sentry", "senz", diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 2ee871964c9378..89051dc39c7693 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -5540,9 +5540,20 @@ }, "sensorpush": { "name": "SensorPush", - "integration_type": "hub", - "config_flow": true, - "iot_class": "local_push" + "integrations": { + "sensorpush": { + "integration_type": "hub", + "config_flow": true, + "iot_class": "local_push", + "name": "SensorPush" + }, + "sensorpush_cloud": { + "integration_type": "hub", + "config_flow": true, + "iot_class": "cloud_polling", + "name": "SensorPush Cloud" + } + } }, "sensoterra": { "name": "Sensoterra", diff --git a/mypy.ini b/mypy.ini index 4eb6bdff80bf1b..c6c2bb6c76807d 100644 --- a/mypy.ini +++ b/mypy.ini @@ -4036,6 +4036,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.sensorpush_cloud.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.sensoterra.*] check_untyped_defs = true disallow_incomplete_defs = true diff --git a/requirements_all.txt b/requirements_all.txt index b918cc3e944f21..af69242b8bdb5b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2680,9 +2680,15 @@ sensirion-ble==0.1.1 # homeassistant.components.sensorpro sensorpro-ble==0.5.3 +# homeassistant.components.sensorpush_cloud +sensorpush-api==2.1.1 + # homeassistant.components.sensorpush sensorpush-ble==1.7.1 +# homeassistant.components.sensorpush_cloud +sensorpush-ha==1.2.0 + # homeassistant.components.sensoterra sensoterra==2.0.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 25dc0470bfd9c3..13875d7c745006 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -2159,9 +2159,15 @@ sensirion-ble==0.1.1 # homeassistant.components.sensorpro sensorpro-ble==0.5.3 +# homeassistant.components.sensorpush_cloud +sensorpush-api==2.1.1 + # homeassistant.components.sensorpush sensorpush-ble==1.7.1 +# homeassistant.components.sensorpush_cloud +sensorpush-ha==1.2.0 + # homeassistant.components.sensoterra sensoterra==2.0.1 diff --git a/tests/components/sensorpush_cloud/__init__.py b/tests/components/sensorpush_cloud/__init__.py new file mode 100644 index 00000000000000..2a5d148692c554 --- /dev/null +++ b/tests/components/sensorpush_cloud/__init__.py @@ -0,0 +1 @@ +"""Tests for the SensorPush Cloud integration.""" diff --git a/tests/components/sensorpush_cloud/conftest.py b/tests/components/sensorpush_cloud/conftest.py new file mode 100644 index 00000000000000..cccee28d32d296 --- /dev/null +++ b/tests/components/sensorpush_cloud/conftest.py @@ -0,0 +1,62 @@ +"""Common fixtures for the SensorPush Cloud tests.""" + +from __future__ import annotations + +from collections.abc import Generator +from unittest.mock import AsyncMock, patch + +import pytest +from sensorpush_ha import SensorPushCloudApi, SensorPushCloudHelper + +from homeassistant.components.sensorpush_cloud.const import DOMAIN +from homeassistant.const import CONF_EMAIL + +from .const import CONF_DATA, MOCK_DATA + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_api(request: pytest.FixtureRequest) -> Generator[AsyncMock]: + """Override SensorPushCloudApi.""" + mock_api = AsyncMock(SensorPushCloudApi) + with ( + patch( + "homeassistant.components.sensorpush_cloud.config_flow.SensorPushCloudApi", + return_value=mock_api, + ), + ): + yield mock_api + + +@pytest.fixture +def mock_helper(request: pytest.FixtureRequest) -> Generator[AsyncMock]: + """Override SensorPushCloudHelper.""" + mock_helper = AsyncMock(SensorPushCloudHelper) + mock_helper.async_get_data.return_value = MOCK_DATA + with ( + patch( + "homeassistant.components.sensorpush_cloud.coordinator.SensorPushCloudHelper", + return_value=mock_helper, + ), + ): + yield mock_helper + + +@pytest.fixture +def mock_config_entry( + request: pytest.FixtureRequest, +) -> MockConfigEntry: + """ConfigEntry mock.""" + return MockConfigEntry( + domain=DOMAIN, data=CONF_DATA, unique_id=CONF_DATA[CONF_EMAIL] + ) + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.sensorpush_cloud.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry diff --git a/tests/components/sensorpush_cloud/const.py b/tests/components/sensorpush_cloud/const.py new file mode 100644 index 00000000000000..1efc4ea445ac05 --- /dev/null +++ b/tests/components/sensorpush_cloud/const.py @@ -0,0 +1,32 @@ +"""Constants for the SensorPush Cloud tests.""" + +from sensorpush_ha import SensorPushCloudData + +from homeassistant.const import CONF_EMAIL, CONF_PASSWORD +from homeassistant.util import dt as dt_util + +CONF_DATA = { + CONF_EMAIL: "test@example.com", + CONF_PASSWORD: "test-password", +} + +NUM_MOCK_DEVICES = 3 + +MOCK_DATA = { + f"test-sensor-device-id-{i}": SensorPushCloudData( + device_id=f"test-sensor-device-id-{i}", + manufacturer=f"test-sensor-manufacturer-{i}", + model=f"test-sensor-model-{i}", + name=f"test-sensor-name-{i}", + altitude=0.0, + atmospheric_pressure=0.0, + battery_voltage=0.0, + dewpoint=0.0, + humidity=0.0, + last_update=dt_util.utcnow(), + signal_strength=0.0, + temperature=0.0, + vapor_pressure=0.0, + ) + for i in range(NUM_MOCK_DEVICES) +} diff --git a/tests/components/sensorpush_cloud/snapshots/test_sensor.ambr b/tests/components/sensorpush_cloud/snapshots/test_sensor.ambr new file mode 100644 index 00000000000000..dd3d5ba65d7517 --- /dev/null +++ b/tests/components/sensorpush_cloud/snapshots/test_sensor.ambr @@ -0,0 +1,1243 @@ +# serializer version: 1 +# name: test_sensors[sensor.test_sensor_name_0_altitude-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_0_altitude', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Altitude', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'altitude', + 'unique_id': 'test-sensor-device-id-0_altitude', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_altitude-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'distance', + 'friendly_name': 'test-sensor-name-0 Altitude', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_0_altitude', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_atmospheric_pressure-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_0_atmospheric_pressure', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Atmospheric pressure', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-0_atmospheric_pressure', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_atmospheric_pressure-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'atmospheric_pressure', + 'friendly_name': 'test-sensor-name-0 Atmospheric pressure', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_0_atmospheric_pressure', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_battery_voltage-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_0_battery_voltage', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Battery voltage', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'battery_voltage', + 'unique_id': 'test-sensor-device-id-0_battery_voltage', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_battery_voltage-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'voltage', + 'friendly_name': 'test-sensor-name-0 Battery voltage', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_0_battery_voltage', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_dew_point-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_0_dew_point', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Dew point', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'dewpoint', + 'unique_id': 'test-sensor-device-id-0_dewpoint', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_dew_point-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'test-sensor-name-0 Dew point', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_0_dew_point', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-17.8', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_humidity-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_0_humidity', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Humidity', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-0_humidity', + 'unit_of_measurement': '%', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_humidity-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'humidity', + 'friendly_name': 'test-sensor-name-0 Humidity', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_0_humidity', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_signal_strength-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_0_signal_strength', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Signal strength', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-0_signal_strength', + 'unit_of_measurement': 'dBm', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_signal_strength-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'test-sensor-name-0 Signal strength', + 'state_class': , + 'unit_of_measurement': 'dBm', + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_0_signal_strength', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_0_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-0_temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'test-sensor-name-0 Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_0_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-17.8', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_vapor_pressure-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_0_vapor_pressure', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Vapor pressure', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'vapor_pressure', + 'unique_id': 'test-sensor-device-id-0_vapor_pressure', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_0_vapor_pressure-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'pressure', + 'friendly_name': 'test-sensor-name-0 Vapor pressure', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_0_vapor_pressure', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_altitude-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_1_altitude', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Altitude', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'altitude', + 'unique_id': 'test-sensor-device-id-1_altitude', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_altitude-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'distance', + 'friendly_name': 'test-sensor-name-1 Altitude', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_1_altitude', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_atmospheric_pressure-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_1_atmospheric_pressure', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Atmospheric pressure', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-1_atmospheric_pressure', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_atmospheric_pressure-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'atmospheric_pressure', + 'friendly_name': 'test-sensor-name-1 Atmospheric pressure', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_1_atmospheric_pressure', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_battery_voltage-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_1_battery_voltage', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Battery voltage', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'battery_voltage', + 'unique_id': 'test-sensor-device-id-1_battery_voltage', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_battery_voltage-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'voltage', + 'friendly_name': 'test-sensor-name-1 Battery voltage', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_1_battery_voltage', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_dew_point-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_1_dew_point', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Dew point', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'dewpoint', + 'unique_id': 'test-sensor-device-id-1_dewpoint', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_dew_point-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'test-sensor-name-1 Dew point', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_1_dew_point', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-17.8', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_humidity-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_1_humidity', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Humidity', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-1_humidity', + 'unit_of_measurement': '%', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_humidity-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'humidity', + 'friendly_name': 'test-sensor-name-1 Humidity', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_1_humidity', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_signal_strength-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_1_signal_strength', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Signal strength', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-1_signal_strength', + 'unit_of_measurement': 'dBm', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_signal_strength-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'test-sensor-name-1 Signal strength', + 'state_class': , + 'unit_of_measurement': 'dBm', + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_1_signal_strength', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_1_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-1_temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'test-sensor-name-1 Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_1_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-17.8', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_vapor_pressure-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_1_vapor_pressure', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Vapor pressure', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'vapor_pressure', + 'unique_id': 'test-sensor-device-id-1_vapor_pressure', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_1_vapor_pressure-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'pressure', + 'friendly_name': 'test-sensor-name-1 Vapor pressure', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_1_vapor_pressure', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_altitude-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_2_altitude', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Altitude', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'altitude', + 'unique_id': 'test-sensor-device-id-2_altitude', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_altitude-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'distance', + 'friendly_name': 'test-sensor-name-2 Altitude', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_2_altitude', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_atmospheric_pressure-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_2_atmospheric_pressure', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Atmospheric pressure', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-2_atmospheric_pressure', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_atmospheric_pressure-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'atmospheric_pressure', + 'friendly_name': 'test-sensor-name-2 Atmospheric pressure', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_2_atmospheric_pressure', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_battery_voltage-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_2_battery_voltage', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Battery voltage', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'battery_voltage', + 'unique_id': 'test-sensor-device-id-2_battery_voltage', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_battery_voltage-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'voltage', + 'friendly_name': 'test-sensor-name-2 Battery voltage', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_2_battery_voltage', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_dew_point-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_2_dew_point', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Dew point', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'dewpoint', + 'unique_id': 'test-sensor-device-id-2_dewpoint', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_dew_point-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'test-sensor-name-2 Dew point', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_2_dew_point', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-17.8', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_humidity-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_2_humidity', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Humidity', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-2_humidity', + 'unit_of_measurement': '%', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_humidity-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'humidity', + 'friendly_name': 'test-sensor-name-2 Humidity', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_2_humidity', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_signal_strength-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_2_signal_strength', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Signal strength', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-2_signal_strength', + 'unit_of_measurement': 'dBm', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_signal_strength-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'test-sensor-name-2 Signal strength', + 'state_class': , + 'unit_of_measurement': 'dBm', + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_2_signal_strength', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_2_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'test-sensor-device-id-2_temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'test-sensor-name-2 Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_2_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-17.8', + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_vapor_pressure-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.test_sensor_name_2_vapor_pressure', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Vapor pressure', + 'platform': 'sensorpush_cloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'vapor_pressure', + 'unique_id': 'test-sensor-device-id-2_vapor_pressure', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.test_sensor_name_2_vapor_pressure-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'pressure', + 'friendly_name': 'test-sensor-name-2 Vapor pressure', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.test_sensor_name_2_vapor_pressure', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- diff --git a/tests/components/sensorpush_cloud/test_config_flow.py b/tests/components/sensorpush_cloud/test_config_flow.py new file mode 100644 index 00000000000000..699febbb1196ca --- /dev/null +++ b/tests/components/sensorpush_cloud/test_config_flow.py @@ -0,0 +1,95 @@ +"""Test the SensorPush Cloud config flow.""" + +from __future__ import annotations + +from unittest.mock import AsyncMock + +import pytest +from sensorpush_ha import SensorPushCloudError + +from homeassistant.components.sensorpush_cloud.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from .const import CONF_DATA, CONF_EMAIL + +from tests.common import MockConfigEntry + + +async def test_user( + hass: HomeAssistant, + mock_api: AsyncMock, + mock_helper: AsyncMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test user initialized flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + CONF_DATA, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "test@example.com" + assert result["data"] == CONF_DATA + assert result["result"].unique_id == CONF_DATA[CONF_EMAIL] + assert len(mock_setup_entry.mock_calls) == 1 + + +async def test_user_already_configured( + hass: HomeAssistant, + mock_api: AsyncMock, + mock_setup_entry: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test we fail on a duplicate entry in the user flow.""" + mock_config_entry.add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=CONF_DATA + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +@pytest.mark.parametrize( + ("error", "expected"), + [(SensorPushCloudError, "invalid_auth"), (Exception, "unknown")], +) +async def test_user_error( + hass: HomeAssistant, + mock_api: AsyncMock, + mock_setup_entry: AsyncMock, + error: Exception, + expected: str, +) -> None: + """Test we display errors in the user flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {} + + mock_api.async_authorize.side_effect = error + result = await hass.config_entries.flow.async_configure( + result["flow_id"], CONF_DATA + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {"base": expected} + + # Show we can recover from errors: + mock_api.async_authorize.side_effect = None + result = await hass.config_entries.flow.async_configure( + result["flow_id"], CONF_DATA + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "test@example.com" + assert result["data"] == CONF_DATA + assert len(mock_setup_entry.mock_calls) == 1 diff --git a/tests/components/sensorpush_cloud/test_sensor.py b/tests/components/sensorpush_cloud/test_sensor.py new file mode 100644 index 00000000000000..c35d40f1bc2c85 --- /dev/null +++ b/tests/components/sensorpush_cloud/test_sensor.py @@ -0,0 +1,29 @@ +"""Test SensorPush Cloud sensors.""" + +from __future__ import annotations + +from unittest.mock import AsyncMock + +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_registry import EntityRegistry + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_sensors( + hass: HomeAssistant, + entity_registry: EntityRegistry, + mock_config_entry: MockConfigEntry, + mock_helper: AsyncMock, + snapshot: SnapshotAssertion, +) -> None: + """Test we can read sensors.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)