From 568dfcfcfacd595cee57ae16adbe88badcd9d1b8 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Mon, 31 Jan 2022 09:15:25 -0500 Subject: [PATCH] Add support for the new Select Entity (#27) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adding support for the new select entity * Newline * Update minimum homeassistant version Co-authored-by: Tomas Hellström --- README.md | 3 +- custom_components/netdaemon/client.py | 2 + custom_components/netdaemon/const.py | 5 +- custom_components/netdaemon/select.py | 67 +++++++++++++++++++++++ custom_components/netdaemon/services.yaml | 4 ++ hacs.json | 2 +- 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 custom_components/netdaemon/select.py diff --git a/README.md b/README.md index 049c32a..946f0b9 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,12 @@ This is a helper integration for NetDaemon, this is not NetDeamon it self, for t - Service to register custom services to use in NetDaemon - Service to force a reload of NetDaemon -For now only the folowing platforms are supported: +For now only the following platforms are supported: - `binary_sensor` - `sensor` - `switch` +- `select` - `climate` ## Installation diff --git a/custom_components/netdaemon/client.py b/custom_components/netdaemon/client.py index f798c58..e9ba5f0 100644 --- a/custom_components/netdaemon/client.py +++ b/custom_components/netdaemon/client.py @@ -11,6 +11,7 @@ ATTR_ICON, ATTR_STATE, ATTR_UNIT, + ATTR_OPTIONS, LOGGER, STORAGE_VERSION, ) @@ -52,6 +53,7 @@ async def entity_create(self, data) -> None: ATTR_STATE: data.get(ATTR_STATE), ATTR_ICON: data.get(ATTR_ICON), ATTR_UNIT: data.get(ATTR_UNIT), + ATTR_OPTIONS: data.get(ATTR_OPTIONS), ATTR_ATTRIBUTES: data.get(ATTR_ATTRIBUTES, {}), } await self.store.async_save(self._entities) diff --git a/custom_components/netdaemon/const.py b/custom_components/netdaemon/const.py index 74a4a28..995b12c 100644 --- a/custom_components/netdaemon/const.py +++ b/custom_components/netdaemon/const.py @@ -6,7 +6,7 @@ INTEGRATION_VERSION = "main" DOMAIN = "netdaemon" NAME = "NetDaemon" -MINIMUM_HA_VERSION = "2020.12.0" +MINIMUM_HA_VERSION = "2021.7.0" STORAGE_VERSION = "1" ND_ID = "86ec6a70-b2b8-427d-8fcf-3f14331dddd7" @@ -23,6 +23,7 @@ ATTR_UNIT = "unit" ATTR_ATTRIBUTES = "attributes" ATTR_VERSION = "version" +ATTR_OPTIONS = "options" ATTR_TARGET_TEMPERATURE = "target_temperature" ATTR_TARGET_HUMIDITY = "target_humidity" ATTR_TEMPERATURE_UNIT = "temperature_unit" @@ -58,12 +59,14 @@ PLATFORM_BINARY_SENSOR = "binary_sensor" PLATFORM_SENSOR = "sensor" PLATFORM_SWITCH = "switch" +PLATFORM_SELECT = "select" PLATFORM_CLIMATE = "climate" PLATFORMS = [ PLATFORM_BINARY_SENSOR, PLATFORM_SENSOR, PLATFORM_SWITCH, + PLATFORM_SELECT, PLATFORM_CLIMATE, ] diff --git a/custom_components/netdaemon/select.py b/custom_components/netdaemon/select.py new file mode 100644 index 0000000..63eb8a0 --- /dev/null +++ b/custom_components/netdaemon/select.py @@ -0,0 +1,67 @@ +"""Select platform for NetDaemon.""" +from typing import TYPE_CHECKING + +from homeassistant.components.select import SelectEntity + +from .const import ( + ATTR_CLIENT, + ATTR_COORDINATOR, + ATTR_ENTITY_ID, + ATTR_STATE, + ATTR_OPTIONS, + DOMAIN, + LOGGER, + PLATFORM_SELECT, +) +from .entity import NetDaemonEntity + +if TYPE_CHECKING: + from homeassistant.config_entries import ConfigEntry + from homeassistant.core import HomeAssistant + from homeassistant.helpers.update_coordinator import DataUpdateCoordinator + + from .client import NetDaemonClient + +async def async_setup_entry( + hass: "HomeAssistant", _config_entry: "ConfigEntry", async_add_devices +) -> None: + """Setup select platform.""" + client: "NetDaemonClient" = hass.data[DOMAIN][ATTR_CLIENT] + coordinator: "DataUpdateCoordinator" = hass.data[DOMAIN][ATTR_COORDINATOR] + + selects = [] + for entity in client.entities: + if entity.split(".")[0] == PLATFORM_SELECT: + LOGGER.debug("Adding %s", entity) + selects.append( + NetDaemonSelect(coordinator, entity.split(".")[1]) + ) + + if selects: + async_add_devices(selects) + + +class NetDaemonSelect(NetDaemonEntity, SelectEntity): + """NetDaemon select class.""" + + @property + def current_option(self): + """Return the state of the select.""" + if not self.entity_id: + return None + state = str(self._coordinator.data[self.entity_id][ATTR_STATE]) + return state + + @property + def options(self): + """Return the list of available options.""" + if not self.entity_id: + return None + return self._coordinator.data[self.entity_id][ATTR_OPTIONS] + + async def async_select_option(self, option: str) -> None: + """Change the selected option.""" + await self.hass.data[DOMAIN][ATTR_CLIENT].entity_update( + {ATTR_ENTITY_ID: self.entity_id, ATTR_STATE: option} + ) + self.async_write_ha_state() diff --git a/custom_components/netdaemon/services.yaml b/custom_components/netdaemon/services.yaml index 044bb44..3cf1256 100644 --- a/custom_components/netdaemon/services.yaml +++ b/custom_components/netdaemon/services.yaml @@ -25,6 +25,8 @@ entity_create: example: "mdi:rocket-launch-outline" unit: description: The unit of measurement for the entity + options: + description: List of options for a select entity attributes: description: The attributes of the entity @@ -42,6 +44,8 @@ entity_update: example: "mdi:rocket-launch-outline" unit: description: The unit of measurement for the entity + options: + description: List of options for a select entity attributes: description: The attributes of the entity diff --git a/hacs.json b/hacs.json index 5b5c7d0..be703ab 100644 --- a/hacs.json +++ b/hacs.json @@ -3,6 +3,6 @@ "filename": "netdaemon.zip", "zip_release": true, "hide_default_branch": true, - "homeassistant": "2020.12.0", + "homeassistant": "2021.7.0", "hacs": "0.19.0" } \ No newline at end of file