-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
ZHA cover device support #30639
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
Merged
ZHA cover device support #30639
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d64fc37
ZHA cover device support
billyburly b5301a1
flake8
billyburly dc4f8ec
flake8, black
billyburly a655508
isort
billyburly 67428fd
pylint
billyburly 9b2ec59
more test
billyburly e3509fe
use zigpy provided functions
billyburly 5860e8b
black
billyburly e748042
handle command errors, better state handling
billyburly 0567474
black
billyburly 374148e
more test
billyburly 81cd67a
lint
billyburly 13fc42f
Update ZHA cover tests coverage.
Adminiuga 9663f9b
Merge pull request #1 from Adminiuga/tests/cover-tests
billyburly 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
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,165 @@ | ||
| """Support for ZHA covers.""" | ||
| from datetime import timedelta | ||
| import functools | ||
| import logging | ||
|
|
||
| from homeassistant.components.cover import ATTR_POSITION, DOMAIN, CoverDevice | ||
| from homeassistant.const import STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING | ||
| from homeassistant.core import callback | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
|
|
||
| from .core.const import ( | ||
| CHANNEL_COVER, | ||
| DATA_ZHA, | ||
| DATA_ZHA_DISPATCHERS, | ||
| SIGNAL_ATTR_UPDATED, | ||
| ZHA_DISCOVERY_NEW, | ||
| ) | ||
| from .core.registries import ZHA_ENTITIES | ||
| from .entity import ZhaEntity | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| SCAN_INTERVAL = timedelta(minutes=60) | ||
| STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, DOMAIN) | ||
|
|
||
|
|
||
| async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): | ||
| """Old way of setting up Zigbee Home Automation covers.""" | ||
| pass | ||
|
|
||
|
|
||
| async def async_setup_entry(hass, config_entry, async_add_entities): | ||
| """Set up the Zigbee Home Automation cover from config entry.""" | ||
|
|
||
| async def async_discover(discovery_info): | ||
| await _async_setup_entities( | ||
| hass, config_entry, async_add_entities, [discovery_info] | ||
| ) | ||
|
|
||
| unsub = async_dispatcher_connect( | ||
| hass, ZHA_DISCOVERY_NEW.format(DOMAIN), async_discover | ||
| ) | ||
| hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub) | ||
|
|
||
| covers = hass.data.get(DATA_ZHA, {}).get(DOMAIN) | ||
| if covers is not None: | ||
| await _async_setup_entities( | ||
| hass, config_entry, async_add_entities, covers.values() | ||
| ) | ||
| del hass.data[DATA_ZHA][DOMAIN] | ||
|
|
||
|
|
||
| async def _async_setup_entities( | ||
| hass, config_entry, async_add_entities, discovery_infos | ||
| ): | ||
| """Set up the ZHA covers.""" | ||
| entities = [] | ||
| for discovery_info in discovery_infos: | ||
| zha_dev = discovery_info["zha_device"] | ||
| channels = discovery_info["channels"] | ||
|
|
||
| entity = ZHA_ENTITIES.get_entity(DOMAIN, zha_dev, channels, ZhaCover) | ||
| if entity: | ||
| entities.append(entity(**discovery_info)) | ||
|
|
||
| if entities: | ||
| async_add_entities(entities, update_before_add=True) | ||
|
|
||
|
|
||
| @STRICT_MATCH(channel_names=CHANNEL_COVER) | ||
| class ZhaCover(ZhaEntity, CoverDevice): | ||
| """Representation of a ZHA cover.""" | ||
|
|
||
| def __init__(self, unique_id, zha_device, channels, **kwargs): | ||
| """Init this sensor.""" | ||
| super().__init__(unique_id, zha_device, channels, **kwargs) | ||
| self._cover_channel = self.cluster_channels.get(CHANNEL_COVER) | ||
| self._current_position = None | ||
|
|
||
| async def async_added_to_hass(self): | ||
| """Run when about to be added to hass.""" | ||
| await super().async_added_to_hass() | ||
| await self.async_accept_signal( | ||
| self._cover_channel, SIGNAL_ATTR_UPDATED, self.async_set_position | ||
| ) | ||
|
|
||
| @callback | ||
| def async_restore_last_state(self, last_state): | ||
| """Restore previous state.""" | ||
| self._state = last_state.state | ||
| if "current_position" in last_state.attributes: | ||
| self._current_position = last_state.attributes["current_position"] | ||
|
|
||
| @property | ||
| def is_closed(self): | ||
| """Return if the cover is closed.""" | ||
| if self.current_cover_position is None: | ||
| return None | ||
| return self.current_cover_position == 0 | ||
|
|
||
| @property | ||
| def current_cover_position(self): | ||
| """Return the current position of ZHA cover. | ||
|
|
||
| None is unknown, 0 is closed, 100 is fully open. | ||
| """ | ||
| return self._current_position | ||
|
|
||
| def async_set_position(self, pos): | ||
| """Handle position update from channel.""" | ||
| _LOGGER.debug("setting position: %s", pos) | ||
| self._current_position = 100 - pos | ||
| self.async_schedule_update_ha_state() | ||
|
|
||
| def async_set_state(self, state): | ||
| """Handle state update from channel.""" | ||
| _LOGGER.debug("state=%s", state) | ||
| self._state = state | ||
| self.async_schedule_update_ha_state() | ||
|
|
||
| async def async_open_cover(self, **kwargs): | ||
| """Open the window cover.""" | ||
| await self._cover_channel.async_open() | ||
| self.async_set_state(STATE_OPENING) | ||
|
|
||
| async def async_close_cover(self, **kwargs): | ||
| """Close the window cover.""" | ||
| await self._cover_channel.async_close() | ||
| self.async_set_state(STATE_CLOSING) | ||
|
|
||
| async def async_set_cover_position(self, **kwargs): | ||
| """Move the roller shutter to a specific position.""" | ||
| new_pos = kwargs.get(ATTR_POSITION) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Position is required. new_pos = kwargs[ATTR_POSITION] |
||
| await self._cover_channel.async_goto_lift_percent(100 - new_pos) | ||
| self.async_set_state( | ||
| STATE_CLOSING if new_pos < self._current_position else STATE_OPENING | ||
| ) | ||
|
|
||
| async def async_stop_cover(self, **kwargs): | ||
| """Stop the window cover.""" | ||
| await self._cover_channel.async_stop() | ||
| self.async_schedule_update_ha_state() | ||
|
|
||
| async def async_update(self): | ||
| """Attempt to retrieve the open/close state of the cover.""" | ||
| await super().async_update() | ||
| await self.async_get_state() | ||
|
|
||
| async def async_get_state(self, from_cache=True): | ||
| """Fetch the current state.""" | ||
| _LOGGER.debug("polling current state") | ||
| if self._cover_channel: | ||
| pos = await self._cover_channel.get_attribute_value( | ||
| "current_position_lift_percentage", from_cache=from_cache | ||
| ) | ||
| _LOGGER.debug("read pos=%s", pos) | ||
|
|
||
| if pos is not None: | ||
| self._current_position = 100 - pos | ||
| self._state = ( | ||
| STATE_OPEN if self.current_cover_position > 0 else STATE_CLOSED | ||
| ) | ||
| else: | ||
| self._current_position = None | ||
| self._state = None | ||
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,58 @@ | ||
| """Test zha cover.""" | ||
| import zigpy.zcl.clusters.closures as closures | ||
| import zigpy.zcl.clusters.general as general | ||
| import zigpy.zcl.foundation as zcl_f | ||
|
|
||
| from homeassistant.components.cover import DOMAIN | ||
| from homeassistant.const import STATE_CLOSED, STATE_OPEN, STATE_UNAVAILABLE | ||
|
|
||
| from .common import ( | ||
| async_enable_traffic, | ||
| async_init_zigpy_device, | ||
| find_entity_id, | ||
| make_attribute, | ||
| make_zcl_header, | ||
| ) | ||
|
|
||
|
|
||
| async def test_cover(hass, config_entry, zha_gateway): | ||
| """Test zha cover platform.""" | ||
|
|
||
| # create zigpy device | ||
| zigpy_device = await async_init_zigpy_device( | ||
| hass, | ||
| [closures.WindowCovering.cluster_id, general.Basic.cluster_id], | ||
| [], | ||
| None, | ||
| zha_gateway, | ||
| ) | ||
|
|
||
| # load up cover domain | ||
| await hass.config_entries.async_forward_entry_setup(config_entry, DOMAIN) | ||
| await hass.async_block_till_done() | ||
|
|
||
| cluster = zigpy_device.endpoints.get(1).window_covering | ||
| zha_device = zha_gateway.get_device(zigpy_device.ieee) | ||
| entity_id = await find_entity_id(DOMAIN, zha_device, hass) | ||
| assert entity_id is not None | ||
|
|
||
| # test that the cover was created and that it is unavailable | ||
| assert hass.states.get(entity_id).state == STATE_UNAVAILABLE | ||
|
|
||
| # allow traffic to flow through the gateway and device | ||
| await async_enable_traffic(hass, zha_gateway, [zha_device]) | ||
|
|
||
| attr = make_attribute(8, 100) | ||
| hdr = make_zcl_header(zcl_f.Command.Report_Attributes) | ||
| cluster.handle_message(hdr, [[attr]]) | ||
| await hass.async_block_till_done() | ||
|
|
||
| # test that the state has changed from unavailable to off | ||
| assert hass.states.get(entity_id).state == STATE_CLOSED | ||
|
|
||
| # test to see if it opens | ||
| attr = make_attribute(8, 0) | ||
| hdr = make_zcl_header(zcl_f.Command.Report_Attributes) | ||
| cluster.handle_message(hdr, [[attr]]) | ||
| await hass.async_block_till_done() | ||
| assert hass.states.get(entity_id).state == STATE_OPEN |
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.