-
-
Notifications
You must be signed in to change notification settings - Fork 37.3k
Add SmartThings Fan platform #20681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
balloob
merged 5 commits into
home-assistant:dev
from
andrewsayre:feature-smartthings-fan
Feb 2, 2019
Merged
Add SmartThings Fan platform #20681
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e3b353
Add SmartThings fan
andrewsayre 741ae8c
Removed unnecessary update method
andrewsayre da959b9
Corrected usage of async_schedule_update_ha_state
andrewsayre c8403a8
Clean-up/optimization
andrewsayre 00418b7
Merge branch 'dev' into feature-smartthings-fan
andrewsayre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """ | ||
| Support for fans through the SmartThings cloud API. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/smartthings.fan/ | ||
| """ | ||
|
|
||
| from homeassistant.components.fan import ( | ||
| SPEED_HIGH, SPEED_LOW, SPEED_MEDIUM, SPEED_OFF, SUPPORT_SET_SPEED, | ||
| FanEntity) | ||
|
|
||
| from . import SmartThingsEntity | ||
| from .const import DATA_BROKERS, DOMAIN | ||
|
|
||
| DEPENDENCIES = ['smartthings'] | ||
|
|
||
| VALUE_TO_SPEED = { | ||
| 0: SPEED_OFF, | ||
| 1: SPEED_LOW, | ||
| 2: SPEED_MEDIUM, | ||
| 3: SPEED_HIGH, | ||
| } | ||
| SPEED_TO_VALUE = { | ||
| v: k for k, v in VALUE_TO_SPEED.items()} | ||
|
|
||
|
|
||
| async def async_setup_platform( | ||
| hass, config, async_add_entities, discovery_info=None): | ||
| """Platform uses config entry setup.""" | ||
| pass | ||
|
|
||
|
|
||
| async def async_setup_entry(hass, config_entry, async_add_entities): | ||
| """Add fans for a config entry.""" | ||
| broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id] | ||
| async_add_entities( | ||
| [SmartThingsFan(device) for device in broker.devices.values() | ||
| if is_fan(device)]) | ||
|
|
||
|
|
||
| def is_fan(device): | ||
| """Determine if the device should be represented as a fan.""" | ||
| from pysmartthings import Capability | ||
| # Must have switch and fan_speed | ||
| return all(capability in device.capabilities | ||
| for capability in [Capability.switch, Capability.fan_speed]) | ||
|
|
||
|
|
||
| class SmartThingsFan(SmartThingsEntity, FanEntity): | ||
| """Define a SmartThings Fan.""" | ||
|
|
||
| async def async_set_speed(self, speed: str): | ||
| """Set the speed of the fan.""" | ||
| value = SPEED_TO_VALUE[speed] | ||
| await self._device.set_fan_speed(value, set_status=True) | ||
| # State is set optimistically in the command above, therefore update | ||
| # the entity state ahead of receiving the confirming push updates | ||
| self.async_schedule_update_ha_state() | ||
|
|
||
| async def async_turn_on(self, speed: str = None, **kwargs) -> None: | ||
| """Turn the fan on.""" | ||
| if speed is not None: | ||
| value = SPEED_TO_VALUE[speed] | ||
| await self._device.set_fan_speed(value, set_status=True) | ||
| else: | ||
| await self._device.switch_on(set_status=True) | ||
| # State is set optimistically in the commands above, therefore update | ||
| # the entity state ahead of receiving the confirming push updates | ||
| self.async_schedule_update_ha_state() | ||
|
|
||
| async def async_turn_off(self, **kwargs) -> None: | ||
| """Turn the fan off.""" | ||
| await self._device.switch_off(set_status=True) | ||
| # State is set optimistically in the command above, therefore update | ||
| # the entity state ahead of receiving the confirming push updates | ||
| self.async_schedule_update_ha_state() | ||
|
|
||
| @property | ||
| def is_on(self) -> bool: | ||
| """Return true if fan is on.""" | ||
| return self._device.status.switch | ||
|
|
||
| @property | ||
| def speed(self) -> str: | ||
| """Return the current speed.""" | ||
| return VALUE_TO_SPEED[self._device.status.fan_speed] | ||
|
|
||
| @property | ||
| def speed_list(self) -> list: | ||
| """Get the list of available speeds.""" | ||
| return [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH] | ||
|
|
||
| @property | ||
| def supported_features(self) -> int: | ||
| """Flag supported features.""" | ||
| return SUPPORT_SET_SPEED | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| """ | ||
| Test for the SmartThings fan platform. | ||
|
|
||
| The only mocking required is of the underlying SmartThings API object so | ||
| real HTTP calls are not initiated during testing. | ||
| """ | ||
| from pysmartthings import Attribute, Capability | ||
|
|
||
| from homeassistant.components.fan import ( | ||
| ATTR_SPEED, ATTR_SPEED_LIST, SPEED_HIGH, SPEED_LOW, SPEED_MEDIUM, | ||
| SPEED_OFF, SUPPORT_SET_SPEED) | ||
| from homeassistant.components.smartthings import DeviceBroker, fan | ||
| from homeassistant.components.smartthings.const import ( | ||
| DATA_BROKERS, DOMAIN, SIGNAL_SMARTTHINGS_UPDATE) | ||
| from homeassistant.config_entries import ( | ||
| CONN_CLASS_CLOUD_PUSH, SOURCE_USER, ConfigEntry) | ||
| from homeassistant.const import ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_send | ||
|
|
||
|
|
||
| async def _setup_platform(hass, *devices): | ||
| """Set up the SmartThings fan platform and prerequisites.""" | ||
| hass.config.components.add(DOMAIN) | ||
| broker = DeviceBroker(hass, devices, '') | ||
| config_entry = ConfigEntry("1", DOMAIN, "Test", {}, | ||
| SOURCE_USER, CONN_CLASS_CLOUD_PUSH) | ||
| hass.data[DOMAIN] = { | ||
| DATA_BROKERS: { | ||
| config_entry.entry_id: broker | ||
| } | ||
| } | ||
| await hass.config_entries.async_forward_entry_setup(config_entry, 'fan') | ||
| await hass.async_block_till_done() | ||
| return config_entry | ||
|
|
||
|
|
||
| async def test_async_setup_platform(): | ||
| """Test setup platform does nothing (it uses config entries).""" | ||
| await fan.async_setup_platform(None, None, None) | ||
|
|
||
|
|
||
| def test_is_fan(device_factory): | ||
| """Test fans are correctly identified.""" | ||
| non_fans = [ | ||
| device_factory('Unknown', ['Unknown']), | ||
| device_factory("Switch 1", [Capability.switch]), | ||
| device_factory("Non-Switchable Fan", [Capability.fan_speed]), | ||
| device_factory("Color Light", | ||
| [Capability.switch, Capability.switch_level, | ||
| Capability.color_control, | ||
| Capability.color_temperature]) | ||
| ] | ||
| fan_device = device_factory( | ||
| "Fan 1", [Capability.switch, Capability.switch_level, | ||
| Capability.fan_speed]) | ||
|
|
||
| assert fan.is_fan(fan_device), fan_device.name | ||
| for device in non_fans: | ||
| assert not fan.is_fan(device), device.name | ||
|
|
||
|
|
||
| async def test_entity_state(hass, device_factory): | ||
| """Tests the state attributes properly match the fan types.""" | ||
| device = device_factory( | ||
| "Fan 1", | ||
| capabilities=[Capability.switch, Capability.fan_speed], | ||
| status={Attribute.switch: 'on', Attribute.fan_speed: 2}) | ||
| await _setup_platform(hass, device) | ||
|
|
||
| # Dimmer 1 | ||
| state = hass.states.get('fan.fan_1') | ||
| assert state.state == 'on' | ||
| assert state.attributes[ATTR_SUPPORTED_FEATURES] == SUPPORT_SET_SPEED | ||
| assert state.attributes[ATTR_SPEED] == SPEED_MEDIUM | ||
| assert state.attributes[ATTR_SPEED_LIST] == \ | ||
| [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH] | ||
|
|
||
|
|
||
| async def test_entity_and_device_attributes(hass, device_factory): | ||
| """Test the attributes of the entity are correct.""" | ||
| # Arrange | ||
| device = device_factory( | ||
| "Fan 1", | ||
| capabilities=[Capability.switch, Capability.fan_speed], | ||
| status={Attribute.switch: 'on', Attribute.fan_speed: 2}) | ||
| await _setup_platform(hass, device) | ||
| entity_registry = await hass.helpers.entity_registry.async_get_registry() | ||
| device_registry = await hass.helpers.device_registry.async_get_registry() | ||
| # Act | ||
| await _setup_platform(hass, device) | ||
| # Assert | ||
| entry = entity_registry.async_get("fan.fan_1") | ||
| assert entry | ||
| assert entry.unique_id == device.device_id | ||
|
|
||
| entry = device_registry.async_get_device( | ||
| {(DOMAIN, device.device_id)}, []) | ||
| assert entry | ||
| assert entry.name == device.label | ||
| assert entry.model == device.device_type_name | ||
| assert entry.manufacturer == 'Unavailable' | ||
|
|
||
|
|
||
| async def test_turn_off(hass, device_factory): | ||
| """Test the fan turns of successfully.""" | ||
| # Arrange | ||
| device = device_factory( | ||
| "Fan 1", | ||
| capabilities=[Capability.switch, Capability.fan_speed], | ||
| status={Attribute.switch: 'on', Attribute.fan_speed: 2}) | ||
| await _setup_platform(hass, device) | ||
| # Act | ||
| await hass.services.async_call( | ||
| 'fan', 'turn_off', {'entity_id': 'fan.fan_1'}, | ||
| blocking=True) | ||
| # Assert | ||
| state = hass.states.get('fan.fan_1') | ||
| assert state is not None | ||
| assert state.state == 'off' | ||
|
|
||
|
|
||
| async def test_turn_on(hass, device_factory): | ||
| """Test the fan turns of successfully.""" | ||
| # Arrange | ||
| device = device_factory( | ||
| "Fan 1", | ||
| capabilities=[Capability.switch, Capability.fan_speed], | ||
| status={Attribute.switch: 'off', Attribute.fan_speed: 0}) | ||
| await _setup_platform(hass, device) | ||
| # Act | ||
| await hass.services.async_call( | ||
| 'fan', 'turn_on', {ATTR_ENTITY_ID: "fan.fan_1"}, | ||
| blocking=True) | ||
| # Assert | ||
| state = hass.states.get("fan.fan_1") | ||
| assert state is not None | ||
| assert state.state == 'on' | ||
|
|
||
|
|
||
| async def test_turn_on_with_speed(hass, device_factory): | ||
| """Test the fan turns on to the specified speed.""" | ||
| # Arrange | ||
| device = device_factory( | ||
| "Fan 1", | ||
| capabilities=[Capability.switch, Capability.fan_speed], | ||
| status={Attribute.switch: 'off', Attribute.fan_speed: 0}) | ||
| await _setup_platform(hass, device) | ||
| # Act | ||
| await hass.services.async_call( | ||
| 'fan', 'turn_on', | ||
| {ATTR_ENTITY_ID: "fan.fan_1", | ||
| ATTR_SPEED: SPEED_HIGH}, | ||
| blocking=True) | ||
| # Assert | ||
| state = hass.states.get("fan.fan_1") | ||
| assert state is not None | ||
| assert state.state == 'on' | ||
| assert state.attributes[ATTR_SPEED] == SPEED_HIGH | ||
|
|
||
|
|
||
| async def test_set_speed(hass, device_factory): | ||
| """Test setting to specific fan speed.""" | ||
| # Arrange | ||
| device = device_factory( | ||
| "Fan 1", | ||
| capabilities=[Capability.switch, Capability.fan_speed], | ||
| status={Attribute.switch: 'off', Attribute.fan_speed: 0}) | ||
| await _setup_platform(hass, device) | ||
| # Act | ||
| await hass.services.async_call( | ||
| 'fan', 'set_speed', | ||
| {ATTR_ENTITY_ID: "fan.fan_1", | ||
| ATTR_SPEED: SPEED_HIGH}, | ||
| blocking=True) | ||
| # Assert | ||
| state = hass.states.get("fan.fan_1") | ||
| assert state is not None | ||
| assert state.state == 'on' | ||
| assert state.attributes[ATTR_SPEED] == SPEED_HIGH | ||
|
|
||
|
|
||
| async def test_update_from_signal(hass, device_factory): | ||
| """Test the fan updates when receiving a signal.""" | ||
| # Arrange | ||
| device = device_factory( | ||
| "Fan 1", | ||
| capabilities=[Capability.switch, Capability.fan_speed], | ||
| status={Attribute.switch: 'off', Attribute.fan_speed: 0}) | ||
| await _setup_platform(hass, device) | ||
| await device.switch_on(True) | ||
| # Act | ||
| async_dispatcher_send(hass, SIGNAL_SMARTTHINGS_UPDATE, | ||
| [device.device_id]) | ||
| # Assert | ||
| await hass.async_block_till_done() | ||
| state = hass.states.get('fan.fan_1') | ||
| assert state is not None | ||
| assert state.state == 'on' | ||
|
|
||
|
|
||
| async def test_unload_config_entry(hass, device_factory): | ||
| """Test the fan is removed when the config entry is unloaded.""" | ||
| # Arrange | ||
| device = device_factory( | ||
| "Fan 1", | ||
| capabilities=[Capability.switch, Capability.fan_speed], | ||
| status={Attribute.switch: 'off', Attribute.fan_speed: 0}) | ||
| config_entry = await _setup_platform(hass, device) | ||
| # Act | ||
| await hass.config_entries.async_forward_entry_unload( | ||
| config_entry, 'fan') | ||
| # Assert | ||
| assert not hass.states.get('fan.fan_1') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.