diff --git a/homeassistant/components/nexia/strings.json b/homeassistant/components/nexia/strings.json index f6b08d5e8e5..6dbfe552e35 100644 --- a/homeassistant/components/nexia/strings.json +++ b/homeassistant/components/nexia/strings.json @@ -65,6 +65,9 @@ "hold": { "name": "Hold" }, + "room_iq_sensor": { + "name": "Include {sensor_name}" + }, "emergency_heat": { "name": "Emergency heat" } diff --git a/homeassistant/components/nexia/switch.py b/homeassistant/components/nexia/switch.py index 1897ad67414..bf1495217a7 100644 --- a/homeassistant/components/nexia/switch.py +++ b/homeassistant/components/nexia/switch.py @@ -2,14 +2,19 @@ from __future__ import annotations +from collections.abc import Iterable +import functools as ft from typing import Any from nexia.const import OPERATION_MODE_OFF +from nexia.roomiq import NexiaRoomIQHarmonizer +from nexia.sensor import NexiaSensor from nexia.thermostat import NexiaThermostat from nexia.zone import NexiaThermostatZone from homeassistant.components.switch import SwitchEntity -from homeassistant.core import HomeAssistant +from homeassistant.const import EVENT_HOMEASSISTANT_STOP +from homeassistant.core import Event, HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from .coordinator import NexiaDataUpdateCoordinator @@ -17,6 +22,14 @@ from .entity import NexiaThermostatEntity, NexiaThermostatZoneEntity from .types import NexiaConfigEntry +async def _stop_harmonizers( + _: Event, harmonizers: Iterable[NexiaRoomIQHarmonizer] +) -> None: + """Run the shutdown methods when preparing to stop.""" + for harmonizer in harmonizers: + await harmonizer.async_shutdown() # Never suspends + + async def async_setup_entry( hass: HomeAssistant, config_entry: NexiaConfigEntry, @@ -25,7 +38,8 @@ async def async_setup_entry( """Set up switches for a Nexia device.""" coordinator = config_entry.runtime_data nexia_home = coordinator.nexia_home - entities: list[NexiaHoldSwitch | NexiaEmergencyHeatSwitch] = [] + entities: list[SwitchEntity] = [] + room_iq_zones: dict[int, NexiaRoomIQHarmonizer] = {} for thermostat_id in nexia_home.get_thermostat_ids(): thermostat: NexiaThermostat = nexia_home.get_thermostat_by_id(thermostat_id) if thermostat.has_emergency_heat(): @@ -33,8 +47,18 @@ async def async_setup_entry( for zone_id in thermostat.get_zone_ids(): zone: NexiaThermostatZone = thermostat.get_zone_by_id(zone_id) entities.append(NexiaHoldSwitch(coordinator, zone)) + if len(zone_sensors := zone.get_sensors()) > 1: + entities.extend( + NexiaRoomIQSwitch(coordinator, zone, sensor, room_iq_zones) + for sensor in zone_sensors + ) async_add_entities(entities) + if room_iq_zones: + listener = ft.partial(_stop_harmonizers, harmonizers=room_iq_zones.values()) + config_entry.async_on_unload( + hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, listener) + ) class NexiaHoldSwitch(NexiaThermostatZoneEntity, SwitchEntity): @@ -68,6 +92,49 @@ class NexiaHoldSwitch(NexiaThermostatZoneEntity, SwitchEntity): self._signal_zone_update() +class NexiaRoomIQSwitch(NexiaThermostatZoneEntity, SwitchEntity): + """Provides Nexia RoomIQ sensor switch support.""" + + _attr_translation_key = "room_iq_sensor" + + def __init__( + self, + coordinator: NexiaDataUpdateCoordinator, + zone: NexiaThermostatZone, + sensor: NexiaSensor, + room_iq_zones: dict[int, NexiaRoomIQHarmonizer], + ) -> None: + """Initialize the RoomIQ sensor switch.""" + super().__init__(coordinator, zone, f"{sensor.id}_room_iq_sensor") + self._attr_translation_placeholders = {"sensor_name": sensor.name} + self._sensor_id = sensor.id + if zone.zone_id in room_iq_zones: + self._harmonizer = room_iq_zones[zone.zone_id] + else: + self._harmonizer = NexiaRoomIQHarmonizer( + zone, coordinator.async_refresh, self._signal_zone_update + ) + room_iq_zones[zone.zone_id] = self._harmonizer + + @property + def is_on(self) -> bool: + """Return if the sensor is part of the zone average temperature.""" + if self._harmonizer.request_pending(): + return self._sensor_id in self._harmonizer.selected_sensor_ids + + return self._zone.get_sensor_by_id(self._sensor_id).weight > 0.0 + + async def async_turn_on(self, **kwargs: Any) -> None: + """Include this sensor.""" + self._harmonizer.trigger_add_sensor(self._sensor_id) + self._signal_zone_update() + + async def async_turn_off(self, **kwargs: Any) -> None: + """Remove this sensor.""" + self._harmonizer.trigger_remove_sensor(self._sensor_id) + self._signal_zone_update() + + class NexiaEmergencyHeatSwitch(NexiaThermostatEntity, SwitchEntity): """Provides Nexia emergency heat switch support.""" diff --git a/tests/components/nexia/fixtures/sensors_xl1050_house.json b/tests/components/nexia/fixtures/sensors_xl1050_house.json new file mode 100644 index 00000000000..4293b92c6cf --- /dev/null +++ b/tests/components/nexia/fixtures/sensors_xl1050_house.json @@ -0,0 +1,1096 @@ +{ + "success": true, + "error": null, + "result": { + "id": 123456, + "name": "My Home", + "third_party_integrations": [], + "latitude": null, + "longitude": null, + "time_zone": "America/New_York", + "dealer_opt_in": true, + "room_iq_enabled": true, + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/houses/123456" + }, + "edit": [ + { + "href": "https://www.mynexia.com/mobile/houses/123456/edit", + "method": "GET" + } + ], + "child": [ + { + "href": "https://www.mynexia.com/mobile/houses/123456/devices", + "type": "application/vnd.nexia.collection+json", + "data": { + "items": [ + { + "id": 5378307, + "name": "Center", + "name_editable": true, + "features": [ + { + "name": "advanced_info", + "items": [ + { + "type": "label_value", + "label": "Model", + "value": "XL1050" + }, + { + "type": "label_value", + "label": "AUID", + "value": "0295CB84" + }, + { + "type": "label_value", + "label": "Firmware Build Number", + "value": "1726826973" + }, + { + "type": "label_value", + "label": "Firmware Build Date", + "value": "2024-09-20 10:09:33 UTC" + }, + { + "type": "label_value", + "label": "Firmware Version", + "value": "5.11.1" + } + ] + }, + { + "name": "thermostat", + "scale": "f", + "temperature": 69, + "device_identifier": "XxlZone-85034552", + "status": "System Idle", + "status_icon": null, + "actions": { + "set_heat_setpoint": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/setpoints" + } + }, + "setpoint_delta": 3, + "setpoint_increment": 1.0, + "setpoint_heat_min": 55, + "setpoint_heat_max": 90, + "setpoint_cool_min": 60, + "setpoint_cool_max": 99, + "setpoint_heat": 69, + "system_status": "System Idle", + "operating_state": "idle" + }, + { + "name": "connection", + "signal_strength": "unknown", + "is_connected": true + }, + { + "name": "dealer_contact_info", + "has_dealer_identifier": true, + "actions": { + "request_current_dealer_info": { + "method": "GET", + "href": "https://www.mynexia.com/mobile/dealers/7043919191" + }, + "request_dealers_by_zip": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/dealers/5378307/search" + } + } + }, + { + "name": "thermostat_mode", + "label": "System Mode", + "value": "HEAT", + "display_value": "Heating", + "options": [ + { + "id": "thermostat_mode", + "label": "System Mode", + "value": "thermostat_mode", + "header": true + }, + { + "value": "AUTO", + "label": "Auto" + }, + { + "value": "COOL", + "label": "Cooling" + }, + { + "value": "HEAT", + "label": "Heating" + }, + { + "value": "OFF", + "label": "Off" + } + ], + "actions": { + "update_thermostat_mode": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/zone_mode" + } + } + }, + { + "name": "thermostat_run_mode", + "label": "Run Mode", + "options": [ + { + "id": "thermostat_run_mode", + "label": "Run Mode", + "value": "thermostat_run_mode", + "header": true + }, + { + "id": "info_text", + "label": "Follow or override the schedule.", + "value": "info_text", + "info": true + }, + { + "value": "permanent_hold", + "label": "Permanent Hold" + }, + { + "value": "run_schedule", + "label": "Run Schedule" + } + ], + "value": "run_schedule", + "display_value": "Run Schedule", + "actions": { + "update_thermostat_run_mode": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/run_mode" + } + } + }, + { + "name": "room_iq_sensors", + "sensors": [ + { + "id": 17687546, + "name": "Center", + "icon": { + "name": "room_iq_onboard", + "modifiers": [] + }, + "type": "thermostat", + "serial_number": "NativeIDTUniqueID", + "weight": 0.5, + "temperature": 68, + "temperature_valid": true, + "humidity": 32, + "humidity_valid": true, + "has_online": false, + "has_battery": false + }, + { + "id": 17687549, + "name": "Upstairs", + "icon": { + "name": "room_iq_wireless", + "modifiers": [] + }, + "type": "930", + "serial_number": "2410R5C53X", + "weight": 0.5, + "temperature": 69, + "temperature_valid": true, + "humidity": 32, + "humidity_valid": true, + "has_online": true, + "connected": true, + "has_battery": true, + "battery_level": 95, + "battery_low": false, + "battery_valid": true + } + ], + "should_show": true, + "actions": { + "request_current_state": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/request_current_sensor_state" + }, + "update_active_sensors": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/update_active_sensors" + } + } + }, + { + "name": "preset_setpoints", + "presets": { + "1": { + "cool": 78, + "heat": 70 + }, + "2": { + "cool": 85, + "heat": 62 + }, + "3": { + "cool": 82, + "heat": 62 + } + } + }, + { + "name": "thermostat_fan_mode", + "label": "Fan Mode", + "options": [ + { + "id": "thermostat_fan_mode", + "label": "Fan Mode", + "value": "thermostat_fan_mode", + "header": true + }, + { + "value": "auto", + "label": "Auto" + }, + { + "value": "on", + "label": "On" + }, + { + "value": "circulate", + "label": "Circulate" + } + ], + "value": "circulate", + "display_value": "Circulate", + "status_icon": { + "name": "thermostat_fan_off", + "modifiers": [] + }, + "actions": { + "update_thermostat_fan_mode": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307/fan_mode" + } + } + }, + { + "name": "thermostat_default_fan_mode", + "value": "circulate", + "actions": { + "update_thermostat_default_fan_mode": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307/fan_mode" + } + } + }, + { + "name": "gen_2_app", + "is_supported": false, + "validation_failures": [ + "Thermostat has wireless sensors.", + "Unauthorized to use Gen 2 App." + ] + }, + { + "name": "schedule", + "enabled": true, + "max_period_name_length": 10, + "setpoint_increment": 1.0, + "collection_url": "https://www.mynexia.com/mobile/schedules?device_identifier=TraneXl1050-5378307\u0026house_id=123456", + "actions": { + "get_active_schedule": { + "href": "https://www.mynexia.com/mobile/thermostat_schedules/get_active_schedule?device_identifier=TraneXl1050-5378307", + "method": "POST" + }, + "set_active_schedule": { + "href": "https://www.mynexia.com/mobile/thermostat_schedules/set_active_schedule?device_identifier=TraneXl1050-5378307", + "method": "POST" + }, + "get_default_schedule": { + "href": "https://www.mynexia.com/mobile/thermostat_schedules/get_default_schedule?device_identifier=TraneXl1050-5378307", + "method": "GET" + }, + "enable_scheduling": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/scheduling_enabled", + "method": "POST", + "data": { + "value": true + } + } + }, + "can_add_remove_periods": true, + "max_periods_per_day": 4 + }, + { + "name": "runtime_history", + "actions": { + "get_runtime_history": { + "method": "GET", + "href": "https://www.mynexia.com/mobile/runtime_history/TraneXl1050-5378307?report_type=daily" + }, + "get_monthly_runtime_history": { + "method": "GET", + "href": "https://www.mynexia.com/mobile/runtime_history/TraneXl1050-5378307?report_type=monthly" + } + } + } + ], + "icon": [ + { + "name": "thermostat", + "modifiers": ["temperature-69"] + } + ], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307" + }, + "nexia:history": { + "href": "https://www.mynexia.com/mobile/houses/123456/events?device_id=5378307" + }, + "filter_events": { + "href": "https://www.mynexia.com/mobile/houses/123456/events/collection?sys_guid=e16684f6-b1e3-4e25-b006-e4d599dab2e9" + } + }, + "last_updated_at": "2025-01-06T17:45:09.000-05:00", + "settings": [ + { + "type": "preset_selected", + "title": "Preset", + "current_value": 0, + "options": [ + { + "value": 0, + "label": "None" + }, + { + "value": 1, + "label": "Home" + }, + { + "value": 2, + "label": "Away" + }, + { + "value": 3, + "label": "Sleep" + } + ], + "labels": ["None", "Home", "Away", "Sleep"], + "values": [0, 1, 2, 3], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/preset_selected" + } + } + }, + { + "type": "system_mode", + "title": "System Mode", + "current_value": "HEAT", + "options": [ + { + "value": "AUTO", + "label": "Auto" + }, + { + "value": "COOL", + "label": "Cooling" + }, + { + "value": "HEAT", + "label": "Heating" + }, + { + "value": "OFF", + "label": "Off" + } + ], + "labels": ["Auto", "Cooling", "Heating", "Off"], + "values": ["AUTO", "COOL", "HEAT", "OFF"], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/zone_mode" + } + } + }, + { + "type": "run_mode", + "title": "Run Mode", + "current_value": "run_schedule", + "options": [ + { + "value": "permanent_hold", + "label": "Permanent Hold" + }, + { + "value": "run_schedule", + "label": "Run Schedule" + } + ], + "labels": ["Permanent Hold", "Run Schedule"], + "values": ["permanent_hold", "run_schedule"], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/run_mode" + } + } + }, + { + "type": "scheduling_enabled", + "title": "Scheduling", + "current_value": true, + "options": [ + { + "value": true, + "label": "ON" + }, + { + "value": false, + "label": "OFF" + } + ], + "labels": ["ON", "OFF"], + "values": [true, false], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/scheduling_enabled" + } + } + }, + { + "type": "fan_mode", + "title": "Fan Mode", + "current_value": "circulate", + "options": [ + { + "value": "auto", + "label": "Auto" + }, + { + "value": "on", + "label": "On" + }, + { + "value": "circulate", + "label": "Circulate" + } + ], + "labels": ["Auto", "On", "Circulate"], + "values": ["auto", "on", "circulate"], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307/fan_mode" + } + } + }, + { + "type": "fan_circulation_time", + "title": "Fan Circulation Time", + "current_value": 10, + "options": [ + { + "value": 10, + "label": "10 minutes" + }, + { + "value": 15, + "label": "15 minutes" + }, + { + "value": 20, + "label": "20 minutes" + }, + { + "value": 25, + "label": "25 minutes" + }, + { + "value": 30, + "label": "30 minutes" + }, + { + "value": 35, + "label": "35 minutes" + }, + { + "value": 40, + "label": "40 minutes" + }, + { + "value": 45, + "label": "45 minutes" + }, + { + "value": 50, + "label": "50 minutes" + }, + { + "value": 55, + "label": "55 minutes" + } + ], + "labels": [ + "10 minutes", + "15 minutes", + "20 minutes", + "25 minutes", + "30 minutes", + "35 minutes", + "40 minutes", + "45 minutes", + "50 minutes", + "55 minutes" + ], + "values": [10, 15, 20, 25, 30, 35, 40, 45, 50, 55], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307/fan_circulation_time" + } + } + }, + { + "type": "air_cleaner_mode", + "title": "Air Cleaner Mode", + "current_value": "auto", + "options": [ + { + "value": "auto", + "label": "Auto" + }, + { + "value": "quick", + "label": "Quick" + }, + { + "value": "allergy", + "label": "Allergy" + } + ], + "labels": ["Auto", "Quick", "Allergy"], + "values": ["auto", "quick", "allergy"], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307/air_cleaner_mode" + } + } + }, + { + "type": "dehumidify", + "title": "Cooling Dehumidify Set Point", + "current_value": 0.5, + "options": [ + { + "value": 0.35, + "label": "35%" + }, + { + "value": 0.4, + "label": "40%" + }, + { + "value": 0.45, + "label": "45%" + }, + { + "value": 0.5, + "label": "50%" + }, + { + "value": 0.55, + "label": "55%" + }, + { + "value": 0.6, + "label": "60%" + }, + { + "value": 0.65, + "label": "65%" + } + ], + "labels": ["35%", "40%", "45%", "50%", "55%", "60%", "65%"], + "values": [0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307/dehumidify" + } + } + }, + { + "type": "emergency_heat", + "title": "Emergency Heat", + "current_value": false, + "options": [ + { + "value": true, + "label": "ON" + }, + { + "value": false, + "label": "OFF" + } + ], + "labels": ["ON", "OFF"], + "values": [true, false], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307/emergency_heat" + } + } + }, + { + "type": "scale", + "title": "Temperature Scale", + "current_value": "f", + "options": [ + { + "value": "f", + "label": "F" + }, + { + "value": "c", + "label": "C" + } + ], + "labels": ["F", "C"], + "values": ["f", "c"], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_thermostats/5378307/scale" + } + } + } + ], + "status_secondary": null, + "status_tertiary": null, + "type": "xxl_thermostat", + "has_outdoor_temperature": true, + "outdoor_temperature": "39", + "has_indoor_humidity": true, + "connected": true, + "indoor_humidity": "33", + "system_status": "System Idle", + "delta": 3, + "manufacturer": "AmericanStandard", + "country_code": "US", + "state_code": "NC", + "zones": [ + { + "type": "xxl_zone", + "id": 85034552, + "name": "NativeZone", + "current_zone_mode": "HEAT", + "temperature": 69, + "setpoints": { + "heat": 69, + "cool": null + }, + "operating_state": "", + "heating_setpoint": 69, + "cooling_setpoint": null, + "zone_status": "", + "settings": [], + "icon": { + "name": "thermostat", + "modifiers": ["temperature-69"] + }, + "features": [ + { + "name": "thermostat", + "scale": "f", + "temperature": 69, + "device_identifier": "XxlZone-85034552", + "status": "", + "status_icon": null, + "actions": { + "set_heat_setpoint": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/setpoints" + } + }, + "setpoint_delta": 3, + "setpoint_increment": 1.0, + "setpoint_heat_min": 55, + "setpoint_heat_max": 90, + "setpoint_cool_min": 60, + "setpoint_cool_max": 99, + "setpoint_heat": 69, + "system_status": "System Idle" + }, + { + "name": "connection", + "signal_strength": "unknown", + "is_connected": true + }, + { + "name": "dealer_contact_info", + "has_dealer_identifier": true, + "actions": { + "request_current_dealer_info": { + "method": "GET", + "href": "https://www.mynexia.com/mobile/dealers/7043919191" + }, + "request_dealers_by_zip": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/dealers/5378307/search" + } + } + }, + { + "name": "thermostat_mode", + "label": "System Mode", + "value": "HEAT", + "display_value": "Heating", + "options": [ + { + "id": "thermostat_mode", + "label": "System Mode", + "value": "thermostat_mode", + "header": true + }, + { + "value": "AUTO", + "label": "Auto" + }, + { + "value": "COOL", + "label": "Cooling" + }, + { + "value": "HEAT", + "label": "Heating" + }, + { + "value": "OFF", + "label": "Off" + } + ], + "actions": { + "update_thermostat_mode": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/zone_mode" + } + } + }, + { + "name": "thermostat_run_mode", + "label": "Run Mode", + "options": [ + { + "id": "thermostat_run_mode", + "label": "Run Mode", + "value": "thermostat_run_mode", + "header": true + }, + { + "id": "info_text", + "label": "Follow or override the schedule.", + "value": "info_text", + "info": true + }, + { + "value": "permanent_hold", + "label": "Permanent Hold" + }, + { + "value": "run_schedule", + "label": "Run Schedule" + } + ], + "value": "run_schedule", + "display_value": "Run Schedule", + "actions": { + "update_thermostat_run_mode": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/run_mode" + } + } + }, + { + "name": "room_iq_sensors", + "sensors": [ + { + "id": 17687546, + "name": "Center", + "icon": { + "name": "room_iq_onboard", + "modifiers": [] + }, + "type": "thermostat", + "serial_number": "NativeIDTUniqueID", + "weight": 0.5, + "temperature": 68, + "temperature_valid": true, + "humidity": 32, + "humidity_valid": true, + "has_online": false, + "has_battery": false + }, + { + "id": 17687549, + "name": "Upstairs", + "icon": { + "name": "room_iq_wireless", + "modifiers": [] + }, + "type": "930", + "serial_number": "2410R5C53X", + "weight": 0.5, + "temperature": 69, + "temperature_valid": true, + "humidity": 32, + "humidity_valid": true, + "has_online": true, + "connected": true, + "has_battery": true, + "battery_level": 95, + "battery_low": false, + "battery_valid": true + } + ], + "should_show": true, + "actions": { + "request_current_state": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/request_current_sensor_state" + }, + "update_active_sensors": { + "method": "POST", + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/update_active_sensors" + } + } + }, + { + "name": "preset_setpoints", + "presets": { + "1": { + "cool": 78, + "heat": 70 + }, + "2": { + "cool": 85, + "heat": 62 + }, + "3": { + "cool": 82, + "heat": 62 + } + } + }, + { + "name": "schedule", + "enabled": true, + "max_period_name_length": 10, + "setpoint_increment": 1.0, + "collection_url": "https://www.mynexia.com/mobile/schedules?device_identifier=XxlZone-85034552\u0026house_id=123456", + "actions": { + "get_active_schedule": { + "href": "https://www.mynexia.com/mobile/thermostat_schedules/get_active_schedule?device_identifier=XxlZone-85034552", + "method": "POST" + }, + "set_active_schedule": { + "href": "https://www.mynexia.com/mobile/thermostat_schedules/set_active_schedule?device_identifier=XxlZone-85034552", + "method": "POST" + }, + "get_default_schedule": { + "href": "https://www.mynexia.com/mobile/thermostat_schedules/get_default_schedule?device_identifier=XxlZone-85034552", + "method": "GET" + }, + "enable_scheduling": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552/scheduling_enabled", + "method": "POST", + "data": { + "value": true + } + } + }, + "can_add_remove_periods": true, + "max_periods_per_day": 4 + } + ], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/xxl_zones/85034552" + } + } + } + ], + "generic_input_sensors": [] + } + ], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/houses/123456/devices" + }, + "template": { + "data": { + "title": null, + "fields": [], + "_links": { + "child-schema": [ + { + "data": { + "label": "Connect New Device", + "icon": { + "name": "new_device", + "modifiers": [] + }, + "_links": { + "next": { + "href": "https://www.mynexia.com/mobile/houses/123456/enrollables_schema" + } + } + } + }, + { + "data": { + "label": "Create Group", + "icon": { + "name": "create_group", + "modifiers": [] + }, + "_links": { + "next": { + "href": "https://www.mynexia.com/mobile/houses/123456/groups/new" + } + } + } + } + ] + } + } + } + }, + "item_type": "application/vnd.nexia.device+json" + } + }, + { + "href": "https://www.mynexia.com/mobile/houses/123456/automations", + "type": "application/vnd.nexia.collection+json", + "data": { + "items": [ + { + "id": 4995413, + "name": "My First Automation", + "enabled": false, + "settings": [], + "triggers": [], + "description": "Click the Edit button to set up automation for your devices.", + "icon": [], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/automations/4995413" + }, + "edit": { + "href": "https://www.mynexia.com/mobile/automation_edit_buffers?automation_id=4995413", + "method": "POST" + }, + "nexia:history": { + "href": "https://www.mynexia.com/mobile/houses/123456/events?automation_id=4995413" + }, + "filter_events": { + "href": "https://www.mynexia.com/mobile/houses/123456/events/collection?sys_guid=d827e212-3055-4835-8bda-333d26f05c9d" + } + } + } + ], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/houses/123456/automations" + }, + "template": { + "href": "https://www.mynexia.com/mobile/houses/123456/automation_edit_buffers", + "method": "POST" + } + }, + "item_type": "application/vnd.nexia.automation+json" + } + }, + { + "href": "https://www.mynexia.com/mobile/houses/123456/modes", + "type": "application/vnd.nexia.collection+json", + "data": { + "items": [ + { + "id": 6631129, + "name": "Day", + "current_mode": false, + "icon": "home.png", + "settings": [], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/modes/6631129" + } + } + }, + { + "id": 6631132, + "name": "Night", + "current_mode": true, + "icon": "home.png", + "settings": [], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/modes/6631132" + } + } + } + ], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/houses/123456/modes" + } + }, + "item_type": "application/vnd.nexia.mode+json" + } + }, + { + "href": "https://www.mynexia.com/mobile/houses/123456/events/collection", + "type": "application/vnd.nexia.collection+json", + "data": { + "item_type": "application/vnd.nexia.event+json" + } + }, + { + "href": "https://www.mynexia.com/mobile/houses/123456/videos/collection", + "type": "application/vnd.nexia.collection+json", + "data": { + "item_type": "application/vnd.nexia.video+json" + } + }, + { + "href": "https://www.mynexia.com/mobile/choices", + "type": "application/vnd.nexia.collection+json", + "data": { + "items": [], + "_links": { + "self": { + "href": "https://www.mynexia.com/mobile/choices" + } + }, + "item_type": "application/vnd.nexia.choice+json" + } + } + ], + "feature_code_url": [ + { + "href": "https://www.mynexia.com/mobile/houses/123456/feature_code", + "method": "POST" + } + ], + "remove_zwave_device": [ + { + "href": "https://www.mynexia.com/mobile/houses/123456/remove_zwave_device", + "cancel_href": "https://www.mynexia.com/mobile/houses/123456/cancel_remove_zwave_device", + "method": "POST", + "timeout": 240, + "display": true + } + ] + } + } +} diff --git a/tests/components/nexia/test_switch.py b/tests/components/nexia/test_switch.py index 821d939bac5..e532201f01e 100644 --- a/tests/components/nexia/test_switch.py +++ b/tests/components/nexia/test_switch.py @@ -1,12 +1,74 @@ """The switch tests for the nexia platform.""" -from homeassistant.const import STATE_ON +from freezegun.api import FrozenDateTimeFactory + +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.const import ( + ATTR_ENTITY_ID, + EVENT_HOMEASSISTANT_STOP, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_OFF, + STATE_ON, + Platform, +) from homeassistant.core import HomeAssistant from .util import async_init_integration +from tests.common import async_fire_time_changed + async def test_hold_switch(hass: HomeAssistant) -> None: """Test creation of the hold switch.""" await async_init_integration(hass) assert hass.states.get("switch.nick_office_hold").state == STATE_ON + + +async def test_nexia_sensor_switch( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: + """Test NexiaRoomIQSensorSwitch.""" + await async_init_integration(hass, house_fixture="nexia/sensors_xl1050_house.json") + sw1_id = f"{Platform.SWITCH}.center_nativezone_include_center" + sw1 = {ATTR_ENTITY_ID: sw1_id} + sw2_id = f"{Platform.SWITCH}.center_nativezone_include_upstairs" + sw2 = {ATTR_ENTITY_ID: sw2_id} + + # Switch starts out on. + assert (entity_state := hass.states.get(sw1_id)) is not None + assert entity_state.state == STATE_ON + + # Turn switch off. + await hass.services.async_call(SWITCH_DOMAIN, SERVICE_TURN_OFF, sw1, blocking=True) + assert hass.states.get(sw1_id).state == STATE_OFF + + # Turn switch back on. + await hass.services.async_call(SWITCH_DOMAIN, SERVICE_TURN_ON, sw1, blocking=True) + assert hass.states.get(sw1_id).state == STATE_ON + + # The other switch also starts out on. + assert (entity_state := hass.states.get(sw2_id)) is not None + assert entity_state.state == STATE_ON + + # Turn both switches off, an invalid combination. + await hass.services.async_call(SWITCH_DOMAIN, SERVICE_TURN_OFF, sw1, blocking=True) + await hass.services.async_call(SWITCH_DOMAIN, SERVICE_TURN_OFF, sw2, blocking=True) + assert hass.states.get(sw1_id).state == STATE_OFF + assert hass.states.get(sw2_id).state == STATE_OFF + + # Wait for switches to revert to device status. + freezer.tick(6) + async_fire_time_changed(hass) + await hass.async_block_till_done() + assert hass.states.get(sw1_id).state == STATE_ON + assert hass.states.get(sw2_id).state == STATE_ON + + # Turn switch off. + await hass.services.async_call(SWITCH_DOMAIN, SERVICE_TURN_OFF, sw2, blocking=True) + assert hass.states.get(sw2_id).state == STATE_OFF + + # Exercise shutdown path. + hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) + await hass.async_block_till_done() + assert hass.states.get(sw2_id).state == STATE_ON diff --git a/tests/components/nexia/util.py b/tests/components/nexia/util.py index 1104ffad63d..d9f0f59b719 100644 --- a/tests/components/nexia/util.py +++ b/tests/components/nexia/util.py @@ -17,10 +17,11 @@ async def async_init_integration( hass: HomeAssistant, skip_setup: bool = False, exception: Exception | None = None, + *, + house_fixture="nexia/mobile_houses_123456.json", ) -> MockConfigEntry: """Set up the nexia integration in Home Assistant.""" - house_fixture = "nexia/mobile_houses_123456.json" session_fixture = "nexia/session_123456.json" sign_in_fixture = "nexia/sign_in.json" set_fan_speed_fixture = "nexia/set_fan_speed_2293892.json"