-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Add ISY994 variables as number entities #85511
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
+201
−5
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c294e2b
Add ISY994 variables as number entities
shbatm 5a19706
Cleanup based on other reviews
shbatm e713b45
Changes requested in review
shbatm 8ff0b04
Fix nesting of data dict
shbatm c474c73
Merge branch 'dev' into isy994_variables2
shbatm 70ae628
Merge branch 'dev' into isy994_variables2
shbatm 07c3ddf
Merge branch 'dev' into isy994_variables2
shbatm 590d8a1
Merge branch 'dev' into isy994_variables2
shbatm 5f4e318
avoid the dict lookups every loop
shbatm 05fa0a5
lint
bdraco c10ea7d
Update homeassistant/components/isy994/number.py
bdraco 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
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,162 @@ | ||
| """Support for ISY number entities.""" | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from pyisy import ISY | ||
| from pyisy.helpers import EventListener, NodeProperty | ||
| from pyisy.variables import Variable | ||
|
|
||
| from homeassistant.components.number import NumberEntity, NumberEntityDescription | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import Platform | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.helpers.device_registry import DeviceEntryType | ||
| from homeassistant.helpers.entity import DeviceInfo, EntityCategory | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from . import _async_isy_to_configuration_url | ||
| from .const import ( | ||
| DOMAIN as ISY994_DOMAIN, | ||
| ISY994_ISY, | ||
| ISY994_VARIABLES, | ||
| ISY_CONF_FIRMWARE, | ||
| ISY_CONF_MODEL, | ||
| ISY_CONF_NAME, | ||
| ISY_CONF_UUID, | ||
| MANUFACTURER, | ||
| ) | ||
| from .helpers import convert_isy_value_to_hass | ||
|
|
||
| ISY_MAX_SIZE = (2**32) / 2 | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
| async_add_entities: AddEntitiesCallback, | ||
| ) -> None: | ||
| """Set up ISY/IoX number entities from config entry.""" | ||
| hass_isy_data = hass.data[ISY994_DOMAIN][config_entry.entry_id] | ||
| isy: ISY = hass_isy_data[ISY994_ISY] | ||
| uuid = isy.configuration[ISY_CONF_UUID] | ||
| entities: list[ISYVariableNumberEntity] = [] | ||
|
|
||
| for node, enable_by_default in hass_isy_data[ISY994_VARIABLES][Platform.NUMBER]: | ||
| step = 10 ** (-1 * node.prec) | ||
| min_max = ISY_MAX_SIZE / (10**node.prec) | ||
| description = NumberEntityDescription( | ||
| key=node.address, | ||
| name=node.name, | ||
| icon="mdi:counter", | ||
| entity_registry_enabled_default=enable_by_default, | ||
| native_unit_of_measurement=None, | ||
| native_step=step, | ||
| native_min_value=-min_max, | ||
| native_max_value=min_max, | ||
| ) | ||
| description_init = NumberEntityDescription( | ||
| key=f"{node.address}_init", | ||
| name=f"{node.name} Initial Value", | ||
| icon="mdi:counter", | ||
| entity_registry_enabled_default=False, | ||
| native_unit_of_measurement=None, | ||
| native_step=step, | ||
| native_min_value=-min_max, | ||
| native_max_value=min_max, | ||
| entity_category=EntityCategory.CONFIG, | ||
| ) | ||
|
|
||
| entities.append( | ||
| ISYVariableNumberEntity( | ||
| node, | ||
| unique_id=f"{uuid}_{node.address}", | ||
| description=description, | ||
| ) | ||
| ) | ||
| entities.append( | ||
| ISYVariableNumberEntity( | ||
| node=node, | ||
| unique_id=f"{uuid}_{node.address}_init", | ||
| description=description_init, | ||
| init_entity=True, | ||
| ) | ||
| ) | ||
|
|
||
| async_add_entities(entities) | ||
|
|
||
|
|
||
| class ISYVariableNumberEntity(NumberEntity): | ||
| """Representation of an ISY variable as a number entity device.""" | ||
|
|
||
| _attr_has_entity_name = True | ||
| _attr_should_poll = False | ||
| _init_entity: bool | ||
| _node: Variable | ||
| entity_description: NumberEntityDescription | ||
|
|
||
| def __init__( | ||
| self, | ||
| node: Variable, | ||
| unique_id: str, | ||
| description: NumberEntityDescription, | ||
| init_entity: bool = False, | ||
| ) -> None: | ||
| """Initialize the ISY variable number.""" | ||
| self._node = node | ||
| self._name = description.name | ||
| self.entity_description = description | ||
| self._change_handler: EventListener | None = None | ||
|
|
||
| # Two entities are created for each variable, one for current value and one for initial. | ||
| # Initial value entities are disabled by default | ||
| self._init_entity = init_entity | ||
|
|
||
| self._attr_unique_id = unique_id | ||
|
|
||
| url = _async_isy_to_configuration_url(node.isy) | ||
| config = node.isy.configuration | ||
| self._attr_device_info = DeviceInfo( | ||
| identifiers={ | ||
| ( | ||
| ISY994_DOMAIN, | ||
| f"{config[ISY_CONF_UUID]}_variables", | ||
| ) | ||
| }, | ||
| manufacturer=MANUFACTURER, | ||
| name=f"{config[ISY_CONF_NAME]} Variables", | ||
| model=config[ISY_CONF_MODEL], | ||
| sw_version=config[ISY_CONF_FIRMWARE], | ||
| configuration_url=url, | ||
| via_device=(ISY994_DOMAIN, config[ISY_CONF_UUID]), | ||
| entry_type=DeviceEntryType.SERVICE, | ||
| ) | ||
|
|
||
| async def async_added_to_hass(self) -> None: | ||
| """Subscribe to the node change events.""" | ||
| self._change_handler = self._node.status_events.subscribe(self.async_on_update) | ||
|
|
||
| @callback | ||
| def async_on_update(self, event: NodeProperty) -> None: | ||
| """Handle the update event from the ISY Node.""" | ||
| self.async_write_ha_state() | ||
|
|
||
| @property | ||
| def native_value(self) -> float | int | None: | ||
| """Return the state of the variable.""" | ||
| return convert_isy_value_to_hass( | ||
| self._node.init if self._init_entity else self._node.status, | ||
| "", | ||
| self._node.prec, | ||
| ) | ||
|
|
||
| @property | ||
| def extra_state_attributes(self) -> dict[str, Any]: | ||
| """Get the state attributes for the device.""" | ||
| return { | ||
| "last_edited": self._node.last_edited, | ||
| } | ||
|
|
||
| async def async_set_native_value(self, value: float) -> None: | ||
| """Set new value.""" | ||
| await self._node.set_value(value, init=self._init_entity) | ||
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
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.