This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial import from https://github.com/sdwilsh/hass-freenas
- Loading branch information
0 parents
commit 41e2f84
Showing
9 changed files
with
526 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,129 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Shawn Wilsher | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,11 @@ | ||
# Python Module for FreeNAS Websocket API | ||
|
||
## Development | ||
|
||
``` | ||
python3.8 -m venv .venv | ||
source .venv/bin/activate | ||
# Install Requirements | ||
pip install -r requirements.txt | ||
``` |
This file contains 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,96 @@ | ||
import asyncio | ||
import websockets | ||
|
||
from .disk import Disk | ||
from .virtualmachine import VirturalMachine | ||
from .websockets_custom import FreeNASWebSocketClientProtocol, freenas_auth_protocol_factory | ||
from typing import Any, Callable, Dict, List, TypeVar | ||
|
||
T = TypeVar('T', bound='Controller') | ||
|
||
|
||
class Controller(object): | ||
_client: FreeNASWebSocketClientProtocol | ||
|
||
@classmethod | ||
async def create(cls, host: str, password: str, username: str = 'root') -> T: | ||
self = Controller() | ||
self._client = await websockets.connect(f"ws://{host}/websocket", create_protocol=freenas_auth_protocol_factory(username, password)) | ||
self._info = await self._client.invoke_method("system.info") | ||
self._state = None | ||
self._disks = None | ||
self._vms = None | ||
return self | ||
|
||
async def refresh(self) -> None: | ||
self._state = { | ||
"disks": await self._fetch_disks(), | ||
"vms": await self._fetch_vms(), | ||
} | ||
self._update_properties_from_state() | ||
|
||
async def _fetch_disks(self) -> Dict[str, dict]: | ||
disks = await self._client.invoke_method( | ||
"disk.query", | ||
[ | ||
[], | ||
{ | ||
"select": [ | ||
"description", | ||
"model", | ||
"name", | ||
"serial", | ||
"size", | ||
"type", | ||
], | ||
} | ||
], | ||
) | ||
disks = {disk["name"]: disk for disk in disks} | ||
temps = await self._client.invoke_method( | ||
"disk.temperatures", | ||
[ | ||
[disk for disk in disks], | ||
], | ||
) | ||
for name, temp in temps.items(): | ||
disks[name]['temperature'] = temp | ||
|
||
return disks | ||
|
||
async def _fetch_vms(self) -> Dict[str, dict]: | ||
vms = await self._client.invoke_method( | ||
'vm.query', | ||
[ | ||
[], | ||
{ | ||
'select': [ | ||
'id', | ||
'name', | ||
'description', | ||
'status', | ||
], | ||
}, | ||
], | ||
) | ||
return {vm["id"]: vm for vm in vms} | ||
|
||
def _update_properties_from_state(self) -> None: | ||
self._disks = [Disk(controller=self, name=disk_name) | ||
for disk_name in self._state["disks"]] | ||
self._vms = [VirturalMachine( | ||
controller=self, id=vm_id) for vm_id in self._state["vms"]] | ||
|
||
@property | ||
def disks(self) -> List[Disk]: | ||
"""Returns a list of disks attached to the host.""" | ||
return self._disks | ||
|
||
@property | ||
def info(self) -> Dict[str, Any]: | ||
return self._info | ||
|
||
@property | ||
def vms(self) -> List[VirturalMachine]: | ||
"""Returns a list of virtual machines on the host.""" | ||
return self._vms |
This file contains 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,90 @@ | ||
import asyncio | ||
|
||
from enum import Enum, unique | ||
from typing import Any, Callable, TypeVar | ||
|
||
TController = TypeVar("TController", bound="Controller") | ||
TType = TypeVar("TType", bound="DiskType") | ||
|
||
@unique | ||
class DiskType(Enum): | ||
HDD = "HDD" | ||
SSD = "SSD" | ||
|
||
@classmethod | ||
def fromValue(cls, value: str) -> TType: | ||
if value == cls.HDD.value: | ||
return cls.HDD | ||
if value == cls.SSD.value: | ||
return cls.SSD | ||
raise Exception(f"Unexpected disk type '{value}'") | ||
|
||
class Disk(object): | ||
def __init__(self, controller: TController, name: str) -> None: | ||
self._controller = controller | ||
self._name = name | ||
self._cached_state = self._state | ||
|
||
@property | ||
def available(self) -> bool: | ||
"""If the disk exists on the server.""" | ||
return self._name in self._controller._state["disks"] | ||
|
||
@property | ||
def description(self) -> str: | ||
"""The description of the desk.""" | ||
if self.available: | ||
self._cached_state = self._state | ||
return self._state["description"] | ||
return self._cached_state["description"] | ||
|
||
@property | ||
def model(self) -> str: | ||
"""The model of the disk.""" | ||
if self.available: | ||
self._cached_state = self._state | ||
return self._state["model"] | ||
return self._cached_state["model"] | ||
|
||
@property | ||
def name(self) -> str: | ||
"""The name of the disk.""" | ||
if self.available: | ||
self._cached_state = self._state | ||
return self._state["name"] | ||
return self._cached_state["name"] | ||
|
||
@property | ||
def serial(self) -> str: | ||
"""The serial of the disk.""" | ||
if self.available: | ||
self._cached_state = self._state | ||
return self._state["serial"] | ||
return self._cached_state["serial"] | ||
|
||
@property | ||
def size(self) -> int: | ||
"""The size of the disk.""" | ||
if self.available: | ||
self._cached_state = self._state | ||
return self._state["size"] | ||
return self._cached_state["size"] | ||
|
||
@property | ||
def temperature(self) -> int: | ||
"""The temperature of the disk.""" | ||
assert self.available | ||
return self._state["temperature"] | ||
|
||
@property | ||
def type(self) -> DiskType: | ||
"""The type of the desk.""" | ||
if self.available: | ||
self._cached_state = self._state | ||
return DiskType.fromValue(self._state["type"]) | ||
return DiskType.fromValue(self._cached_state["type"]) | ||
|
||
@property | ||
def _state(self) -> dict: | ||
"""The state of the desk, according to the Controller.""" | ||
return self._controller._state["disks"][self._name] |
Oops, something went wrong.