-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Preliminary support for Matter cover #90262
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
+1,017
−0
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fcd769c
preliminary support for Matter cover
hidaris ec0dc90
add matter cover test, tweak some code structure
hidaris d1528f6
rename matter cover test case
hidaris 0298fb0
change operational status to snake case, reorder match case
hidaris ff5b61e
fix lint problem in matter cover
hidaris 50c988a
change operational status from int flag to enum
hidaris b1b8bf3
fix cover entity id in tests
hidaris aca0091
fix matter cover tests
hidaris 65ee421
keep discovery schemas sorted
hidaris 5e5fae3
recover matter cover state tests
hidaris c60379b
Adjust the coding style related to OperationalStatus and remove unuse…
hidaris 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| """Matter cover.""" | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from chip.clusters import Objects as clusters | ||
| from matter_server.client.models import device_types | ||
|
|
||
| from homeassistant.components.cover import ( | ||
| ATTR_POSITION, | ||
| CoverEntity, | ||
| CoverEntityDescription, | ||
| CoverEntityFeature, | ||
| ) | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import Platform | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from .const import LOGGER | ||
| from .entity import MatterEntity | ||
| from .helpers import get_matter | ||
| from .models import MatterDiscoverySchema | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
| async_add_entities: AddEntitiesCallback, | ||
| ) -> None: | ||
| """Set up Matter cover from Config Entry.""" | ||
| matter = get_matter(hass) | ||
| matter.register_platform_handler(Platform.COVER, async_add_entities) | ||
|
|
||
|
|
||
| class MatterCover(MatterEntity, CoverEntity): | ||
| """Representation of a Matter cover.""" | ||
|
|
||
| entity_description: CoverEntityDescription | ||
|
|
||
| @property | ||
| def supported_features(self) -> CoverEntityFeature: | ||
| """Flag supported features.""" | ||
| features = ( | ||
| CoverEntityFeature.OPEN | ||
| | CoverEntityFeature.CLOSE | ||
| | CoverEntityFeature.STOP | ||
| | CoverEntityFeature.SET_POSITION | ||
| ) | ||
| return features | ||
|
|
||
| @property | ||
| def current_cover_position(self) -> int: | ||
| """Return the current position of cover.""" | ||
| if self._attr_current_cover_position: | ||
| current_position = self._attr_current_cover_position | ||
| else: | ||
| current_position = self.get_matter_attribute_value( | ||
| clusters.WindowCovering.Attributes.CurrentPositionLiftPercentage | ||
| ) | ||
|
|
||
| assert current_position is not None | ||
|
|
||
| LOGGER.info( | ||
|
hidaris marked this conversation as resolved.
Outdated
|
||
| "Got current position %s for %s", | ||
| current_position, | ||
| self.entity_id, | ||
| ) | ||
|
|
||
| return current_position | ||
|
|
||
| @property | ||
| def is_closed(self) -> bool: | ||
| """Return true if cover is closed, else False.""" | ||
| return self.current_cover_position == 0 | ||
|
|
||
| @property | ||
| def is_closing(self) -> bool: | ||
| """Return if the cover is closing or not.""" | ||
| operational_status = self.get_matter_attribute_value( | ||
|
hidaris marked this conversation as resolved.
Outdated
|
||
| clusters.WindowCovering.Attributes.OperationalStatus | ||
| ) | ||
|
|
||
| assert operational_status is not None | ||
|
|
||
| LOGGER.debug( | ||
| "GOT OPERATIONAL STATUS %s for %s", operational_status, self.entity_id | ||
| ) | ||
| state = operational_status & 0b11 | ||
|
hidaris marked this conversation as resolved.
Outdated
|
||
| return state == 0b10 | ||
|
|
||
| @property | ||
| def is_opening(self) -> bool: | ||
| """Return if the cover is opening or not.""" | ||
| operational_status = self.get_matter_attribute_value( | ||
| clusters.WindowCovering.Attributes.OperationalStatus | ||
| ) | ||
|
|
||
| assert operational_status is not None | ||
|
|
||
| LOGGER.debug( | ||
| "GOT OPERATIONAL STATUS %s for %s", operational_status, self.entity_id | ||
| ) | ||
| state = operational_status & 0b11 | ||
| return state == 0b01 | ||
|
|
||
| async def async_stop_cover(self, **kwargs: Any) -> None: | ||
| """Stop the cover movement.""" | ||
| await self.send_device_command(clusters.WindowCovering.Commands.StopMotion()) | ||
|
|
||
| async def async_open_cover(self, **kwargs: Any) -> None: | ||
| """Open the cover.""" | ||
| await self.send_device_command(clusters.WindowCovering.Commands.UpOrOpen()) | ||
|
|
||
| async def async_close_cover(self, **kwargs: Any) -> None: | ||
| """Close the cover.""" | ||
| await self.send_device_command(clusters.WindowCovering.Commands.DownOrClose()) | ||
|
|
||
| async def async_set_cover_position(self, **kwargs: Any) -> None: | ||
| """Set the cover to a specific position.""" | ||
| position = kwargs[ATTR_POSITION] | ||
| await self.send_device_command( | ||
| clusters.WindowCovering.Commands.GoToLiftPercentage(position) | ||
| ) | ||
|
|
||
| async def send_device_command(self, command: Any) -> None: | ||
| """Send device command.""" | ||
| await self.matter_client.send_device_command( | ||
| node_id=self._endpoint.node.node_id, | ||
| endpoint_id=self._endpoint.endpoint_id, | ||
| command=command, | ||
| ) | ||
|
|
||
| @callback | ||
| def _update_from_device(self) -> None: | ||
| """Update from device.""" | ||
| self._attr_current_cover_position = self.get_matter_attribute_value( | ||
| clusters.WindowCovering.Attributes.CurrentPositionLiftPercentage | ||
| ) | ||
| LOGGER.info("GOT CURRENT POSITION %s", self._attr_current_cover_position) | ||
|
hidaris marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| # Discovery schema(s) to map Matter Attributes to HA entities | ||
| DISCOVERY_SCHEMAS = [ | ||
| MatterDiscoverySchema( | ||
| platform=Platform.COVER, | ||
| entity_description=CoverEntityDescription(key="MatterCover"), | ||
| entity_class=MatterCover, | ||
| required_attributes=( | ||
| clusters.WindowCovering.Attributes.CurrentPositionLiftPercent100ths, | ||
| clusters.WindowCovering.Attributes.OperationalStatus, | ||
| ), | ||
| optional_attributes=(), | ||
|
hidaris marked this conversation as resolved.
Outdated
|
||
| # restrict device type to prevent discovery in switch platform | ||
|
hidaris marked this conversation as resolved.
Outdated
|
||
| not_device_type=(device_types.OnOffPlugInUnit,), | ||
| ), | ||
| ] | ||
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
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.