Initial commit
This commit is contained in:
2
.gitattributes
vendored
Normal file
2
.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
152
.gitignore
vendored
Normal file
152
.gitignore
vendored
Normal file
@@ -0,0 +1,152 @@
|
||||
# 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/
|
||||
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/
|
||||
cover/
|
||||
|
||||
# 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
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .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
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.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/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
11
pyproject.toml
Normal file
11
pyproject.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[project]
|
||||
name = 'room_control'
|
||||
version = "1.0.0"
|
||||
description = "Various utilities for a single room controlling app"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.8"
|
||||
dependencies = [
|
||||
'pvlib'
|
||||
]
|
||||
|
||||
[tool.setuptools.packages.find]
|
||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
pvlib
|
||||
0
room_control/__init__.py
Normal file
0
room_control/__init__.py
Normal file
141
room_control/daylight_adjuster.py
Normal file
141
room_control/daylight_adjuster.py
Normal file
@@ -0,0 +1,141 @@
|
||||
import logging
|
||||
from dataclasses import InitVar, dataclass, field
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Iterable
|
||||
|
||||
import matplotlib.dates as mdates
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
import pvlib
|
||||
|
||||
|
||||
def format_x_axis(fig):
|
||||
ax: plt.Axes = fig.axes[0]
|
||||
# ax.xaxis.axis_date(tz=HOME_TZ)
|
||||
# logging.info(HOME_TZ)
|
||||
ax.xaxis.set_major_locator(mdates.HourLocator(byhour=range(0, 24, 2)))
|
||||
ax.xaxis.set_major_formatter(mdates.DateFormatter('%I%p'))
|
||||
ax.grid(True)
|
||||
fig.autofmt_xdate()
|
||||
|
||||
|
||||
HOME_TZ = datetime.now().astimezone().tzinfo
|
||||
|
||||
|
||||
@dataclass
|
||||
class DaylightAdjuster:
|
||||
location: pvlib.location.Location
|
||||
brightness_range: Iterable[int] = field(default=(0, 100))
|
||||
periods: InitVar[int] = field(default=200)
|
||||
datetime: datetime = field(default_factory=datetime.now)
|
||||
|
||||
def __post_init__(self, periods: int):
|
||||
self.logger: logging.Logger = logging.getLogger(type(self).__name__)
|
||||
today = self.datetime.date()
|
||||
times = pd.date_range(
|
||||
today, today + timedelta(days=1),
|
||||
periods=periods,
|
||||
tz=HOME_TZ
|
||||
)
|
||||
self.logger.info(
|
||||
f'{type(times).__name__}:\n' +
|
||||
'\n'.join(f' {dt}' for dt in times[:5]) +
|
||||
'\n ...\n' +
|
||||
'\n'.join(f' {dt}' for dt in times[-5:])
|
||||
)
|
||||
|
||||
df = self.location.get_solarposition(times)
|
||||
df.index = df.index.tz_localize(None)
|
||||
|
||||
min_e, max_e = df['elevation'].min(), df['elevation'].max()
|
||||
self.elevation_range = (min_e, max_e)
|
||||
|
||||
df['pct_elevation'] = (df['elevation'] - min_e) / (max_e - min_e)
|
||||
df['brightness'] = (df['pct_elevation'] * (self.brightness_range[1] - self.brightness_range[0])
|
||||
) + self.brightness_range[0]
|
||||
# df['brightness'] = df['brightness'].round(0).astype(int)
|
||||
self.df = df[['elevation', 'pct_elevation', 'brightness']]
|
||||
|
||||
@property
|
||||
def elevation(self):
|
||||
return self.df['elevation']
|
||||
|
||||
def elevation_fig(self):
|
||||
fig, ax = plt.subplots(figsize=(10, 7))
|
||||
handles = ax.plot(self.elevation)
|
||||
|
||||
ax.set_ylabel('Elevation')
|
||||
ax.set_ylim(-100, 100)
|
||||
format_x_axis(fig)
|
||||
ax.set_xlim(self.df.index[0], self.df.index[-1])
|
||||
|
||||
ax2 = ax.twinx()
|
||||
handles.extend(ax2.plot(
|
||||
self.df['brightness'], 'r',
|
||||
# drawstyle='steps'
|
||||
))
|
||||
ax2.set_ylabel('Brightness')
|
||||
ax2.set_ylim(0, 255)
|
||||
|
||||
handles.append(ax.axvline(datetime.now(),
|
||||
linestyle='--',
|
||||
color='g'))
|
||||
|
||||
handles.append(ax2.axhline(self.get_brightness(),
|
||||
linestyle='--',
|
||||
color='r'))
|
||||
|
||||
handles.append(ax.axhline(self.get_elevation(),
|
||||
linestyle='--',
|
||||
color=handles[0].get_color()))
|
||||
|
||||
ax.legend(handles=handles, loc='lower center', labels=[
|
||||
'Sun Elevation Angle',
|
||||
'Brightness Setting',
|
||||
'Current Time',
|
||||
'Current Brightness',
|
||||
'Current Elevation'
|
||||
])
|
||||
|
||||
fig.tight_layout()
|
||||
plt.close(fig)
|
||||
return fig
|
||||
|
||||
def get_solar_position(self, dt: datetime = None):
|
||||
dt = dt or datetime.now()
|
||||
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=HOME_TZ)
|
||||
|
||||
return pvlib.solarposition.get_solarposition(
|
||||
dt.astimezone(None),
|
||||
latitude=self.location.latitude,
|
||||
longitude=self.location.longitude
|
||||
)
|
||||
|
||||
def get_elevation(self, time=None):
|
||||
time = time or datetime.now()
|
||||
return self.get_solar_position(dt=time).iloc[0].loc['elevation']
|
||||
|
||||
def get_brightness(self, time=None):
|
||||
time = time or datetime.now()
|
||||
|
||||
min_e, max_e = self.elevation_range
|
||||
rng_e = max_e - min_e
|
||||
|
||||
min_b, max_b = self.brightness_range
|
||||
rng_b = max_b - min_b
|
||||
|
||||
current_elevation = self.get_elevation(time=time)
|
||||
pct = (current_elevation - min_e) / rng_e
|
||||
current_brightness = (pct * rng_b) + min_b
|
||||
|
||||
# self.logger.info(time)
|
||||
# self.logger.info(f'Elevation: {current_elevation:.0f}, {pct*100:.1f}%')
|
||||
# self.logger.info(f'Brightness: {current_brightness:.0f}')
|
||||
|
||||
print(time)
|
||||
print(f'Elevation: {current_elevation:.0f}, {pct*100:.1f}%')
|
||||
print(f'Brightness: {current_brightness:.0f}')
|
||||
|
||||
return int(round(current_brightness))
|
||||
Reference in New Issue
Block a user