Skip to content

Commit

Permalink
Add support for the new Select Entity (#27)
Browse files Browse the repository at this point in the history
* Adding support for the new select entity

* Newline

* Update minimum homeassistant version

Co-authored-by: Tomas Hellström <[email protected]>
  • Loading branch information
Laccolith and helto4real authored Jan 31, 2022
1 parent 9ce2911 commit 568dfcf
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions custom_components/netdaemon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ATTR_ICON,
ATTR_STATE,
ATTR_UNIT,
ATTR_OPTIONS,
LOGGER,
STORAGE_VERSION,
)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion custom_components/netdaemon/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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,
]

Expand Down
67 changes: 67 additions & 0 deletions custom_components/netdaemon/select.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions custom_components/netdaemon/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 568dfcf

Please sign in to comment.