Honeywell outdoor sensor (#65347)

This commit is contained in:
RDFurman 2022-04-04 12:26:33 -06:00 committed by GitHub
parent e830032b33
commit 95a97d99dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 1 deletions

View File

@ -21,7 +21,7 @@ from .const import (
UPDATE_LOOP_SLEEP_TIME = 5
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=300)
PLATFORMS = [Platform.CLIMATE]
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR]
MIGRATE_OPTIONS_KEYS = {CONF_COOL_AWAY_TEMPERATURE, CONF_HEAT_AWAY_TEMPERATURE}

View File

@ -9,5 +9,7 @@ DEFAULT_COOL_AWAY_TEMPERATURE = 88
DEFAULT_HEAT_AWAY_TEMPERATURE = 61
CONF_DEV_ID = "thermostat"
CONF_LOC_ID = "location"
TEMPERATURE_STATUS_KEY = "outdoor_temperature"
HUMIDITY_STATUS_KEY = "outdoor_humidity"
_LOGGER = logging.getLogger(__name__)

View File

@ -0,0 +1,96 @@
"""Support for Honeywell (US) Total Connect Comfort sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from somecomfort import Device
from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import (
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE,
PERCENTAGE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
)
from homeassistant.helpers.typing import StateType
from .const import DOMAIN, HUMIDITY_STATUS_KEY, TEMPERATURE_STATUS_KEY
def _get_temperature_sensor_unit(device: Device) -> str:
"""Get the correct temperature unit for the device."""
return TEMP_CELSIUS if device.temperature_unit == "C" else TEMP_FAHRENHEIT
@dataclass
class HoneywellSensorEntityDescriptionMixin:
"""Mixin for required keys."""
value_fn: Callable[[Device], Any]
unit_fn: Callable[[Device], Any]
@dataclass
class HoneywellSensorEntityDescription(
SensorEntityDescription, HoneywellSensorEntityDescriptionMixin
):
"""Describes a Honeywell sensor entity."""
SENSOR_TYPES: tuple[HoneywellSensorEntityDescription, ...] = (
HoneywellSensorEntityDescription(
key=TEMPERATURE_STATUS_KEY,
name="Temperature",
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda device: device.outdoor_temperature,
unit_fn=_get_temperature_sensor_unit,
),
HoneywellSensorEntityDescription(
key=HUMIDITY_STATUS_KEY,
name="Humidity",
device_class=DEVICE_CLASS_HUMIDITY,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda device: device.outdoor_humidity,
unit_fn=lambda device: PERCENTAGE,
),
)
async def async_setup_entry(hass, config, async_add_entities, discovery_info=None):
"""Set up the Honeywell thermostat."""
data = hass.data[DOMAIN][config.entry_id]
sensors = []
for device in data.devices.values():
for description in SENSOR_TYPES:
if getattr(device, description.key) is not None:
sensors.append(HoneywellSensor(device, description))
async_add_entities(sensors)
class HoneywellSensor(SensorEntity):
"""Representation of a Honeywell US Outdoor Temperature Sensor."""
entity_description: HoneywellSensorEntityDescription
def __init__(self, device, description):
"""Initialize the outdoor temperature sensor."""
self._device = device
self.entity_description = description
self._attr_unique_id = f"{device.deviceid}_outdoor_{description.device_class}"
self._attr_name = f"{device.name} outdoor {description.device_class}"
self._attr_native_unit_of_measurement = description.unit_fn(device)
@property
def native_value(self) -> StateType:
"""Return the state."""
return self.entity_description.value_fn(self._device)

View File

@ -40,6 +40,27 @@ def device():
mock_device.name = "device1"
mock_device.current_temperature = 20
mock_device.mac_address = "macaddress1"
mock_device.outdoor_temperature = None
mock_device.outdoor_humidity = None
return mock_device
@pytest.fixture
def device_with_outdoor_sensor():
"""Mock a somecomfort.Device."""
mock_device = create_autospec(somecomfort.Device, instance=True)
mock_device.deviceid = 1234567
mock_device._data = {
"canControlHumidification": False,
"hasFan": False,
}
mock_device.system_mode = "off"
mock_device.name = "device1"
mock_device.current_temperature = 20
mock_device.mac_address = "macaddress1"
mock_device.temperature_unit = "C"
mock_device.outdoor_temperature = 5
mock_device.outdoor_humidity = 25
return mock_device
@ -56,6 +77,8 @@ def another_device():
mock_device.name = "device2"
mock_device.current_temperature = 20
mock_device.mac_address = "macaddress1"
mock_device.outdoor_temperature = None
mock_device.outdoor_humidity = None
return mock_device

View File

@ -0,0 +1,29 @@
"""Test honeywell sensor."""
from somecomfort import Device, Location
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def test_outdoor_sensor(
hass: HomeAssistant,
config_entry: MockConfigEntry,
location: Location,
device_with_outdoor_sensor: Device,
):
"""Test outdoor temperature sensor."""
location.devices_by_id[
device_with_outdoor_sensor.deviceid
] = device_with_outdoor_sensor
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
temperature_state = hass.states.get("sensor.device1_outdoor_temperature")
humidity_state = hass.states.get("sensor.device1_outdoor_humidity")
assert temperature_state
assert humidity_state
assert temperature_state.state == "5"
assert humidity_state.state == "25"