-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Key sensors for HomeKit Remotes #35421
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """The homekitintegration base entity.""" | ||
|
|
||
| from homeassistant.helpers.entity import Entity | ||
|
|
||
| from .const import DOMAIN, MANUFACTURER | ||
|
|
||
|
|
||
| class HomeKitEntity(Entity): | ||
| """Base class for homekit entities.""" | ||
|
|
||
| def __init__(self, bridge_mac, acc, model): | ||
| """Initialize the sensor.""" | ||
| super().__init__() | ||
| self.model = model | ||
| self.acc = acc | ||
| self.bridge_mac = bridge_mac | ||
| self.base_unique_id = f"{bridge_mac}_{acc.aid}" | ||
|
|
||
| @property | ||
| def device_info(self): | ||
| """Entity device info.""" | ||
| device_info = { | ||
| "identifiers": {(DOMAIN, self.base_unique_id)}, | ||
| "name": self.name, | ||
| "manufacturer": MANUFACTURER, | ||
| "model": self.model, | ||
| "via_device": (DOMAIN, self.bridge_mac), | ||
| } | ||
| return device_info | ||
|
|
||
| @property | ||
| def should_poll(self): | ||
| """Update from callback.""" | ||
| return False | ||
|
|
||
| async def async_remove(self): | ||
| """Remove from entity registry on remove.""" | ||
| entity_registry = await self.hass.helpers.entity_registry.async_get_registry() | ||
| if entity_registry.async_get(self.entity_id): | ||
| entity_registry.async_remove(self.entity_id) | ||
| return await super().async_remove() |
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,66 @@ | ||
| """Support for homekit sensors.""" | ||
| import logging | ||
|
|
||
| from homeassistant.core import callback | ||
|
|
||
| from .const import DOMAIN, HOMEKIT | ||
| from .entity import HomeKitEntity | ||
| from .type_media_players import TelevisionMediaPlayer | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| HOMEKIT_REMOTE_MODEL = "HomeKit Remote" | ||
|
|
||
|
|
||
| async def async_setup_entry(hass, config_entry, async_add_entities): | ||
| """Set up the homekit sensors.""" | ||
|
|
||
| homekit = hass.data[DOMAIN][config_entry.entry_id][HOMEKIT] | ||
| mac = homekit.driver.state.mac | ||
| entities = [] | ||
|
|
||
| for acc in homekit.bridge.accessories.values(): | ||
| if isinstance(acc, TelevisionMediaPlayer): | ||
| entities.append(RemoteKeySensor(mac, acc)) | ||
|
|
||
| async_add_entities(entities, False) | ||
|
|
||
|
|
||
| class RemoteKeySensor(HomeKitEntity): | ||
| """Representation of an homekit remote control sensor.""" | ||
|
|
||
| def __init__(self, mac, acc): | ||
| """Init the remote control sensor.""" | ||
| super().__init__(mac, acc, HOMEKIT_REMOTE_MODEL) | ||
| self._state = None | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Sensor Name.""" | ||
| return f"{self.acc.name} {self.model}" | ||
|
|
||
| @property | ||
| def unique_id(self): | ||
| """Sensor Uniqueid.""" | ||
| return f"{self.base_unique_id}_remote" | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Key pressed.""" | ||
| return self._state | ||
|
|
||
| @callback | ||
| def _async_key_pressed(self, value): | ||
| """Update the sensor with the keypress.""" | ||
| # Press | ||
| self._state = value | ||
| self.async_write_ha_state() | ||
| # .. and release | ||
| self._state = None | ||
| self.async_write_ha_state() | ||
|
|
||
| async def async_added_to_hass(self): | ||
| """Subscribe to updates.""" | ||
| self.async_on_remove( | ||
| self.acc.async_add_remote_key_listener(self._async_key_pressed) | ||
| ) | ||
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.