Skip to content

Commit

Permalink
Add pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Jan 1, 2020
1 parent 1ed2afe commit aed6ef4
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ venv/
ENV/
env.bak/
venv.bak/
/.idea/

# Spyder project settings
.spyderproject
Expand Down
18 changes: 18 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
repos:
- repo: local
hooks:
- id: update-tracker
name: "Update Tracker"
entry: "./update_tracker.py"
language: system
- id: pylint
name: pylint
entry: python3 -m pylint.__main__
language: system
types: [python]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: check-json
- id: check-yaml
- id: trailing-whitespace
52 changes: 26 additions & 26 deletions custom_components/car_wash/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
_LOGGER = logging.getLogger(__name__)

# Base component constants
VERSION = '1.2.7'
VERSION = '1.2.8'
ISSUE_URL = "https://github.com/Limych/ha-car_wash/issues"

CONF_WEATHER = 'weather'
Expand All @@ -50,7 +50,7 @@


async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
discovery_info=None): # pylint: disable=w0613
"""Set up the Car Wash sensor."""
# Print startup message
_LOGGER.info('Version %s', VERSION)
Expand Down Expand Up @@ -79,12 +79,12 @@ async def async_added_to_hass(self):
"""Register callbacks."""

@callback
def sensor_state_listener(entity, old_state, new_state):
def sensor_state_listener(entity, old_state, new_state): # pylint: disable=w0613
"""Handle device state changes."""
self.async_schedule_update_ha_state(True)

@callback
def sensor_startup(event):
def sensor_startup(event): # pylint: disable=w0613
"""Update template on startup."""
async_track_state_change(
self._hass, [self._weather_entity], sensor_state_listener)
Expand Down Expand Up @@ -123,28 +123,28 @@ def _temp2c(temperature: float, temperature_unit: str) -> float:

return temperature

async def async_update(self):
async def async_update(self): # pylint: disable=r0912,r0915
"""Update the sensor state."""
wd = self._hass.states.get(self._weather_entity)
wdata = self._hass.states.get(self._weather_entity)

if wd is None:
if wdata is None:
raise HomeAssistantError(
'Unable to find an entity called {}'.format(
self._weather_entity))

tu = self._hass.config.units.temperature_unit
t = wd.attributes.get(ATTR_WEATHER_TEMPERATURE)
cond = wd.state
forecast = wd.attributes.get(ATTR_FORECAST)
tmpu = self._hass.config.units.temperature_unit
temp = wdata.attributes.get(ATTR_WEATHER_TEMPERATURE)
cond = wdata.state
forecast = wdata.attributes.get(ATTR_FORECAST)

if forecast is None:
raise HomeAssistantError(
'Can\'t get forecast data!'
' Are you sure it\'s the weather provider?')

_LOGGER.debug('Current temperature %s, condition \'%s\'', t, cond)
_LOGGER.debug('Current temperature %s, condition \'%s\'', temp, cond)

t = self._temp2c(t, tu)
temp = self._temp2c(temp, tmpu)

if cond in BAD_CONDITIONS:
_LOGGER.debug('Detected bad weather condition')
Expand All @@ -157,9 +157,9 @@ async def async_update(self):
).strftime('%F')

_LOGGER.debug('Inspect weather forecast from now till %s', stop_date)
for fc in forecast:
fc_date = fc.get(ATTR_FORECAST_TIME)
if type(fc_date) == int:
for fcast in forecast:
fc_date = fcast.get(ATTR_FORECAST_TIME)
if isinstance(fc_date) == int:
fc_date = dt_util.as_local(datetime.utcfromtimestamp(
fc_date / 1000)).isoformat()
fc_date = fc_date[:10]
Expand All @@ -169,10 +169,10 @@ async def async_update(self):
break
_LOGGER.debug('Inspect weather forecast for %s', fc_date)

prec = fc.get(ATTR_FORECAST_PRECIPITATION)
cond = fc.get(ATTR_FORECAST_CONDITION)
tmin = fc.get(ATTR_FORECAST_TEMP_LOW)
tmax = fc.get(ATTR_FORECAST_TEMP)
prec = fcast.get(ATTR_FORECAST_PRECIPITATION)
cond = fcast.get(ATTR_FORECAST_CONDITION)
tmin = fcast.get(ATTR_FORECAST_TEMP_LOW)
tmax = fcast.get(ATTR_FORECAST_TEMP)
_LOGGER.debug(
'Precipitation %s, Condition \'%s\','
' Min temperature: %s, Max temperature %s',
Expand All @@ -187,23 +187,23 @@ async def async_update(self):
self._state = False
return
if tmin is not None and fc_date != cur_date:
tmin = self._temp2c(tmin, tu)
if t < 0 <= tmin:
tmin = self._temp2c(tmin, tmpu)
if temp < 0 <= tmin:
_LOGGER.debug(
'Detected passage of temperature through melting'
' point')
self._state = False
return
t = tmin
temp = tmin
if tmax is not None:
tmax = self._temp2c(tmax, tu)
if t < 0 <= tmax:
tmax = self._temp2c(tmax, tmpu)
if temp < 0 <= tmax:
_LOGGER.debug(
'Detected passage of temperature through melting'
' point')
self._state = False
return
t = tmax
temp = tmax

_LOGGER.debug('Inspection done. No bad forecast detected')
self._state = True
5 changes: 5 additions & 0 deletions dev-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

pip3 install -r requirements.txt -r requirements-dev.txt --user
pre-commit install
pre-commit autoupdate
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pre-commit
pylint
2 changes: 1 addition & 1 deletion tracker.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"car_wash": {
"version": "1.2.7",
"version": "1.2.8",
"local_location": "/custom_components/car_wash/__init__.py",
"remote_location": "https://raw.githubusercontent.com/Limych/ha-car_wash/master/custom_components/car_wash/__init__.py",
"visit_repo": "https://github.com/Limych/ha-car_wash",
Expand Down
60 changes: 38 additions & 22 deletions update_tracker.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env python3
"""Tracker updater for custom_updater."""

# Copyright (c) 2019, Andrey "Limych" Khrolenok <[email protected]>
# Creative Commons BY-NC-SA 4.0 International Public License
# (see LICENSE.md or https://creativecommons.org/licenses/by-nc-sa/4.0/)

import copy
import json
import logging
import os
Expand All @@ -15,6 +18,9 @@

_LOGGER = logging.getLogger(__name__)

TRACKER_FPATH = 'custom_components.json' if os.path.isfile('custom_components.json') \
else 'tracker.json'


def fallback_version(localpath):
"""Return version from regex match."""
Expand All @@ -32,7 +38,7 @@ def fallback_version(localpath):

def get_component_version(localpath, name):
"""Return the local version if any."""
_LOGGER.debug('Started for ' + localpath)
_LOGGER.debug('Started for %s', localpath)
if '.' in name:
name = "{}.{}".format(name.split('.')[1], name.split('.')[0])
return_value = ''
Expand All @@ -57,23 +63,33 @@ def get_component_version(localpath, name):
return return_value


with open('tracker.json', 'r') as tracker_file:
tracker = json.load(tracker_file)
for package in tracker:
_LOGGER.info('Updating version for %s', package)
local_path = tracker[package]['local_location'].lstrip('/\\')
tracker[package]['version'] = \
get_component_version(local_path, package)
base_path = os.path.split(local_path)[0]
base_url = os.path.split(tracker[package]['remote_location'])[0]
resources = []
for current_path, dirs, files in os.walk(base_path):
if current_path.find('__pycache__') != -1:
continue
for file in files:
file = os.path.join(current_path, file).replace('\\', '/')
if file != local_path:
resources.append(base_url + file[len(base_path):])
tracker[package]['resources'] = resources
with open('tracker.json', 'w') as tracker_file:
json.dump(tracker, tracker_file, indent=4)
def update_tracker(tracker_fpath):
"""Run tracker file update."""
with open(tracker_fpath, 'r') as tracker_file:
tracker = json.load(tracker_file)
old_tr = copy.deepcopy(tracker)
for package in tracker:
_LOGGER.info('Updating version for %s', package)
local_path = tracker[package]['local_location'].lstrip('/\\')
tracker[package]['version'] = \
get_component_version(local_path, package)
base_path = os.path.split(local_path)[0]
base_url = os.path.split(tracker[package]['remote_location'])[0]
resources = []
for current_path, _, files in os.walk(base_path):
if current_path.find('__pycache__') != -1:
continue
for file in files:
file = os.path.join(current_path, file).replace('\\', '/')
if file != local_path:
resources.append(base_url + file[len(base_path):])
resources.sort()
tracker[package]['resources'] = resources

if tracker != old_tr:
with open(tracker_fpath, 'w') as tracker_file:
json.dump(tracker, tracker_file, indent=4)


update_tracker(TRACKER_FPATH)
# subprocess.run(["git", "add", TRACKER_FPATH])

0 comments on commit aed6ef4

Please sign in to comment.