Skip to content

Commit

Permalink
Adjusted dev environment
Browse files Browse the repository at this point in the history
  • Loading branch information
iprak authored and github-actions[bot] committed Oct 29, 2020
1 parent 5b57744 commit 8ea14ff
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 19 deletions.
17 changes: 17 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.7

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

RUN python -m pip install --upgrade colorlog black pylint
RUN python -m pip install --upgrade git+git://github.com/home-assistant/home-assistant.git@dev
RUN cd && mkdir -p /config/custom_components


WORKDIR /workspace

# Set the default shell to bash instead of sh
ENV SHELL /bin/bash
53 changes: 53 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Devcontainer

_The easiest way to contribute to and/or test this repository._

## Requirements

- [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- [docker](https://docs.docker.com/install/)
- [VS Code](https://code.visualstudio.com/)
- [Remote - Containers (VSC Extention)](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers)

[More info about requirements and devcontainer in general](https://code.visualstudio.com/docs/remote/containers#_getting-started)

## How to use Devcontainer for development/test

1. Make sure your computer meets the requirements.
1. Fork this repository.
1. Clone the repository to your computer.
1. Open the repository using VS Code.

When you open this repository with VSCode and your computer meets the requirements you are asked to "Reopen in Container", do that.

![reopen](images/reopen.png)

If you don't see this notification, open the command pallet (ctrl+shift+p) and select `Remote-Containers: Reopen Folder in Container`.

_It will now build the devcontainer._

The container have some "tasks" to help you testing your changes.

## Custom Tasks in this repository

_Start "tasks" by opening the the command pallet (ctrl+shift+p) and select `Tasks: Run Task`_

Running tasks like `Start Home Assistant on port 8124` can be restarted by opening the the command pallet (ctrl+shift+p) and select `Tasks: Restart Running Task`, then select the task you want to restart.

### Start Home Assistant on port 8124

This will copy the configuration and the integration files to the expected location in the container.

And start up Home Assistant on [port 8124.](http://localhost:8124)

### Upgrade Home Assistant to latest dev

This will upgrade Home Assistant to the latest dev version.

### Set Home Assistant Version

This allows you to specify a version of Home Assistant to install inside the devcontainer.

### Home Assistant Config Check

This runs a config check to make sure your config is valid.
12 changes: 12 additions & 0 deletions .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
default_config:
logger:
default: error
logs:
custom_components.yahoofinance: debug


sensor:
platform: yahoofinance
use_trending_icon: True
symbols:
- ISTNX
26 changes: 26 additions & 0 deletions .devcontainer/custom_component_helper
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

function StartHomeAssistant {
echo "Copy configuration.yaml"
cp -f .devcontainer/configuration.yaml /config || echo ".devcontainer/configuration.yaml are missing!" exit 1

echo "Copy the custom component"
rm -R /config/custom_components/ || echo ""
cp -r custom_components /config/custom_components/ || echo "Could not copy the custom_component" exit 1

echo "Start Home Assistant"
hass -c /config
}

function UpdgradeHomeAssistantDev {
python -m pip install --upgrade git+git://github.com/home-assistant/home-assistant.git@dev
}

function SetHomeAssistantVersion {
read -p 'Version: ' version
python -m pip install --upgrade homeassistant==$version
}

function HomeAssistantConfigCheck {
hass -c /config --script check_config
}
24 changes: 24 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// See https://aka.ms/vscode-remote/devcontainer.json for format details.
{
"context": "..",
"dockerFile": "Dockerfile",
"appPort": "8124:8123",
"runArgs": [
"-e",
"GIT_EDTIOR='code --wait'"
],
"extensions": [
"ms-python.python",
"spmeesseman.vscode-taskexplorer"
],
"settings": {
"python.pythonPath": "/usr/local/bin/python",
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.formatting.provider": "black",
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"files.trimTrailingWhitespace": true
}
}
Binary file added .devcontainer/images/reopen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

61 changes: 61 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Start Home Assistant on port 8124",
"type": "shell",
"command": "source .devcontainer/custom_component_helper && StartHomeAssistant",
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": []
},
{
"label": "Upgrade Home Assistant to latest dev",
"type": "shell",
"command": "source .devcontainer/custom_component_helper && UpdgradeHomeAssistantDev",
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": []
},
{
"label": "Set Home Assistant Version",
"type": "shell",
"command": "source .devcontainer/custom_component_helper && SetHomeAssistantVersion",
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": []
},
{
"label": "Home Assistant Config Check",
"type": "shell",
"command": "source .devcontainer/custom_component_helper && HomeAssistantConfigCheck",
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": []
}
]
}
33 changes: 17 additions & 16 deletions custom_components/yahoofinance/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@

import aiohttp
import async_timeout
import voluptuous as vol

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ATTR_ATTRIBUTION, CONF_SCAN_INTERVAL
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import Entity, async_generate_entity_id
from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import (
ATTR_CURRENCY_SYMBOL,
Expand Down Expand Up @@ -52,7 +48,9 @@
)


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): # pylint: disable=unused-argument
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None
): # pylint: disable=unused-argument
"""Set up the Yahoo Finance sensors."""
symbols = config.get(CONF_SYMBOLS, [])
coordinator = YahooSymbolUpdateCoordinator(
Expand All @@ -73,7 +71,9 @@ async def handle_refresh_symbols(_call):
await coordinator.async_request_refresh()

hass.services.async_register(
DOMAIN, SERVICE_REFRESH, handle_refresh_symbols,
DOMAIN,
SERVICE_REFRESH,
handle_refresh_symbols,
)

_LOGGER.info("Added sensors for %s", symbols)
Expand All @@ -96,9 +96,7 @@ def __init__(self, coordinator, symbol, hass) -> None:
"""Initialize the sensor."""
self._symbol = symbol
self._coordinator = coordinator
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, symbol, hass=hass
)
self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, symbol, hass=hass)
_LOGGER.debug("Created %s", self.entity_id)

@property
Expand Down Expand Up @@ -196,7 +194,10 @@ def __init__(self, symbols, hass, update_interval) -> None:
self.websession = async_get_clientsession(hass)

super().__init__(
hass, _LOGGER, name=DOMAIN, update_interval=update_interval,
hass,
_LOGGER,
name=DOMAIN,
update_interval=update_interval,
)

async def _async_update_data(self):
Expand All @@ -212,9 +213,7 @@ async def get_json(self):
json = None
try:
async with async_timeout.timeout(DEFAULT_TIMEOUT, loop=self.loop):
response = await self.websession.get(
BASE + ",".join(self._symbols)
)
response = await self.websession.get(BASE + ",".join(self._symbols))
json = await response.json()

_LOGGER.debug("Data = %s", json)
Expand Down Expand Up @@ -251,7 +250,9 @@ async def update(self):
"financialCurrency": item.get("financialCurrency"),
}
_LOGGER.debug(
"Updated %s=%s", symbol, data[symbol]["regularMarketPrice"],
"Updated %s=%s",
symbol,
data[symbol]["regularMarketPrice"],
)

self.data = data
Expand Down

0 comments on commit 8ea14ff

Please sign in to comment.