From 73f714c88b7747f3db702439a332d779911a47e6 Mon Sep 17 00:00:00 2001 From: libots <989623+libots@users.noreply.github.com> Date: Sun, 21 Jun 2020 16:58:16 -0400 Subject: [PATCH 1/4] storage sensors --- homeassistant/components/proxmoxve/sensor.py | 112 +++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 homeassistant/components/proxmoxve/sensor.py diff --git a/homeassistant/components/proxmoxve/sensor.py b/homeassistant/components/proxmoxve/sensor.py new file mode 100644 index 00000000000000..ca88cdbf3063ff --- /dev/null +++ b/homeassistant/components/proxmoxve/sensor.py @@ -0,0 +1,112 @@ +"""Sensor to read Proxmox VE data.""" +import logging + +from homeassistant.const import ATTR_ATTRIBUTION, CONF_HOST, CONF_PORT +from homeassistant.helpers.entity import Entity + +from . import CONF_NODES, CONF_STORAGE, PROXMOX_CLIENTS, ProxmoxItemType + +ATTRIBUTION = "Data provided by Proxmox VE" +_LOGGER = logging.getLogger(__name__) + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the sensor platform.""" + + sensors = [] + + for entry in discovery_info["entries"]: + port = entry[CONF_PORT] + + for node in entry[CONF_NODES]: + for storage in node[CONF_STORAGE]: + sensors.append( + ProxmoxSensor( + hass.data[PROXMOX_CLIENTS][f"{entry[CONF_HOST]}:{port}"], + node["node"], + ProxmoxItemType.storage, + storage, + ) + ) + + add_entities(sensors, True) + + +class ProxmoxSensor(Entity): + """A binary sensor for reading Proxmox VE data.""" + + def __init__(self, proxmox_client, item_node, item_type, item_name): + """Initialize the binary sensor.""" + self._proxmox_client = proxmox_client + self._item_node = item_node + self._item_type = item_type + self._storagename = item_name + + self._name = None + self._maxdisk = None + self._usedisk = None + self._state = None + + @property + def name(self): + """Return the name of the entity.""" + return self._name + + @property + def unit_of_measurement(self): + """Return the unit the value is expressed in.""" + return "%" + + @property + def state(self): + """Return percent of storage used if storage is available.""" + return self._state + + @property + def device_state_attributes(self): + """Return device attributes of the entity.""" + return { + "node": self._item_node, + "storagename": self._storagename, + "type": self._type, + "gb_total": self._total, + "gb_used": self._used, + "gb_avail": self._avail, + "content": self._content, + ATTR_ATTRIBUTION: ATTRIBUTION, + } + + def update(self): + """Check if the storage exists.""" + item = self.poll_item() + + if item is None: + _LOGGER.warning("Failed to poll storage %s", self._storagename) + return + + self._state = round(item["used_fraction"] * 100, 1) + self._total = round(item["total"] / 1024 / 1024 / 1024, 1) + self._used = round(item["used"] / 1024 / 1024 / 1024, 1) + self._avail = round(item["avail"] / 1024 / 1024 / 1024, 1) + self._content = item["content"] + self._type = item["type"] + + def poll_item(self): + """Find the storage with the set name.""" + items = ( + self._proxmox_client.get_api_client() + .nodes(self._item_node) + .get(self._item_type.name) + ) + item = next( + (item for item in items if item["storage"] == str(self._storagename)), None + ) + + if item is None: + _LOGGER.warning("Couldn't find storage with the name %s", self._storagename) + return None + + if self._name is None: + self._name = f"{self._item_node} storage {self._storagename}" + + return item From cd380b8549111fbd2d63a1c2858f4a78a6367ae5 Mon Sep 17 00:00:00 2001 From: libots <989623+libots@users.noreply.github.com> Date: Sun, 21 Jun 2020 17:00:31 -0400 Subject: [PATCH 2/4] add support for storage sensors --- homeassistant/components/proxmoxve/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/homeassistant/components/proxmoxve/__init__.py b/homeassistant/components/proxmoxve/__init__.py index 0919feb15e3472..d87ff7c2716e47 100644 --- a/homeassistant/components/proxmoxve/__init__.py +++ b/homeassistant/components/proxmoxve/__init__.py @@ -26,6 +26,7 @@ CONF_NODES = "nodes" CONF_VMS = "vms" CONF_CONTAINERS = "containers" +CONF_STORAGE = "storage" DEFAULT_PORT = 8006 DEFAULT_REALM = "pam" @@ -58,6 +59,9 @@ vol.Optional(CONF_CONTAINERS, default=[]): [ cv.positive_int ], + vol.Optional(CONF_STORAGE, default=[]): [ + cv.string + ], } ) ], @@ -107,6 +111,9 @@ def setup(hass, config): hass.helpers.discovery.load_platform( "binary_sensor", DOMAIN, {"entries": config[DOMAIN]}, config ) + hass.helpers.discovery.load_platform( + "sensor", DOMAIN, {"entries": config[DOMAIN]}, config + ) return True return False @@ -117,6 +124,7 @@ class ProxmoxItemType(Enum): qemu = 0 lxc = 1 + storage = 2 class ProxmoxClient: From 62f5470dc60b7ed89ecab9590bd80447ae554260 Mon Sep 17 00:00:00 2001 From: libots <989623+libots@users.noreply.github.com> Date: Sun, 21 Jun 2020 17:28:10 -0400 Subject: [PATCH 3/4] Add variables to sensor __init__ --- homeassistant/components/proxmoxve/sensor.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/homeassistant/components/proxmoxve/sensor.py b/homeassistant/components/proxmoxve/sensor.py index ca88cdbf3063ff..09ab2c4a67dde7 100644 --- a/homeassistant/components/proxmoxve/sensor.py +++ b/homeassistant/components/proxmoxve/sensor.py @@ -46,6 +46,11 @@ def __init__(self, proxmox_client, item_node, item_type, item_name): self._maxdisk = None self._usedisk = None self._state = None + self._total = None + self._used = None + self._avail = None + self._content = None + self._type = None @property def name(self): From cad117c1dcaa4921a0227447b1b167f93a80dba9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 9 Jan 2023 06:00:46 +0000 Subject: [PATCH 4/4] Bump actions/upload-artifact from 2.0.1 to 3.1.2 Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 2.0.1 to 3.1.2. - [Release notes](https://github.com/actions/upload-artifact/releases) - [Commits](https://github.com/actions/upload-artifact/compare/v2.0.1...v3.1.2) --- updated-dependencies: - dependency-name: actions/upload-artifact dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 74d692efb945ae..e2f3906482e4af 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -738,7 +738,7 @@ jobs: -p no:sugar \ tests - name: Upload coverage artifact - uses: actions/upload-artifact@v2.0.1 + uses: actions/upload-artifact@v3.1.2 with: name: coverage-${{ matrix.python-version }}-group${{ matrix.group }} path: .coverage