Skip to content

Home Information - Your home, your data, organized.

Python

Fixed code style errors from Flake8 run.

Anthony Cassandraa7c730eeee97
diff --git a/.github/workflows/django-tests.yml b/.github/workflows/django-tests.yml
new file mode 100644
index 00000000..527b62e2
--- /dev/null
+++ b/.github/workflows/django-tests.yml
@@ -0,0 +1,35 @@
+name: Django Tests
+
+on:
+ pull_request:
+ branches: [master, staging]
+
+jobs:
+ build:
+
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout@v3
+
+ - name: Set up Python
+ uses: actions/setup-python@v4
+ with:
+ python-version: '3.11'
+
+ - name: Install Dependencies
+ run: |
+ python -m pip install --upgrade pip
+ pip install -r ./src/hi/requirements/development.txt
+
+ - name: Run Unit Tests
+ working-directory: ./src
+ run: |
+ python manage.py test --keepdb
+
+ - name: Check Code Style
+ working-directory: ./src
+ run: |
+ flake8 . --config=.flake8-ci --ignore=E501
+
+
diff --git a/src/.flake8 b/src/.flake8
index af861518..1f2d81a3 100644
--- a/src/.flake8
+++ b/src/.flake8
@@ -17,3 +17,5 @@ max-line-length = 110
# W503 - line break before binary operator
ignore = E129,D203,E201,E202,E203,E221,E231,E251,W293,W291,W391,W503
+
+exclude = hi/settings/*
diff --git a/src/custom/management/commands/hi_creategroups.py b/src/custom/management/commands/hi_creategroups.py
index daeb8be1..0c0ea67d 100644
--- a/src/custom/management/commands/hi_creategroups.py
+++ b/src/custom/management/commands/hi_creategroups.py
@@ -1,7 +1,5 @@
from django.core.management.base import BaseCommand
-from django.contrib.auth.models import Group
-
class Command( BaseCommand ):
"""
diff --git a/src/hi/apps/alert/admin.py b/src/hi/apps/alert/admin.py
deleted file mode 100644
index 8c38f3f3..00000000
--- a/src/hi/apps/alert/admin.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.contrib import admin
-
-# Register your models here.
diff --git a/src/hi/apps/alert/alert_manager.py b/src/hi/apps/alert/alert_manager.py
index 62dbf57c..c077cdd5 100644
--- a/src/hi/apps/alert/alert_manager.py
+++ b/src/hi/apps/alert/alert_manager.py
@@ -70,7 +70,7 @@ class AlertManager( Singleton, NotificationMixin ):
# whether to show it based on the console settings and user
# interaction context.
#
- latest_new_alarm = self._alert_queue.get_most_recent_alarm(
+ _ = self._alert_queue.get_most_recent_alarm(
since_datetime = last_alert_status_datetime,
)
diff --git a/src/hi/apps/collection/edit/async_views.py b/src/hi/apps/collection/edit/async_views.py
deleted file mode 100644
index 3c44e156..00000000
--- a/src/hi/apps/collection/edit/async_views.py
+++ /dev/null
@@ -1,27 +0,0 @@
-import json
-import logging
-
-from django.core.exceptions import BadRequest
-from django.http import Http404
-from django.template.loader import get_template
-from django.utils.decorators import method_decorator
-from django.views.generic import View
-
-from hi.apps.collection.collection_manager import CollectionManager
-from hi.apps.collection.models import CollectionPosition
-from hi.apps.collection.transient_models import CollectionEditData
-from hi.apps.collection.view_mixin import CollectionViewMixin
-import hi.apps.common.antinode as antinode
-from hi.apps.entity.view_mixin import EntityViewMixin
-from hi.apps.location.svg_item_factory import SvgItemFactory
-from hi.apps.location.location_manager import LocationManager
-
-from hi.constants import DIVID
-from hi.decorators import edit_required
-from hi.hi_async_view import HiSideView
-
-from . import forms
-
-logger = logging.getLogger(__name__)
-
-
diff --git a/src/hi/apps/common/email_utils.py b/src/hi/apps/common/email_utils.py
index df02ad31..10b20274 100644
--- a/src/hi/apps/common/email_utils.py
+++ b/src/hi/apps/common/email_utils.py
@@ -82,7 +82,7 @@ def send_html_email( request,
for file in files:
message.attach_file(file)
- if False and non_blocking and not settings.UNIT_TESTING: # Unit tests will fail if async emails
+ if non_blocking and not settings.UNIT_TESTING: # Unit tests will fail if async emails
EmailThread( message = message ).start()
else:
message.send()
diff --git a/src/hi/apps/common/tests/test_datetimeproxy.py b/src/hi/apps/common/tests/test_datetimeproxy.py
index ebcef7e4..95cb2e64 100644
--- a/src/hi/apps/common/tests/test_datetimeproxy.py
+++ b/src/hi/apps/common/tests/test_datetimeproxy.py
@@ -31,8 +31,8 @@ class TestDateTimeProxy(unittest.TestCase):
datetimeproxy.set( future_dt )
proxy_dt = datetimeproxy.now()
delta = proxy_dt - native_dt
- self.assertTrue( delta.total_seconds() >= (5*60*60-1) )
- self.assertTrue( delta.total_seconds() <= (5*60*60+1) )
+ self.assertTrue( delta.total_seconds() >= (5 * 60 * 60 - 1) )
+ self.assertTrue( delta.total_seconds() <= (5 * 60 * 60 + 1) )
##########
# Setting specific time in the past
@@ -43,8 +43,8 @@ class TestDateTimeProxy(unittest.TestCase):
datetimeproxy.set( future_dt )
proxy_dt = datetimeproxy.now()
delta = proxy_dt - native_dt
- self.assertTrue( -1*delta.total_seconds() >= (5*60*60-1) )
- self.assertTrue( -1*delta.total_seconds() <= (5*60*60+1) )
+ self.assertTrue( -1 * delta.total_seconds() >= (5 * 60 * 60 - 1) )
+ self.assertTrue( -1 * delta.total_seconds() <= (5 * 60 * 60 + 1) )
##########
# Incrementing time forward
@@ -54,8 +54,8 @@ class TestDateTimeProxy(unittest.TestCase):
datetimeproxy.increment( hours=8 )
proxy_dt = datetimeproxy.now()
delta = proxy_dt - native_dt
- self.assertTrue( delta.total_seconds() >= (8*60*60-1) )
- self.assertTrue( delta.total_seconds() <= (8*60*60+1) )
+ self.assertTrue( delta.total_seconds() >= (8 * 60 * 60 - 1) )
+ self.assertTrue( delta.total_seconds() <= (8 * 60 * 60 + 1) )
##########
# Incrementing time backward
@@ -65,8 +65,8 @@ class TestDateTimeProxy(unittest.TestCase):
datetimeproxy.increment( hours=-8 )
proxy_dt = datetimeproxy.now()
delta = proxy_dt - native_dt
- self.assertTrue( -1*delta.total_seconds() >= (8*60*60-1) )
- self.assertTrue( -1*delta.total_seconds() <= (8*60*60+1) )
+ self.assertTrue( -1 * delta.total_seconds() >= (8 * 60 * 60 - 1) )
+ self.assertTrue( -1 * delta.total_seconds() <= (8 * 60 * 60 + 1) )
return
@@ -97,25 +97,25 @@ class TestDateTimeProxy(unittest.TestCase):
##########
# Ensuring timezones working for common cases
- proxy_dt = datetimeproxy.reset()
+ _ = datetimeproxy.reset()
localnow = datetimeproxy.now('America/New_York')
delta_secs = localnow.utcoffset().total_seconds()
- min_secs = -1 * 4 * 60 * 60 + 2 # EDT
+ min_secs = -1 * 4 * 60 * 60 + 2 # EDT
max_secs = -1 * 5 * 60 * 60 - 2 # EST
self.assertTrue( (( delta_secs < min_secs )
and ( delta_secs > max_secs )))
localnow = datetimeproxy.now('America/Chicago')
delta_secs = localnow.utcoffset().total_seconds()
- min_secs = -1 * 5 * 60 * 60 + 2 # CDT
+ min_secs = -1 * 5 * 60 * 60 + 2 # CDT
max_secs = -1 * 6 * 60 * 60 - 2 # CST
self.assertTrue( (( delta_secs < min_secs )
and ( delta_secs > max_secs )))
##########
# Ensure time of day shows up relative to timezone
- naive_dt = datetime.datetime( year=2016, month=3, day=29,
- hour=17, minute=12, second=12 )
+ naive_dt = datetime.datetime( year = 2016, month = 3, day = 29,
+ hour = 17, minute = 12, second = 12 )
tzinfo = pytz.timezone('UTC')
force_dt = tzinfo.localize(naive_dt)
datetimeproxy.set( force_dt )
@@ -130,8 +130,8 @@ class TestDateTimeProxy(unittest.TestCase):
def test_rfc2822_conversions(self):
- naive_dt = datetime.datetime( year=2016, month=3, day=29,
- hour=17, minute=12, second=12 )
+ naive_dt = datetime.datetime( year = 2016, month = 3, day = 29,
+ hour = 17, minute = 12, second = 12 )
tzinfo = pytz.timezone('UTC')
the_dt = tzinfo.localize(naive_dt)
@@ -141,7 +141,6 @@ class TestDateTimeProxy(unittest.TestCase):
result_dt = datetimeproxy.rfc2822_to_datetime( rfc2822_string )
self.assertEqual( the_dt, result_dt )
return
-
def test_week_of_month(self):
diff --git a/src/hi/apps/common/tests/test_utils.py b/src/hi/apps/common/tests/test_utils.py
index 032cfc6f..20ed106f 100644
--- a/src/hi/apps/common/tests/test_utils.py
+++ b/src/hi/apps/common/tests/test_utils.py
@@ -81,76 +81,76 @@ class CommonUtilsTestCase(TestCase):
data_list = [
{ 'secs': 0,
'expect': '0 secs',
- },
+ },
{ 'secs': 1,
'expect': '1 sec',
- },
+ },
{ 'secs': 2,
'expect': '2 secs',
- },
+ },
{ 'secs': 59,
'expect': '59 secs',
- },
+ },
{ 'secs': 60,
'expect': '1 min',
- },
+ },
{ 'secs': 61,
'expect': '1 min, 1 sec',
- },
+ },
{ 'secs': 65,
'expect': '1 min, 5 secs',
- },
+ },
{ 'secs': 120,
'expect': '2 mins',
- },
+ },
{ 'secs': 121,
'expect': '2 mins, 1 sec',
- },
+ },
{ 'secs': 126,
'expect': '2 mins, 6 secs',
- },
+ },
{ 'secs': 60 * 60 - 1,
'expect': '59 mins, 59 secs',
- },
+ },
{ 'secs': 60 * 60,
'expect': '1 hr',
- },
+ },
{ 'secs': 60 * 60 + 1,
'expect': '1 hr, 1 sec',
- },
+ },
{ 'secs': 60 * 60 + 24,
'expect': '1 hr, 24 secs',
- },
+ },
{ 'secs': 60 * 60 + 61,
'expect': '1 hr, 1 min, 1 sec',
- },
+ },
{ 'secs': 60 * 60 + 68,
'expect': '1 hr, 1 min, 8 secs',
- },
+ },
{ 'secs': 60 * 60 + 120,
'expect': '1 hr, 2 mins',
- },
+ },
{ 'secs': 60 * 60 + 121,
'expect': '1 hr, 2 mins, 1 sec',
- },
+ },
{ 'secs': 60 * 60 + 127,
'expect': '1 hr, 2 mins, 7 secs',
- },
+ },
{ 'secs': 2 * 60 * 60 + 127,
'expect': '2 hrs, 2 mins, 7 secs',
- },
+ },
{ 'secs': 24 * 60 * 60 + 1,
'expect': '1 day, 1 sec',
- },
+ },
{ 'secs': 24 * 60 * 60 + 11,
'expect': '1 day, 11 secs',
- },
+ },
{ 'secs': 24 * 60 * 60 + 60,
'expect': '1 day, 1 min',
- },
+ },
{ 'secs': 24 * 60 * 60 + 3669,
'expect': '1 day, 1 hr, 1 min, 9 secs',
- },
+ },
]
for data in data_list:
diff --git a/src/hi/apps/common/views.py b/src/hi/apps/common/views.py
deleted file mode 100644
index 91ea44a2..00000000
--- a/src/hi/apps/common/views.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.shortcuts import render
-
-# Create your views here.
diff --git a/src/hi/apps/console/transient_models.py b/src/hi/apps/console/transient_models.py
index 928a3b19..c2d3775b 100644
--- a/src/hi/apps/console/transient_models.py
+++ b/src/hi/apps/console/transient_models.py
@@ -2,7 +2,6 @@ from dataclasses import dataclass
from hi.apps.entity.models import Entity, EntityState
from hi.apps.sense.models import Sensor
-from hi.apps.sense.transient_models import SensorResponse
@dataclass
diff --git a/src/hi/apps/edit/forms.py b/src/hi/apps/edit/forms.py
deleted file mode 100644
index e79048bf..00000000
--- a/src/hi/apps/edit/forms.py
+++ /dev/null
@@ -1 +0,0 @@
-from django import forms
diff --git a/src/hi/apps/event/event_manager.py b/src/hi/apps/event/event_manager.py
index c98f5bee..06a4d0a3 100644
--- a/src/hi/apps/event/event_manager.py
+++ b/src/hi/apps/event/event_manager.py
@@ -141,7 +141,6 @@ class EventManager( Singleton, AlertMixin, ControllerMixin ):
event_definition = event_definition,
sensor_response_list = sensor_response_list,
)
- return event
def _purge_old_transitions( self ):
current_timestamp = datetimeproxy.now()
diff --git a/src/hi/apps/monitor/monitor_manager.py b/src/hi/apps/monitor/monitor_manager.py
index a9ca22cc..ccfd13a2 100644
--- a/src/hi/apps/monitor/monitor_manager.py
+++ b/src/hi/apps/monitor/monitor_manager.py
@@ -23,7 +23,7 @@ class AppMonitorManager( Singleton ):
self._monitor_event_loop = None
return
- async def initialize( self, event_loop : "AbstractEventLoop" ) -> None:
+ async def initialize( self, event_loop ) -> None:
with self._data_lock:
if self._initialized:
logger.info('MonitorManager already initialize. Skipping.')
diff --git a/src/hi/apps/notify/transient_models.py b/src/hi/apps/notify/transient_models.py
index 8affe159..caee2b26 100644
--- a/src/hi/apps/notify/transient_models.py
+++ b/src/hi/apps/notify/transient_models.py
@@ -1,7 +1,5 @@
-from dataclasses import dataclass, field
-from typing import Dict, List, Union
-
-from django.http.request import HttpRequest
+from dataclasses import dataclass
+from typing import List
@dataclass
diff --git a/src/hi/apps/security/security_monitor.py b/src/hi/apps/security/security_monitor.py
index c3b51ee7..18feb1d1 100644
--- a/src/hi/apps/security/security_monitor.py
+++ b/src/hi/apps/security/security_monitor.py
@@ -1,7 +1,6 @@
import logging
import hi.apps.common.datetimeproxy as datetimeproxy
-from hi.apps.config.settings_mixins import SettingsMixin
from hi.apps.console.settings import ConsoleSetting
from hi.apps.monitor.periodic_monitor import PeriodicMonitor
diff --git a/src/hi/integrations/integration_manage_view_pane.py b/src/hi/integrations/integration_manage_view_pane.py
index 113ca692..ce7bac8a 100644
--- a/src/hi/integrations/integration_manage_view_pane.py
+++ b/src/hi/integrations/integration_manage_view_pane.py
@@ -7,5 +7,5 @@ class IntegrationManageViewPane:
def get_template_name( self ) -> str:
raise NotImplementedError('Subclasses must override this method')
- def get_template_context( self, integration_data : 'IntegrationData' ) -> Dict[ str, object ]:
+ def get_template_context( self, integration_data ) -> Dict[ str, object ]:
raise NotImplementedError('Subclasses must override this method')
diff --git a/src/hi/integrations/integration_manager.py b/src/hi/integrations/integration_manager.py
index 56c7878e..6b71f4cf 100644
--- a/src/hi/integrations/integration_manager.py
+++ b/src/hi/integrations/integration_manager.py
@@ -70,7 +70,7 @@ class IntegrationManager( Singleton ):
return self._integration_data_map[integration_id].integration_gateway
raise KeyError( f'Unknown integration id "{integration_id}".' )
- async def initialize( self, event_loop : "AbstractEventLoop" ) -> None:
+ async def initialize( self, event_loop ) -> None:
"""
This should be initialized from the background thread where the
integration monitor task will run.
diff --git a/src/hi/services/hass/admin.py b/src/hi/services/hass/admin.py
deleted file mode 100644
index 8c38f3f3..00000000
--- a/src/hi/services/hass/admin.py
+++ /dev/null
@@ -1,3 +0,0 @@
-from django.contrib import admin
-
-# Register your models here.
diff --git a/src/hi/services/hass/hass_converter.py b/src/hi/services/hass/hass_converter.py
index b9845109..2797663e 100644
--- a/src/hi/services/hass/hass_converter.py
+++ b/src/hi/services/hass/hass_converter.py
@@ -4,7 +4,6 @@ from typing import Dict, List
from django.db import transaction
-from hi.apps.alert.enums import AlarmLevel
from hi.apps.attribute.enums import (
AttributeType,
AttributeValueType,
diff --git a/src/hi/services/hass/monitors.py b/src/hi/services/hass/monitors.py
index 48363551..7986a8e8 100644
--- a/src/hi/services/hass/monitors.py
+++ b/src/hi/services/hass/monitors.py
@@ -1,4 +1,3 @@
-from asgiref.sync import async_to_sync
import logging
import hi.apps.common.datetimeproxy as datetimeproxy
diff --git a/src/hi/simulator/simulator_data.py b/src/hi/simulator/simulator_data.py
index 95afb843..84dbf8f6 100644
--- a/src/hi/simulator/simulator_data.py
+++ b/src/hi/simulator/simulator_data.py
@@ -1,5 +1,5 @@
from dataclasses import dataclass, field
-from typing import Dict, List
+from typing import Dict
from .base_models import SimEntityDefinition
from .simulator import Simulator
diff --git a/src/hi/tests/ui/views.py b/src/hi/tests/ui/views.py
index 3b91b548..9d00ce08 100644
--- a/src/hi/tests/ui/views.py
+++ b/src/hi/tests/ui/views.py
@@ -1,4 +1,3 @@
-import importlib
import logging
from django.apps import apps
diff --git a/src/hi/views.py b/src/hi/views.py
index 91dfc349..b0f46bf6 100644
--- a/src/hi/views.py
+++ b/src/hi/views.py
@@ -1,7 +1,6 @@
import json
from typing import Dict
-from django.db import connection
from django.http import (
HttpRequest,
HttpResponse,