Skip to content

Commit

Permalink
fix: light changes
Browse files Browse the repository at this point in the history
  • Loading branch information
janwo committed Sep 24, 2023
1 parent 70ee51c commit 61aee24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions smartblueberry/backend/plugins/homeassistant/modules/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,14 @@ async function setupLightModeEntities(server: hapi.Server) {
async function setupConstantLightModes(server: hapi.Server) {
const callback = async () => {
for (const { area_id } of server.app.hassRegistry.getAreas()) {
const lightMode = await getAreaLightMode(server, area_id)
switch (lightMode?.mode) {
const { mode, options } = (await getAreaLightMode(server, area_id)) || {}
switch (mode) {
case 'on':
await server.app.hassRegistry.callService('light', 'turn_on', {
target: { area_id },
service_data: { brightness: lightMode.options.brightness }
service_data: {
brightness: options?.brightness
}
})
break

Expand All @@ -243,8 +245,8 @@ async function setupConstantLightModes(server: hapi.Server) {

server.events.on(EVENT_STORAGE.STORAGE_UPDATED, callback)
server.events.on(
EVENT_HASSREGISTRY.ENTITY_UPDATED,
async (entityId: string) => {
EVENT_HASSREGISTRY.STATE_UPDATED,
async ({ entity_id: entityId }: State) => {
if (
entityId == OBJECT_IDS.lightCondition(server) ||
entityId.startsWith(OBJECT_IDS.lightMode(server, ''))
Expand Down Expand Up @@ -293,7 +295,7 @@ async function setupAutoOnLightMode(server: hapi.Server) {
area_id,
last_updated: (last_updated: string) =>
elapsedTime.isAfter(last_updated),
state: 'off'
state: 'on'
})
.map(({ entity_id }) => entity_id)

Expand All @@ -318,8 +320,9 @@ async function setupSimulateLightMode(server: hapi.Server) {
server.plugins.schedule.addJob('every 5 minutes', async () => {
try {
for (const { area_id } of server.app.hassRegistry.getAreas()) {
const lightMode = await getAreaLightMode(server, area_id)
switch (lightMode?.mode) {
const { mode, options } =
(await getAreaLightMode(server, area_id)) || {}
switch (mode) {
case 'simulate':
const historyTimestamp = dayjs(dayjs())
.subtract(1, 'month')
Expand Down Expand Up @@ -353,7 +356,10 @@ async function setupSimulateLightMode(server: hapi.Server) {

const service = hadActiveLight ? 'turn_on' : 'turn_off'
await server.app.hassRegistry.callService('light', service, {
target: { entity_id: lightEntityNames }
target: { entity_id: lightEntityNames },
service_data: {
brightness: options?.brightness
}
})
}
break
Expand Down Expand Up @@ -401,7 +407,10 @@ async function getAreaLightMode(server: hapi.Server, areaId: string) {
mode: lightMode[lightConditionMap[lightConditionEntity.state]],
options: {
duration: lightMode.duration,
brightness: lightMode.brightness
brightness:
lightMode.brightness === undefined
? undefined
: Math.round(lightMode.brightness * 255)
}
}
)
Expand Down
2 changes: 1 addition & 1 deletion smartblueberry/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"slug": "smartblueberry",
"name": "Smart Blueberry 🫐",
"version": "1.0.0",
"version": "1.0.1",
"description": "Smart Blueberry is a Home Assistant environment providing multiple extensions to simplify the configuration of light, presence and irrigation management.",
"url": "https://github.com/janwo/smartblueberry/tree/main/smartblueberry",
"image": "janwo/smartblueberry-{arch}",
Expand Down

0 comments on commit 61aee24

Please sign in to comment.